Skip to content

Instantly share code, notes, and snippets.

@niklasbuschmann
Last active June 24, 2020 20:20
Show Gist options
  • Save niklasbuschmann/2eda399e21014cd54e96f653a2b4621c to your computer and use it in GitHub Desktop.
Save niklasbuschmann/2eda399e21014cd54e96f653a2b4621c to your computer and use it in GitHub Desktop.
#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