Last active
March 10, 2021 05:27
-
-
Save gunavaran/0f110c02c3c0914cd6101fba3aa9eae4 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
// Created by gunavaran on 3/9/21. | |
//Acknowledgement: The functions for all the sorting algorithms are extracted from | |
//https://www.geeksforgeeks.org/selection-sort/ | |
//https://www.geeksforgeeks.org/insertion-sort/ | |
//https://www.geeksforgeeks.org/bubble-sort/ | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <stdlib.h> | |
#include <sys/time.h> | |
void insertionSort(int arr[], int n) { | |
int i, key, j; | |
for (i = 1; i < n; i++) { | |
key = arr[i]; | |
j = i - 1; | |
/* Move elements of arr[0..i-1], that are | |
greater than key, to one position ahead | |
of their current position */ | |
while (j >= 0 && arr[j] > key) { | |
arr[j + 1] = arr[j]; | |
j = j - 1; | |
} | |
arr[j + 1] = key; | |
} | |
} | |
void selectionSort(int arr[], int n) { | |
int i, j, min_idx, temp; | |
// One by one move boundary of unsorted subarray | |
for (i = 0; i < n - 1; i++) { | |
// Find the minimum element in unsorted array | |
min_idx = i; | |
for (j = i + 1; j < n; j++) | |
if (arr[j] < arr[min_idx]) | |
min_idx = j; | |
// Swap the found minimum element with the first element | |
temp = arr[min_idx]; | |
arr[min_idx] = arr[i]; | |
arr[i] = temp; | |
} | |
} | |
void bubbleSort(int arr[], int n) { | |
int i, j, temp; | |
bool swapped; | |
for (i = 0; i < n - 1; i++) { | |
swapped = false; | |
for (j = 0; j < n - i - 1; j++) { | |
if (arr[j] > arr[j + 1]) { | |
temp = arr[j]; | |
arr[j] = arr[j + 1]; | |
arr[j + 1] = temp; | |
swapped = true; | |
} | |
} | |
// IF no two elements were swapped by inner loop, then break | |
if (swapped == false) | |
break; | |
} | |
} | |
// A utility function to print an array of size n | |
void printArray(int arr[], int n) { | |
int i; | |
for (i = 0; i < n; i++) | |
printf("%d ", arr[i]); | |
printf("\n"); | |
} | |
int main() { | |
int N = 100000, i, sorted_array[N], reversed_array[N], random_array[N]; | |
struct timeval start, end; | |
long micros, seconds; | |
for (i = 0; i < N; i++) { | |
sorted_array[i] = i; | |
reversed_array[N - 1 - i] = i; | |
random_array[i] = rand() % N; | |
} | |
// printArray(sorted_array, N); | |
// printArray(reversed_array, N); | |
// printArray(random_array, N); | |
gettimeofday(&start, NULL); | |
insertionSort(sorted_array, N); | |
// selectionSort(sorted_array, N); | |
// bubbleSort(sorted_array, N); | |
gettimeofday(&end, NULL); | |
seconds = (end.tv_sec - start.tv_sec); | |
micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec); | |
printf("Time elpased is %ld microseconds \n", micros); | |
gettimeofday(&start, NULL); | |
insertionSort(reversed_array, N); | |
// selectionSort(reversed_array, N); | |
// bubbleSort(reversed_array, N); | |
gettimeofday(&end, NULL); | |
seconds = (end.tv_sec - start.tv_sec); | |
micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec); | |
printf("Time elpased is %ld microseconds \n", micros); | |
gettimeofday(&start, NULL); | |
insertionSort(random_array, N); | |
// selectionSort(random_array, N); | |
// bubbleSort(random_array, N); | |
gettimeofday(&end, NULL); | |
seconds = (end.tv_sec - start.tv_sec); | |
micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec); | |
printf("Time elpased is %ld microseconds \n", micros); | |
// printArray(sorted_array, N); | |
// printArray(reversed_array, N); | |
// printArray(random_array, N); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment