Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Algorithm comparison code for cpp #135

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Cpp/AlgorithmComparison.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Compares various sorting algorithms.
* To execute, compile files using:
* g++ *.cpp -o output
* Run:
* output.exe
*/

#include <bits/stdc++.h>
#include <algorithm>
#include <chrono>
#include<string>

#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 CYCLE:
countSort(arrCopy, SIZE);
algorithmUsed = "CYCLE_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<chrono::nanoseconds>(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::CYCLE, 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);
}

32 changes: 4 additions & 28 deletions Cpp/BubbleSort.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#include <bits/stdc++.h>
#include <bits/stdc++.h>
#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)
{
Expand All @@ -19,31 +14,12 @@ 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)
break;
}
}

/* 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;
}

30 changes: 10 additions & 20 deletions Cpp/CountSort.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
//Implementing Count Sort
#include <iostream>
#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<num;i++)
cin>>arr[i];

int max = arr[0];
int min = arr[0];
for(i=1;i<num;i++){//Iterating through the array for maxiumum and minimum items
for(int i=1;i<num;i++){//Iterating through the array for maxiumum and minimum items
if(max<arr[i])
max=arr[i];

Expand All @@ -30,18 +21,17 @@ int main()

//Creating an array with an index for each element between minimum and maximum items of given array
sums = (int*) calloc(sumSize,sizeof(int));//Using calloc so that memory allocation and initialization (to 0) happens in one go
for(i=0;i<num;i++)
for(int i=0;i<num;i++)
sums[arr[i]-min]++; //Storing count of each unique item in sums array at the its index

for(i=1;i<sumSize;i++)
for(int i=1;i<sumSize;i++)
sums[i] = sums[i-1]+sums[i]; //Cumulating sums of all items

int sorted[num]; //For sorted items
for(i=0;i<num;i++){
for(int i=0;i<num;i++){
sums[arr[i]-min]--;
sorted[sums[arr[i]-min]] = arr[i]; //Checking values in sums array and inserting items in sorted array at appropriate indices
}
for(i=0;i<num;i++)
cout<<sorted[i]<<" ";
return 0;

copy(sorted, sorted + num, arr);
}
51 changes: 51 additions & 0 deletions Cpp/CycleSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
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]);
}
}
}
}
19 changes: 1 addition & 18 deletions Cpp/HeapSort.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include "sorting_algorithms.h"

using namespace std;

Expand Down Expand Up @@ -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<n; ++i)
cout << arr[i] << " ";
cout << "\n";
}

// Driver program
int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);

heapSort(arr, n);

cout << "Sorted array is \n";
printArray(arr, n);
}

25 changes: 3 additions & 22 deletions Cpp/InsertionSort.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <bits/stdc++.h>
#include <bits/stdc++.h>
#include "sorting_algorithms.h"

using namespace std;

/* Function to sort an array using insertion sort*/
Expand All @@ -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;
}


25 changes: 4 additions & 21 deletions Cpp/MergeSort.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include "sorting_algorithms.h"

using namespace std;

Expand Down Expand Up @@ -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);
}
Loading