Skip to content

Instantly share code, notes, and snippets.

@tornikegomareli
Created April 7, 2017 17:40
Show Gist options
  • Save tornikegomareli/9eddc4b78947c4b566b8eabf3cbcef38 to your computer and use it in GitHub Desktop.
Save tornikegomareli/9eddc4b78947c4b566b8eabf3cbcef38 to your computer and use it in GitHub Desktop.
Bubble Sort vs Selection Sort Competition #165
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <Windows.h>
#include <conio.h>
using namespace std;
void Rand(int Array[], const int Size)
{
for (int i = 0; i < Size; i++)
{
Array[i] = rand() % 100;
}
}
void Print(int Array[], const int Size)
{
for (int i = 0; i < Size; i++)
{
cout << Array[i] << " ";
}
}
void SelectionSort(int Array[], const int Size)
{
for (int i = 0; i < Size; i++)
{
for (int j = i+1; j < Size; j++)
{
if (Array[i] > Array[j])
{
int tmp = Array[i];
Array[i] = Array[j];
Array[j] = tmp;
}
}
}
}
void BubbleSort(int Array[], int Size)
{
for (int i = 0; i < Size; i++)
{
for (int j = 0; j < Size-1; j++)
{
if (Array[j] > Array[j + 1])
{
int tmp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = tmp;
}
}
}
}
void Swap(int A, int B)
{
int tmp = A;
A = B;
B = tmp;
}
int main()
{
const int Length = 100000;
int Array[Length];
Rand(Array, Length);
clock_t start;
clock_t end;
clock_t total;
cout << " Starting Bubble Sort !! " << endl;
cout << " ............... " << endl;
start = clock();
BubbleSort(Array, Length);
end = clock();
total = (double)(end - start) / CLOCKS_PER_SEC;
cout << " Finished ! " << endl;
cout << " Time : " << total;
start = 0;
end = 0;
total = 0;
cout << " Starting with Selection Sort " << endl;
cout << " ...................... " << endl;
start = clock();
SelectionSort(Array, Length);
end = clock();
total = (double)(end - start) / CLOCKS_PER_SEC;
cout << " Finished " << endl;
cout << " Time : " << total;
cin.get();
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment