From 477141816d58fa018364b718750815c02d1db194 Mon Sep 17 00:00:00 2001 From: prash98 Date: Sat, 20 Oct 2018 19:27:46 +0530 Subject: [PATCH 1/2] ADding mer-ins sort in c --- C/mer-ins.c.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ mer-ins.c.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 210 insertions(+) create mode 100644 C/mer-ins.c.c create mode 100644 mer-ins.c.c diff --git a/C/mer-ins.c.c b/C/mer-ins.c.c new file mode 100644 index 0000000..d971d4b --- /dev/null +++ b/C/mer-ins.c.c @@ -0,0 +1,105 @@ +// C program for insertion sort +#include +#include +#include + +/* Function to sort an array using insertion sort*/ +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i-1; + while (j >= 0 && arr[j] > key) + { + arr[j+1] = arr[j]; + j = j-1; + } + arr[j+1] = key; + } +} +void print(int arr[], int n) +{ + int i; + for (i=0; i < n; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +int main() +{ + int arr[] = {12, 11, 13, 5, 6,1,7,2,90,45,65,66,78,87,45,34,12,32,345,56}; + int n = sizeof(arr)/sizeof(arr[0]); + if(n<20){ + insertionSort(arr, n); + printf("insertionSort implemented\n"); + print(arr, n); + } + else{ + mergeSort(arr, 0, n - 1); + printf("mergeSort\n"); + print(arr, n); + + return 0; +}} + +void merge(int arr[], int l, int m, int r) +{ + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + + + int L[n1], R[n2]; + for (i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1+ j]; + i = 0; // Initial index of first subarray + j = 0; // Initial index of second subarray + k = l; // Initial index of merged subarray + while (i < n1 && j < n2) + { + if (L[i] <= R[j]) + { + arr[k] = L[i]; + i++; + } + else + { + arr[k] = R[j]; + j++; + } + k++; + } + while (i < n1) + { + arr[k] = L[i]; + i++; + k++; + } + while (j < n2) + { + arr[k] = R[j]; + j++; + k++; + } +} + +void mergeSort(int arr[], int l, int r) +{ + if (l < r) + { + // Same as (l+r)/2, but avoids overflow for + // large l and h + int m = l+(r-l)/2; + + // Sort first and second halves + mergeSort(arr, l, m); + mergeSort(arr, m+1, r); + + merge(arr, l, m, r); + } +} + diff --git a/mer-ins.c.c b/mer-ins.c.c new file mode 100644 index 0000000..d971d4b --- /dev/null +++ b/mer-ins.c.c @@ -0,0 +1,105 @@ +// C program for insertion sort +#include +#include +#include + +/* Function to sort an array using insertion sort*/ +void insertionSort(int arr[], int n) +{ + int i, key, j; + for (i = 1; i < n; i++) + { + key = arr[i]; + j = i-1; + while (j >= 0 && arr[j] > key) + { + arr[j+1] = arr[j]; + j = j-1; + } + arr[j+1] = key; + } +} +void print(int arr[], int n) +{ + int i; + for (i=0; i < n; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +int main() +{ + int arr[] = {12, 11, 13, 5, 6,1,7,2,90,45,65,66,78,87,45,34,12,32,345,56}; + int n = sizeof(arr)/sizeof(arr[0]); + if(n<20){ + insertionSort(arr, n); + printf("insertionSort implemented\n"); + print(arr, n); + } + else{ + mergeSort(arr, 0, n - 1); + printf("mergeSort\n"); + print(arr, n); + + return 0; +}} + +void merge(int arr[], int l, int m, int r) +{ + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + + + int L[n1], R[n2]; + for (i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1+ j]; + i = 0; // Initial index of first subarray + j = 0; // Initial index of second subarray + k = l; // Initial index of merged subarray + while (i < n1 && j < n2) + { + if (L[i] <= R[j]) + { + arr[k] = L[i]; + i++; + } + else + { + arr[k] = R[j]; + j++; + } + k++; + } + while (i < n1) + { + arr[k] = L[i]; + i++; + k++; + } + while (j < n2) + { + arr[k] = R[j]; + j++; + k++; + } +} + +void mergeSort(int arr[], int l, int r) +{ + if (l < r) + { + // Same as (l+r)/2, but avoids overflow for + // large l and h + int m = l+(r-l)/2; + + // Sort first and second halves + mergeSort(arr, l, m); + mergeSort(arr, m+1, r); + + merge(arr, l, m, r); + } +} + From 53788e013149f455dadbaf361d54daabf4901d8c Mon Sep 17 00:00:00 2001 From: prash98 Date: Sat, 20 Oct 2018 19:30:44 +0530 Subject: [PATCH 2/2] Adding B&C --- C/Bell.c.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 C/Bell.c.c diff --git a/C/Bell.c.c b/C/Bell.c.c new file mode 100644 index 0000000..b1c652a --- /dev/null +++ b/C/Bell.c.c @@ -0,0 +1,40 @@ +#include + +int main(void) { + int t; + scanf("%d",&t); + int u[100]; + int v[100]; + int w[100]; + for(int i=0;i