Last active
April 29, 2019 09:44
-
-
Save greatsharma/6c703301194a2cd65b6adae44b58e937 to your computer and use it in GitHub Desktop.
A simple c++ program to sort an array using bubble sort algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Name: Bubble Sort | |
Author: Gaurav Sharma | |
Date: 28-04-19 17:29 | |
Description: A simple c++ program to sort an array using bubble sort algorithm. | |
*/ | |
#include <iostream> | |
#include <conio.h> | |
#include <time.h> | |
#define NO_RUNS 100 | |
using namespace std; | |
int bubbleSort(int *Arr, const int &size) | |
{ | |
int count = 0; | |
for (int i = size - 1; i >= 1; --i) | |
{ | |
for (int j = 0; j < i; ++j) | |
{ | |
++count; | |
if (Arr[j] > Arr[j + 1]) | |
{ | |
int temp = Arr[j]; | |
Arr[j] = Arr[j + 1]; | |
Arr[j + 1] = temp; | |
} | |
} | |
} | |
return count; | |
} | |
int main() | |
{ | |
srand(time(0)); | |
int *compr_array = new int[NO_RUNS]; | |
for (int k = 0; k < NO_RUNS; k++) | |
{ | |
cout << "\n\nRUN " << k + 1; | |
int size = 5 + (rand() % (15 - (5) + 1)); | |
cout << "\n\nsize : " << size; | |
int *arr = new int[size]; | |
cout << "\n\nInitial array : "; | |
for (int i = 0; i < size; i++) | |
{ | |
arr[i] = -100 + (rand() % (100 - (-100) + 1)); | |
cout << " " << arr[i]; | |
} | |
int no_comp = bubbleSort(arr, size); | |
compr_array[k] = no_comp; | |
cout << "\n\ntotal no of comparisons : " << no_comp; | |
cout << "\n\nSorted array : \n"; | |
for (int i = 0; i < size; i++) | |
{ | |
cout << "\n" | |
<< arr[i]; | |
} | |
} | |
getch(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment