diff --git a/C/Bankersalgo.c b/C/Bankersalgo.c new file mode 100644 index 0000000..bbb87e8 --- /dev/null +++ b/C/Bankersalgo.c @@ -0,0 +1,91 @@ +#include +#include + +int main(){ + + int available[4]; + int max[5][4]; + int allocation[5][4]; + int need[5][4]; + bool finish[5]; + int p; + int work[4]; + printf("Enter the number of process required"); + scanf("%d",&p); + for(int i = 0;i + +int main(void) { + int t; + scanf("%d",&t); + int u[100]; + int v[100]; + int w[100]; + for(int i=0;i +int main(){ + int n,fact=1; + printf("Enter the no\n"); + scanf("%d",&n); + for(int i=1;i<=n;i++){ + fact = fact * i; + } + printf("%d",fact); + return 0; +} \ No newline at end of file diff --git a/C/maxSub.c.c b/C/maxSub.c.c new file mode 100644 index 0000000..f6e8fd0 --- /dev/null +++ b/C/maxSub.c.c @@ -0,0 +1,59 @@ +#include +int MaxSubArraySum(int *,int,int); +int submidsum(int,int,int,int *); +int max(int,int,int); +int main(void) { + int n; + scanf("%d",&n); + int arr[100]; + for(int i=0;il-1;i--){ + temp = temp + arr[i]; + if(temp>left_sum){ + left_sum = temp; + } + } + int right_sum = arr[m+1]; + for(int j=m+1;jright_sum){ + right_sum=temp1; + } + } + int sub_sum = left_sum + right_sum; + return sub_sum; +} +int max(int a, int b, int c){ + int d[3] = {a,b,c}; + d[0]=a; + d[1]=b; + d[2]=c; + int max=d[0]; + for(int i=1;i<3;i++){ + if(max +#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); + } +} +