From 79e187649d934d3fecefdaf5d652cffbbf11ceaf Mon Sep 17 00:00:00 2001 From: theycallmemac Date: Thu, 28 Sep 2017 21:31:27 +0100 Subject: [PATCH 1/3] create shellSort.c --- C | 81 +++++++++++++++++++++++++++++++++++++++++++++++ Sorts/shellSort.c | 48 ++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 C create mode 100644 Sorts/shellSort.c diff --git a/C b/C new file mode 100644 index 00000000..abf53655 --- /dev/null +++ b/C @@ -0,0 +1,81 @@ + +#include + +void merge(int *array, int low, int middle, int high){ + int left = middle - low + 1; + int right = high - middle; + + int tmpLeft[left], tmpRight[right]; + + for(int i = 0; i < left; i++){ + tmpLeft[i] = array[low + i]; + } + + for(int j = 0; j < right; j++){ + tmpRight[j] = array[middle + 1+ j]; + } + + int i = 0; + int j = 0; + int k = low; + + while(i < left && j < right){ + if (tmpLeft[i] <= tmpRight[j]){ + array[k] = tmpLeft[i]; + i++; + } + else{ + array[k] = tmpRight[j]; + j++; + } + k++; + } + + while(i < left){ + array[k] = tmpLeft[i]; + i++; + k++; + } + + while(j < right){ + array[k] = tmpRight[j]; + j++; + k++; + } +} + +void mergeSort(int array[], int low, int high){ + if (low < high){ + int middle = (low + high) / 2; + mergeSort(array, low, middle); + mergeSort(array , middle + 1, high); + merge(array, low, middle, high); + } + } + + +int main(){ + + int array[] = {10,7,3,9,18,-4,67,32,0,21}; + int size = sizeof array / sizeof array[0]; + int high = size - 1; + int low = 0; + printf("\n"); + printf("%s", "Original Array: \n"); + for(int i = 0; i < size; i++) { + printf("%d ",array[i]); + } + + printf("\n"); + printf("\n"); + printf("%s", "Sorted Array:\n"); + + mergeSort(array,low,high); + + for(int i = 0; i < size; i++) { + printf("%d ",array[i]); + } + + return 0; +} + diff --git a/Sorts/shellSort.c b/Sorts/shellSort.c new file mode 100644 index 00000000..e81cad95 --- /dev/null +++ b/Sorts/shellSort.c @@ -0,0 +1,48 @@ +#include +#include +#include + +void shellSort(int array[], int value){ + int i = value; + int j, k, tmp; + for (i = value / 2; i > 0; i = i / 2){ + for (j = i; j < value; j++){ + for(k = j - i; k >= 0; k = k - i){ + if (array[k+i] >= array[k]){ + break; + } + else{ + tmp = array[k]; + array[k] = array[k+i]; + array[k+i] = tmp; + } + } + } + } +} + + +int main(){ + + int array[20]; + int range = 500; + for(int i = 0; i < 100; i++){ + array[i] = rand() % range + 1; + } + int size = sizeof array / sizeof array[0]; + + + + clock_t start = clock(); + shellSort(array,size); + clock_t end = clock(); + double time_spent = (double)(end - start) / CLOCKS_PER_SEC; + + + printf("Data Sorted\n"); + printf("%s\n", "Shell Sort Big O Notation:\n--> Best Case: O(n log(n))\n--> Average Case: depends on gap sequence\n--> Worst Case: O(n)\n"); + printf("Time spent sorting: %f\n", time_spent); + + return 0; +} + From 8156611ab24bf2fdd63b41b877058e2613240e54 Mon Sep 17 00:00:00 2001 From: theycallmemac Date: Thu, 28 Sep 2017 21:32:39 +0100 Subject: [PATCH 2/3] created fibonacciSearch.c --- Searches/fibonacciSearch.c | 68 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Searches/fibonacciSearch.c diff --git a/Searches/fibonacciSearch.c b/Searches/fibonacciSearch.c new file mode 100644 index 00000000..297f192d --- /dev/null +++ b/Searches/fibonacciSearch.c @@ -0,0 +1,68 @@ +int fibMonaccianSearch(int arr[], int x, int n) +{ + /* Initialize fibonacci numbers */ + int fibMMm2 = 0; // (m-2)'th Fibonacci No. + int fibMMm1 = 1; // (m-1)'th Fibonacci No. + int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci + + /* fibM is going to store the smallest Fibonacci + Number greater than or equal to n */ + while (fibM < n) + { + fibMMm2 = fibMMm1; + fibMMm1 = fibM; + fibM = fibMMm2 + fibMMm1; + } + + // Marks the eliminated range from front + int offset = -1; + + /* while there are elements to be inspected. Note that + we compare arr[fibMm2] with x. When fibM becomes 1, + fibMm2 becomes 0 */ + while (fibM > 1) + { + // Check if fibMm2 is a valid location + int i = min(offset+fibMMm2, n-1); + + /* If x is greater than the value at index fibMm2, + cut the subarray array from offset to i */ + if (arr[i] < x) + { + fibM = fibMMm1; + fibMMm1 = fibMMm2; + fibMMm2 = fibM - fibMMm1; + offset = i; + } + + /* If x is greater than the value at index fibMm2, + cut the subarray after i+1 */ + else if (arr[i] > x) + { + fibM = fibMMm2; + fibMMm1 = fibMMm1 - fibMMm2; + fibMMm2 = fibM - fibMMm1; + } + + /* element found. return index */ + else return i; + } + + /* comparing the last element with x */ + if(fibMMm1 && arr[offset+1]==x)return offset+1; + + /*element not found. return -1 */ + return -1; +} + + +int main(void) +{ + int arr[] = {10, 22, 35, 40, 45, 50, 80, 82, + 85, 90, 100}; + int n = sizeof(arr)/sizeof(arr[0]); + int x = 85; + printf("Found at index: %d", + fibMonaccianSearch(arr, x, n)); + return 0; +} From ec6a9d55e4c4a312a7025cf81440c924c6a7c99b Mon Sep 17 00:00:00 2001 From: James Mc Dermott Date: Sun, 1 Oct 2017 22:00:19 +0100 Subject: [PATCH 3/3] Delete C --- C | 81 --------------------------------------------------------------- 1 file changed, 81 deletions(-) delete mode 100644 C diff --git a/C b/C deleted file mode 100644 index abf53655..00000000 --- a/C +++ /dev/null @@ -1,81 +0,0 @@ - -#include - -void merge(int *array, int low, int middle, int high){ - int left = middle - low + 1; - int right = high - middle; - - int tmpLeft[left], tmpRight[right]; - - for(int i = 0; i < left; i++){ - tmpLeft[i] = array[low + i]; - } - - for(int j = 0; j < right; j++){ - tmpRight[j] = array[middle + 1+ j]; - } - - int i = 0; - int j = 0; - int k = low; - - while(i < left && j < right){ - if (tmpLeft[i] <= tmpRight[j]){ - array[k] = tmpLeft[i]; - i++; - } - else{ - array[k] = tmpRight[j]; - j++; - } - k++; - } - - while(i < left){ - array[k] = tmpLeft[i]; - i++; - k++; - } - - while(j < right){ - array[k] = tmpRight[j]; - j++; - k++; - } -} - -void mergeSort(int array[], int low, int high){ - if (low < high){ - int middle = (low + high) / 2; - mergeSort(array, low, middle); - mergeSort(array , middle + 1, high); - merge(array, low, middle, high); - } - } - - -int main(){ - - int array[] = {10,7,3,9,18,-4,67,32,0,21}; - int size = sizeof array / sizeof array[0]; - int high = size - 1; - int low = 0; - printf("\n"); - printf("%s", "Original Array: \n"); - for(int i = 0; i < size; i++) { - printf("%d ",array[i]); - } - - printf("\n"); - printf("\n"); - printf("%s", "Sorted Array:\n"); - - mergeSort(array,low,high); - - for(int i = 0; i < size; i++) { - printf("%d ",array[i]); - } - - return 0; -} -