Last active
June 24, 2020 20:20
-
-
Save niklasbuschmann/2eda399e21014cd54e96f653a2b4621c 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 <iostream> | |
#include <random> | |
using namespace std; | |
double random_number() { | |
return double (rand()) / RAND_MAX; | |
} | |
int partition(double A[], int s, int t) { | |
double pivot = A[s]; | |
int l = s; | |
for (int i = s + 1; i <= t; i++) { | |
if (A[i] < pivot) { | |
l = l + 1; | |
swap(A[i], A[l]); | |
} | |
} | |
swap(A[s], A[l]); | |
return l; | |
} | |
void quicksort(double A[], int s, int t) { | |
if (t <= s) | |
return; | |
int p = partition(A, s, t); | |
quicksort(A, s, p - 1); | |
quicksort(A, p + 1, t); | |
} | |
int main () { | |
srand(time(NULL)); | |
double A[1000]; | |
for (auto &d: A) | |
d = random_number(); | |
for (auto d: A) | |
cout << d << " "; | |
quicksort(A, 0, 999); | |
cout << endl << endl << "Sorted: " << endl << endl; | |
for (auto d: A) | |
cout << d << " "; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment