Created
November 20, 2018 19:12
-
-
Save primetoxinz/784a820e4bdb39816d569798556ecdd1 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <random> | |
#include <algorithm> | |
#include <iterator> | |
#include <cstring> | |
int randInt(int min, int max) { | |
std::random_device generator; | |
std::uniform_int_distribution<int> dist_int(min, max); | |
return dist_int(generator); | |
} | |
void fillArray(int array[], int length) { | |
for (int i = 0; i < length; i++) { | |
array[i] = randInt(0, 1000); | |
} | |
} | |
void swap(int *xp, int *yp) { | |
int temp = *xp; | |
*xp = *yp; | |
*yp = temp; | |
} | |
void bubbleSort(int arr[], int n, int &compare, int &assign) { | |
int i, j; | |
for (i = 0; i < n - 1; i++) { | |
for (j = 0; j < n - i - 1; j++) { | |
if (arr[j] > arr[j + 1]) { | |
compare++; | |
swap(&arr[j], &arr[j + 1]); | |
assign++; | |
} | |
} | |
} | |
} | |
void selectionSort(int arr[], int n, int &compare, int &assign) { | |
int i, j, min_idx; | |
for (i = 0; i < n - 1; i++) { | |
min_idx = i; | |
for (j = i + 1; j < n; j++) { | |
if (arr[j] < arr[min_idx]) { | |
min_idx = j; | |
compare++; | |
} | |
} | |
swap(&arr[min_idx], &arr[i]); | |
assign++; | |
} | |
} | |
void insertionSort(int arr[], int n, int &compare, int &assign) { | |
int i, key, j; | |
for (i = 1; i < n; i++) { | |
key = arr[i]; | |
j = i - 1; | |
while (j >= 0 && arr[j] > key) { | |
compare++; | |
assign++; | |
arr[j + 1] = arr[j]; | |
j = j - 1; | |
} | |
arr[j + 1] = key; | |
assign++; | |
} | |
} | |
int main() { | |
const int length = 5000; | |
int list1[length], list2[length], list3[length]; | |
fillArray(list1, length); | |
//Copy first array to second array | |
memcpy(list2, list1, sizeof(list1)); | |
//Copy first array to third array | |
memcpy(list3, list1, sizeof(list1)); | |
int bubbleCompares = 0, bubbleAssigns = 0; | |
int selectionCompares = 0, selectionAssigns = 0; | |
int insertionCompares = 0, insertionAssigns = 0; | |
bubbleSort(list1, length, bubbleCompares, bubbleAssigns); | |
selectionSort(list2, length, selectionCompares, selectionAssigns); | |
insertionSort(list3, length, insertionCompares, insertionAssigns); | |
std::cout << "Total Comparisions" << std::endl | |
<< "Bubble Sort Comparisons:" << bubbleCompares << std::endl | |
<< "Bubble Sort Assignments:" << bubbleAssigns << std::endl << std::endl | |
<< "Selection Sort Comparisons:" << selectionCompares << std::endl | |
<< "Selection Sort Assignments:" << selectionAssigns << std::endl << std::endl | |
<< "Insertion Sort Comparisons:" << insertionCompares << std::endl | |
<< "Insertion Sort Assignments:" << insertionAssigns << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment