Skip to content

Instantly share code, notes, and snippets.

@maxgoren
Last active June 12, 2023 17:52
Show Gist options
  • Save maxgoren/0b8c8ba8cc4fdefe41fe302905b6022e to your computer and use it in GitHub Desktop.
Save maxgoren/0b8c8ba8cc4fdefe41fe302905b6022e to your computer and use it in GitHub Desktop.
#include <iostream>
#include <chrono>
using namespace std;
void sorterQ(unsigned long a, int * b) {
unsigned long c = a;
unsigned long d = 0;
unsigned long e;
int f;
while (a != 1) {
a--;
if (b[a] > b[a - 1]) {
d = a - 1;
e = d;
f = b[d];
while (d != 0) {
d--;
if (b[d] < f) {
f = b[d];
e = d;
}
}
b[e] = b[a];
b[a] = f;
}
}
while (d == 0) {
a = c;
while (a != 1) {
a--;
if (b[a] > b[a - 1]) {
d = b[a];
b[a] = b[a - 1];
b[a - 1] = d;
d = 1;
}
}
d ^= 1;
}
return;
}
void insertionsort(int a[], int n) {
for (int i = 1; i < n; i++) {
int j = i; int v = a[j];
while (j > 0 && a[j - 1] > v) {
a[j] = a[j - 1];
j--;
}
a[j] = v;
}
}
void checksort(int a[], int n) {
for (int i = 0; i < n - 1; i++) {
if (a[i+1] < a[i]) {
cout<<"Sort failed.\n";
return;
}
}
cout<<"Sorted.\n";
}
int main() {
int a[1000];
int b[1000];
for (int i = 0; i < 1000; i++) {
int p = rand() % 31337;
a[i] = p;
b[i] = p;
}
auto t1 = std::chrono::steady_clock::now();
sorterQ(1000, a);
auto t2 = std::chrono::steady_clock::now();
auto timing = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(t2 - t1);
cout<<"Reddit sort Completed in: "<<timing.count()<<"ms"<<endl;
checksort(a, 1000);
auto t3 = std::chrono::steady_clock::now();
insertionsort(b, 1000);
auto t4 = std::chrono::steady_clock::now();
auto timing2 = std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(t4 - t3);
cout<<"Insertion sort Completed in: "<<timing2.count()<<"ms"<<endl;
checksort(b, 1000);
return 0;
}
/*
max@MaxGorenLaptop:/mnt/c/Users/mgoren/langtonsJava$ g++ stupid.cpp -o stupid
max@MaxGorenLaptop:/mnt/c/Users/mgoren/langtonsJava$ ./stupid
Reddit sort Completed in: 1.11821ms
Sort failed.
Insertion sort Completed in: 0.660489ms
Sorted.
max@MaxGorenLaptop:/mnt/c/Users/mgoren/langtonsJava$
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment