Skip to content

Instantly share code, notes, and snippets.

@mat-staszczyk
Created April 3, 2015 08:31
Show Gist options
  • Save mat-staszczyk/a425aaea97527fc3b359 to your computer and use it in GitHub Desktop.
Save mat-staszczyk/a425aaea97527fc3b359 to your computer and use it in GitHub Desktop.
Bubble-sort performance test
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// Setting of data amount
const int N = 10000;
int main()
{
int data[N+1], pom, nr, i;
bool marker;
srand(time(NULL));
for(i = 1; i<=N; i++) {
// cout << "Element nr " << i << " ";
// cin >> data[i];
// Sorted data
//data[i] = i;
// Conversely sorted data
//data[i] = -i;
// Random data
data[i] = rand() % 32000;
}
// Timig test
clock_t start = clock();
// Bubble sort:
/*
for (int nr = 1; nr < N; nr++) {
for (i = 1; i <= N-nr; i++) {
if (data[i] > data[i+1]) {
pom = data[i];
data[i] = data[i+1];
data[i+1] = pom;
}
}
}
*/
// Bubble sort with marker
nr = 1;
do {
marker = 0;
for (i=1; i<=N-nr;i++) {
if (data[i] > data[i+1]) {
marker = 1;
pom = data[i];
data[i] = data[i+1];
data[i+1] = pom;
}
}
nr++;
} while (marker);
// Testing section:
for (int i = 1; i<=N; i++) {
cout << data[i] << endl;
}
cout << "Sorting algorithm execution time: " << clock() - start << "ms." << endl;
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment