From 17f3a1dbec3a6c41e247764eae34dc31100690c4 Mon Sep 17 00:00:00 2001 From: RohitMazumder Date: Thu, 1 Oct 2020 00:17:21 +0530 Subject: [PATCH 1/3] Added Algorithm comparison code for cpp --- Cpp/AlgorithmComparison.cpp | 101 ++++++++++++++++++++++++++++++++++++ Cpp/BubbleSort.cpp | 32 ++---------- Cpp/CountSort.cpp | 30 ++++------- Cpp/HeapSort.cpp | 19 +------ Cpp/InsertionSort.cpp | 25 ++------- Cpp/MergeSort.cpp | 25 ++------- Cpp/QuickSort.cpp | 35 ++----------- Cpp/SelectionSort.cpp | 30 ++--------- Cpp/sort_type.h | 9 ++++ Cpp/sorting_algorithms.h | 13 +++++ 10 files changed, 152 insertions(+), 167 deletions(-) create mode 100644 Cpp/AlgorithmComparison.cpp create mode 100644 Cpp/sort_type.h create mode 100644 Cpp/sorting_algorithms.h diff --git a/Cpp/AlgorithmComparison.cpp b/Cpp/AlgorithmComparison.cpp new file mode 100644 index 0000000..b44fff6 --- /dev/null +++ b/Cpp/AlgorithmComparison.cpp @@ -0,0 +1,101 @@ +/** +* Compares various sorting algorithms. +* To execute, compile files using: +* g++ *.cpp -o output +* Run: +* output.exe +*/ + +#include +#include +#include +#include + +#include "sort_type.h" +#include "sorting_algorithms.h" + +using namespace std; + +int SIZE = 600; +int MAX_ELEMENT = 1000; + +void printSortingTime(SortType sortType, int arr[]){ + int arrCopy[SIZE]; + copy(arr, arr + SIZE, arrCopy); + string algorithmUsed = ""; + + auto start = chrono::high_resolution_clock::now(); + + switch(sortType){ + case BUBBLE: + bubbleSort(arrCopy, SIZE); + algorithmUsed = "BUBBLE_SORT"; + break; + case COUNT: + countSort(arrCopy, SIZE); + algorithmUsed = "COUNT_SORT"; + break; + case HEAP: + heapSort(arrCopy, SIZE); + algorithmUsed = "HEAP_SORT"; + break; + case INSERTION: + insertionSort(arrCopy, SIZE); + algorithmUsed = "INSERTION_SORT"; + break; + case MERGE: + MergeSort(arrCopy, 0, SIZE - 1); + algorithmUsed = "MERGE_SORT"; + break; + case QUICK: + quickSort(arrCopy, 0, SIZE - 1); + algorithmUsed = "QUICK_SORT"; + break; + case SELECTION: + selectionSort(arrCopy, SIZE); + algorithmUsed = "SELECTION_SORT"; + break; + } + + auto finish = chrono::high_resolution_clock::now(); + auto timeTaken = chrono::duration_cast(finish-start).count(); + printf("%-17s %15d \n", algorithmUsed.c_str(), timeTaken); + +} + +void compareSortingAlgorithms(int arr[]){ + printSortingTime(SortType::BUBBLE, arr); + printSortingTime(SortType::COUNT, arr); + printSortingTime(SortType::HEAP, arr); + printSortingTime(SortType::INSERTION, arr); + printSortingTime(SortType::MERGE, arr); + printSortingTime(SortType::QUICK, arr); + printSortingTime(SortType::SELECTION, arr); +} + +int main(){ + int arr[SIZE]; + + for(int i = 0; i < SIZE; i++){ + arr[i] = rand() % MAX_ELEMENT; + } + + cout << "Array length = " << SIZE << endl; + cout << "\nElements randomly distributed: " << endl; + cout << "\nSorting Algorithm\tTime Taken(ns)" << endl; + + compareSortingAlgorithms(arr); + + cout << "\nElements already sorted: " << endl; + cout << "\nSorting Algorithm\tTime Taken(ns)" << endl; + + sort(arr, arr + SIZE); + compareSortingAlgorithms(arr); + + cout << "\nElements sorted in reverse order:" << endl; + cout << "\nSorting Algorithm\tTime Taken(ns)" << endl; + + reverse(arr, arr + SIZE); + compareSortingAlgorithms(arr); +} + diff --git a/Cpp/BubbleSort.cpp b/Cpp/BubbleSort.cpp index e500460..31345fa 100644 --- a/Cpp/BubbleSort.cpp +++ b/Cpp/BubbleSort.cpp @@ -1,13 +1,8 @@ -#include +#include +#include "sorting_algorithms.h" + using namespace std; -void swap(int *xp, int *yp) -{ - int temp = *xp; - *xp = *yp; - *yp = temp; -} - // A function to implement bubble sort void bubbleSort(int arr[], int n) { @@ -19,7 +14,7 @@ void bubbleSort(int arr[], int n) for (j = 0; j < n-i-1; j++) if (arr[j] > arr[j+1]) { - swap(&arr[j], &arr[j+1]); + swap(arr[j], arr[j+1]); is_swap=1; } if(is_swap==0) @@ -27,23 +22,4 @@ void bubbleSort(int arr[], int n) } } -/* Function to print an array */ -void printArray(int arr[], int size) -{ - int i; - for (i = 0; i < size; i++) - cout << arr[i] << " "; - cout << endl; -} - -// Driver code -int main() -{ - int arr[] = {64, 34, 25, 12, 22, 11, 90}; - int n = sizeof(arr)/sizeof(arr[0]); - bubbleSort(arr, n); - cout<<"Sorted array: \n"; - printArray(arr, n); - return 0; -} diff --git a/Cpp/CountSort.cpp b/Cpp/CountSort.cpp index 5cac78f..1084ae8 100644 --- a/Cpp/CountSort.cpp +++ b/Cpp/CountSort.cpp @@ -1,24 +1,15 @@ //Implementing Count Sort #include +#include "sorting_algorithms.h" + using namespace std; -int main() + +void countSort(int arr[], int num) { - cout<<"Enter number of items: "; //For size of array - int num, i; - cin>>num; - while(num<=0){ - cout<<"Enter a number more than 0\n"; - cin>>num; - } - int arr[num]; - cout<<"Enter the items: "; - - for(i=0;i>arr[i]; int max = arr[0]; int min = arr[0]; - for(i=1;i +#include "sorting_algorithms.h" using namespace std; @@ -46,23 +47,5 @@ void heapSort(int arr[], int n) } } -/* A utility function to print array of size n */ -void printArray(int arr[], int n) -{ - for (int i=0; i +#include +#include "sorting_algorithms.h" + using namespace std; /* Function to sort an array using insertion sort*/ @@ -22,25 +24,4 @@ void insertionSort(int arr[], int n) } } -// A utility function to print an array of size n -void printArray(int arr[], int n) -{ - int i; - for (i = 0; i < n; i++) - cout << arr[i] << " "; - cout << endl; -} - -/* Driver code */ -int main() -{ - int arr[] = { 12, 11, 13, 5, 6 }; - int n = sizeof(arr) / sizeof(arr[0]); - - insertionSort(arr, n); - printArray(arr, n); - - return 0; -} - diff --git a/Cpp/MergeSort.cpp b/Cpp/MergeSort.cpp index a1be3e0..3bc077b 100644 --- a/Cpp/MergeSort.cpp +++ b/Cpp/MergeSort.cpp @@ -1,4 +1,5 @@ #include +#include "sorting_algorithms.h" using namespace std; @@ -70,26 +71,8 @@ MergeSort (int *a, int low, int high) } } -int -main () +void +MergeSort (int* a, int n) { - int n, i; - cout << "\nEnter the number of data element to be sorted: "; - cin >> n; - - int arr[n]; - for (i = 0; i < n; i++) - { - cout << "Enter element " << i + 1 << ": "; - cin >> arr[i]; - } - - MergeSort (arr, 0, n - 1); - - // Printing the sorted data. - cout << "\nSorted Data "; - for (i = 0; i < n; i++) - cout << "->" << arr[i]; - - return 0; + MergeSort(a, 0, n - 1); } diff --git a/Cpp/QuickSort.cpp b/Cpp/QuickSort.cpp index a7e019d..ebfc99f 100644 --- a/Cpp/QuickSort.cpp +++ b/Cpp/QuickSort.cpp @@ -1,13 +1,7 @@ #include -using namespace std; +#include "sorting_algorithms.h" -// A utility function to swap two elements -void swap(int* a, int* b) -{ - int t = *a; - *a = *b; - *b = t; -} +using namespace std; /* This function takes last element as pivot, places the pivot element at its correct position in sorted @@ -25,10 +19,10 @@ int partition (int arr[], int low, int high) if (arr[j] < pivot) { i++; // increment index of smaller element - swap(&arr[i], &arr[j]); + swap(arr[i], arr[j]); } } - swap(&arr[i + 1], &arr[high]); + swap(arr[i + 1], arr[high]); return (i + 1); } @@ -51,24 +45,3 @@ void quickSort(int arr[], int low, int high) } } -/* Function to print an array */ -void printArray(int arr[], int size) -{ - int i; - for (i = 0; i < size; i++) - cout << arr[i] << " "; - cout << endl; -} - -// Driver Code -int main() -{ - int arr[] = {10, 7, 8, 9, 1, 5}; - int n = sizeof(arr) / sizeof(arr[0]); - quickSort(arr, 0, n - 1); - cout << "Sorted array: \n"; - printArray(arr, n); - return 0; -} - - diff --git a/Cpp/SelectionSort.cpp b/Cpp/SelectionSort.cpp index 9976903..94b1372 100644 --- a/Cpp/SelectionSort.cpp +++ b/Cpp/SelectionSort.cpp @@ -1,12 +1,7 @@ #include -using namespace std; +#include "sorting_algorithms.h" -void swap(int *xp, int *yp) -{ - int temp = *xp; - *xp = *yp; - *yp = temp; -} +using namespace std; void selectionSort(int arr[], int n) { @@ -22,29 +17,10 @@ void selectionSort(int arr[], int n) min_idx = j; // Swap the found minimum element with the first element - swap(&arr[min_idx], &arr[i]); + swap(arr[min_idx], arr[i]); } } -/* Function to print an array */ -void printArray(int arr[], int size) -{ - int i; - for (i=0; i < size; i++) - cout << arr[i] << " "; - cout << endl; -} - -// Driver program to test above functions -int main() -{ - int arr[] = {64, 25, 12, 22, 11}; - int n = sizeof(arr)/sizeof(arr[0]); - selectionSort(arr, n); - cout << "Sorted array: \n"; - printArray(arr, n); - return 0; -} diff --git a/Cpp/sort_type.h b/Cpp/sort_type.h new file mode 100644 index 0000000..4db20b8 --- /dev/null +++ b/Cpp/sort_type.h @@ -0,0 +1,9 @@ +enum SortType { + BUBBLE, + COUNT, + HEAP, + INSERTION, + MERGE, + QUICK, + SELECTION +}; \ No newline at end of file diff --git a/Cpp/sorting_algorithms.h b/Cpp/sorting_algorithms.h new file mode 100644 index 0000000..b764531 --- /dev/null +++ b/Cpp/sorting_algorithms.h @@ -0,0 +1,13 @@ +#ifndef SORTING_ALGORITHMS_H +#define SORTING_ALGORITHMS_H + +void bubbleSort(int arr[], int n); +void countSort(int arr[], int num); +void heapSort(int arr[], int n); +void insertionSort(int arr[], int n); +void MergeSort (int *a, int low, int high); +void quickSort(int arr[], int low, int high); +void selectionSort(int arr[], int n); + + +#endif From cba089edf1422647a6ea23b7f929a95e5eec909d Mon Sep 17 00:00:00 2001 From: RohitMazumder Date: Thu, 1 Oct 2020 01:40:05 +0530 Subject: [PATCH 2/3] Added CycleSort.cpp --- Cpp/CycleSort.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Cpp/CycleSort.cpp diff --git a/Cpp/CycleSort.cpp b/Cpp/CycleSort.cpp new file mode 100644 index 0000000..21e0c81 --- /dev/null +++ b/Cpp/CycleSort.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; + +// Function sort the array using Cycle sort +void cycleSort(int arr[], int n) +{ + // This loop picks up an element in the currItem, + // and finds the position to place this currItem in posForCurrItem. + for (int start = 0; start <= n - 2; start++) { + int currItem = arr[start]; + + // Since all elements smaller than currItem must occupy a position before currItem, + // so to find posForCurrItem, we just need to get the number of smaller elements than currItem + // after start. + int smallerThanCurrItem = 0; + for (int i = start + 1; i < n; i++) + if (arr[i] < currItem) + smallerThanCurrItem++; + + int posForCurrItem = start + smallerThanCurrItem; + + // In case the element is already at its correct place, we can move to the next element. + if (posForCurrItem == start) + continue; + + // Special care needs to be taken to ignore the duplicate elements + while (currItem == arr[posForCurrItem]) + posForCurrItem += 1; + + // Now that we have the correct position for currItem, we can place it in its correct position. + if (posForCurrItem != start) { + swap(currItem, arr[posForCurrItem]); + } + + // Rotate the rest of the cycle. + while (posForCurrItem != start) { + posForCurrItem = start; + + for (int i = start + 1; i < n; i++) + if (arr[i] < currItem) + posForCurrItem += 1; + + while (currItem == arr[posForCurrItem]) + posForCurrItem += 1; + + if (currItem != arr[posForCurrItem]) { + swap(currItem, arr[posForCurrItem]); + } + } + } +} + +void printArray(int arr[], int n){ + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + + cout << endl; +} + +int main() +{ + int n = 6; + int maxElement = 100; + int arr[n]; + + for(int i = 0; i < n; i++) { + arr[i] = rand() % maxElement; + } + + cout << "Before sort: " << endl; + printArray(arr, n); + + cycleSort(arr, n); + + cout << "After sort : " << endl; + printArray(arr, n); + + return 0; +} From 4699e2ccd1a3d4a2388b52dd4f9615d16cd6fb12 Mon Sep 17 00:00:00 2001 From: RohitMazumder Date: Thu, 1 Oct 2020 02:00:31 +0530 Subject: [PATCH 3/3] Added CycleSort to Comparison --- Cpp/AlgorithmComparison.cpp | 5 +++++ Cpp/CycleSort.cpp | 28 ---------------------------- Cpp/sort_type.h | 1 + Cpp/sorting_algorithms.h | 1 + 4 files changed, 7 insertions(+), 28 deletions(-) diff --git a/Cpp/AlgorithmComparison.cpp b/Cpp/AlgorithmComparison.cpp index b44fff6..25d40f9 100644 --- a/Cpp/AlgorithmComparison.cpp +++ b/Cpp/AlgorithmComparison.cpp @@ -35,6 +35,10 @@ void printSortingTime(SortType sortType, int arr[]){ countSort(arrCopy, SIZE); algorithmUsed = "COUNT_SORT"; break; + case CYCLE: + countSort(arrCopy, SIZE); + algorithmUsed = "CYCLE_SORT"; + break; case HEAP: heapSort(arrCopy, SIZE); algorithmUsed = "HEAP_SORT"; @@ -66,6 +70,7 @@ void printSortingTime(SortType sortType, int arr[]){ void compareSortingAlgorithms(int arr[]){ printSortingTime(SortType::BUBBLE, arr); printSortingTime(SortType::COUNT, arr); + printSortingTime(SortType::CYCLE, arr); printSortingTime(SortType::HEAP, arr); printSortingTime(SortType::INSERTION, arr); printSortingTime(SortType::MERGE, arr); diff --git a/Cpp/CycleSort.cpp b/Cpp/CycleSort.cpp index 21e0c81..f098e5c 100644 --- a/Cpp/CycleSort.cpp +++ b/Cpp/CycleSort.cpp @@ -49,31 +49,3 @@ void cycleSort(int arr[], int n) } } } - -void printArray(int arr[], int n){ - for (int i = 0; i < n; i++) - cout << arr[i] << " "; - - cout << endl; -} - -int main() -{ - int n = 6; - int maxElement = 100; - int arr[n]; - - for(int i = 0; i < n; i++) { - arr[i] = rand() % maxElement; - } - - cout << "Before sort: " << endl; - printArray(arr, n); - - cycleSort(arr, n); - - cout << "After sort : " << endl; - printArray(arr, n); - - return 0; -} diff --git a/Cpp/sort_type.h b/Cpp/sort_type.h index 4db20b8..c8009d4 100644 --- a/Cpp/sort_type.h +++ b/Cpp/sort_type.h @@ -1,6 +1,7 @@ enum SortType { BUBBLE, COUNT, + CYCLE, HEAP, INSERTION, MERGE, diff --git a/Cpp/sorting_algorithms.h b/Cpp/sorting_algorithms.h index b764531..bb50327 100644 --- a/Cpp/sorting_algorithms.h +++ b/Cpp/sorting_algorithms.h @@ -3,6 +3,7 @@ void bubbleSort(int arr[], int n); void countSort(int arr[], int num); +void cycleSort(int arr[], int num); void heapSort(int arr[], int n); void insertionSort(int arr[], int n); void MergeSort (int *a, int low, int high);