Skip to content

Instantly share code, notes, and snippets.

@tornikegomareli
Created April 4, 2017 08:57
Show Gist options
  • Save tornikegomareli/4ac2d8d8d88f901e8c1a605d45830074 to your computer and use it in GitHub Desktop.
Save tornikegomareli/4ac2d8d8d88f901e8c1a605d45830074 to your computer and use it in GitHub Desktop.
Selection and Bubble Sort Analyze and Time comparison in C/C++
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
clock_t start;
clock_t end;
clock_t total;
const int length = 100000;
int Array[length];
int Array2[length];
for (int i = 0; i < length; i++)
{
Array[i] = rand() % 50;
Array2[i] = rand() % 50;
}
cout << " Starting Bubble Sort ! " << endl;
cout << "................." << endl;
start = clock();
for (int i = 0; i < length; i++)
{
for (size_t j = 0; j < length -1; j++)
{
if (Array[j] > Array[j+1])
{
int tmp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = tmp;
}
}
}
end = clock();
cout << " Finished " << endl;
total = (double)(end - start) / CLOCKS_PER_SEC;
cout << " Time of Bubble Sort : " << total << endl;
start = 0;
end = 0;
total = 0;
cout << " Starting Selection Sort !! " << endl;
cout << " ...................... " << endl;
start = clock();
for (size_t i = 0; i < length; i++)
{
for (size_t j = i+1; j < length; j++)
{
if (Array2[i] > Array2[j])
{
int tmp = Array2[i];
Array2[i] = Array2[j];
Array2[j] = tmp;
}
}
}
end = clock();
cout << " Finished " << endl;
total = (double)(end - start) / CLOCKS_PER_SEC;
cout << " Time of Selection Sort : " << total;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment