Created
October 2, 2016 19:06
-
-
Save abrarShariar/a35c4ddb348115223c61e2ae7559b856 to your computer and use it in GitHub Desktop.
Calculating execution time of different sorting algorithms using clock()
This file contains hidden or 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<ctime> | |
| using namespace std; | |
| void bubbleSort1(int*,int); | |
| void bubbleSort2(int*,int); | |
| void bubbleSort3(int*,int); | |
| void insertionSort(int*,int); | |
| void selectionSort(int*,int); | |
| void doSwap(int*,int*); | |
| void printArray(int*,int); | |
| int main(){ | |
| int arr1[10] = {10,9,8,6,4,3,1,2,5,7}; | |
| int arr2[10] = {10,9,8,6,4,3,1,2,5,7}; | |
| int arr3[10] = {10,9,8,6,4,3,1,2,5,7}; | |
| int arr4[10] = {10,9,8,6,4,3,1,2,5,7}; | |
| int arr5[10] = {10,9,8,6,4,3,1,2,5,7}; | |
| int N = 10; | |
| int start_time,stop_time; | |
| start_time = clock(); | |
| insertionSort(arr1,N); | |
| stop_time = clock(); | |
| cout<<"Insertion sort time: "<<(stop_time-start_time)/double(CLOCKS_PER_SEC)*1000<<endl; | |
| start_time = clock(); | |
| selectionSort(arr2,N); | |
| stop_time = clock(); | |
| cout<<"Selection sort time: "<<(stop_time-start_time)/double(CLOCKS_PER_SEC)*1000<<endl; | |
| start_time = clock(); | |
| bubbleSort1(arr3,N); | |
| stop_time = clock(); | |
| cout<<"BubbleSort 1 time: "<<(stop_time-start_time)/double(CLOCKS_PER_SEC)*1000<<endl; | |
| start_time = clock(); | |
| bubbleSort2(arr4,N); | |
| stop_time = clock(); | |
| cout<<"BubbleSort 2 time: "<<(stop_time-start_time)/double(CLOCKS_PER_SEC)*1000<<endl; | |
| start_time = clock(); | |
| bubbleSort2(arr5,N); | |
| stop_time = clock(); | |
| cout<<"BubbleSort 3 time: "<<(stop_time-start_time)/double(CLOCKS_PER_SEC)*1000<<endl; | |
| //printArray(arr3,N) | |
| //printArray(arr2,N); | |
| //printArray(arr3,N); | |
| } | |
| //selection sort | |
| void selectionSort(int* arr,int N){ | |
| for(int i=0;i<N;i++){ | |
| int minNum = arr[i]; | |
| int minKey = i; | |
| for(int j=i+1;j<N;j++){ | |
| if(arr[j] < minNum){ | |
| minNum = arr[j]; | |
| minKey = j; | |
| } | |
| } | |
| int temp = arr[i]; | |
| arr[i] = minNum; | |
| arr[minKey] = temp; | |
| } | |
| } | |
| //insertion sort | |
| void insertionSort(int* arr,int N){ | |
| for(int i=1;i<N;i++){ | |
| int key = arr[i]; | |
| int j = i-1; | |
| while(j>=0 && key<arr[j]){ | |
| arr[j+1] = arr[j]; | |
| j--; | |
| } | |
| arr[j+1] = key; | |
| } | |
| } | |
| //bubble sort 1 | |
| void bubbleSort1(int* arr,int N){ | |
| for(int i=1;i<N;i++){ | |
| for(int j=0;j<N-1;j++){ | |
| if(arr[j+1] < arr[j]){ | |
| doSwap(&arr[j+1],&arr[j]); | |
| } | |
| } | |
| } | |
| } | |
| //bubble sort 2 | |
| void bubbleSort2(int* arr,int N){ | |
| for(int i=1;i<N;i++){ | |
| int swaps = 0; | |
| for(int j=0;j<N-1;j++){ | |
| if(arr[j+1] < arr[j]){ | |
| doSwap(&arr[j+1],&arr[j]); | |
| swaps++; | |
| } | |
| } | |
| if(swaps == 0){ | |
| break; | |
| } | |
| } | |
| } | |
| //bubble sort 3 | |
| void bubbleSort3(int* arr,int N){ | |
| for(int i=1;i<N;i++){ | |
| int swaps = 0; | |
| for(int j=0;j<N-i;j++){ | |
| if(arr[j+1] < arr[j]){ | |
| doSwap(&arr[j+1],&arr[j]); | |
| swaps++; | |
| } | |
| } | |
| if(swaps == 0){ | |
| break; | |
| } | |
| } | |
| } | |
| //swap fuction | |
| void doSwap(int* x,int* y){ | |
| int temp = *x; | |
| *x = *y; | |
| *y = temp; | |
| } | |
| //print function | |
| void printArray(int* arr,int N){ | |
| for(int i=0;i<N;i++){ | |
| cout<<arr[i]<<" "; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment