Last active
October 8, 2019 08:43
-
-
Save surinoel/2bd9413e09a5654e635ea128a6f43428 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
using namespace std; | |
void swap(int& a, int& b) { | |
int tmp = a; | |
a = b; | |
b = tmp; | |
} | |
int partition(vector<int>& arr, int left, int right) { | |
int pivot = arr[right]; | |
int i = left - 1; | |
for (int j = left; j <= right - 1; j++) { | |
if (pivot > arr[j]) { | |
i++; | |
swap(arr[i], arr[j]); | |
} | |
} | |
swap(arr[i + 1], arr[right]); | |
return (i + 1); | |
} | |
void quicksort(vector<int>& arr, int left, int right) { | |
if (left < right) { | |
int p = partition(arr, left, right); | |
quicksort(arr, left, p - 1); | |
quicksort(arr, p + 1, right); | |
} | |
return; | |
} | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n; | |
cin >> n; | |
vector<int> arr(n); | |
for (int i = 0; i < n; i++) { | |
cin >> arr[i]; | |
} | |
quicksort(arr, 0, n - 1); | |
for (int i = 0; i < n; i++) { | |
cout << arr[i] << '\n'; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <iostream> | |
using namespace std; | |
void swap(int& a, int& b) { | |
int tmp = a; | |
a = b; | |
b = tmp; | |
} | |
int partition(vector<int>& arr, int l, int r) { | |
int pivot = arr[l]; | |
int i = r + 1; | |
for (int j = r; j > l; j--) { | |
if (pivot < arr[j]) { | |
i--; | |
swap(arr[i], arr[j]); | |
} | |
} | |
swap(arr[i - 1], arr[l]); | |
return (i - 1); | |
} | |
void quicksort(vector<int>& arr, int l, int r) { | |
if (l < r) { | |
int p = partition(arr, l, r); | |
quicksort(arr, l, p - 1); | |
quicksort(arr, p + 1, r); | |
} | |
} | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n; | |
cin >> n; | |
vector<int> a(n); | |
for (int i = 0; i < n; i++) { | |
cin >> a[i]; | |
} | |
quicksort(a, 0, n - 1); | |
for (int i = 0; i < n; i++) { | |
cout << a[i] << '\n'; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment