Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <vector>
- #include <algorithm>
- void quicksort(std::vector<int>& arr, int left, int right) {
- if (left >= right) {
- return;
- }
- int pivot = arr[(left + right) / 2];
- int i = left;
- int j = right;
- while (i <= j) {
- while (arr[i] < pivot) {
- i++;
- }
- while (arr[j] > pivot) {
- j--;
- }
- if (i <= j) {
- std::swap(arr[i], arr[j]);
- i++;
- j--;
- }
- }
- std::thread t1(quicksort, std::ref(arr), left, j);
- std::thread t2(quicksort, std::ref(arr), i, right);
- t1.join();
- t2.join();
- }
- int main() {
- std::vector<int> arr = {9, 3, 7, 1, 5, 8, 2, 6, 4};
- quicksort(arr, 0, arr.size() - 1);
- std::cout << "Sorted array: ";
- for (int num : arr) {
- std::cout << num << " ";
- }
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement