diff --git a/CMakeLists.txt b/CMakeLists.txt index 7296bda3..f393c103 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ set(CMAKE_C_STANDARD_REQUIRED ON) if(MSVC) add_compile_definitions(_CRT_SECURE_NO_WARNINGS) + add_compile_options(/Za) endif(MSVC) add_subdirectory(conversions) diff --git a/misc/demonetization.c b/misc/demonetization.c index e21f6cd4..2f155df5 100644 --- a/misc/demonetization.c +++ b/misc/demonetization.c @@ -3,13 +3,17 @@ //generate notes for an entered amount N. #include +#include -int ways(int n, int a[], int k) +int ways(int n, int *a, int k) { - if(n<0 || k<0) return 0; - if(n == 0) return 1; - if(k == 0) return 0; - return ways(n, a, k-1) + ways(n-a[k-1], a, k); + if (n < 0 || k < 0) + return 0; + if (n == 0) + return 1; + if (k == 0) + return 0; + return ways(n, a, k - 1) + ways(n - a[k - 1], a, k); } int main() @@ -20,15 +24,15 @@ int main() printf("Number of coins? "); scanf("%d", &m); - int coin[m], i; - for(i=0; i #include -void swap(char* left, char* right){ +void swap(char *left, char *right) +{ char temp = *left; *left = *right; *right = temp; } -int compare (const void * a, const void * b){ - return ( *(char*)a - *(char*)b ); +int compare(const void *a, const void *b) +{ + return (*(char *)a - *(char *)b); } -void PrintSortedPermutations(char str[]) +void PrintSortedPermutations(char *str) { int strSize = strlen(str); qsort(str, strSize, sizeof(char), compare); int largerPermFound = 1; - do{ + do + { // 1. Print permutation printf("%s\n", str); // 2. Find rightmost char that is smaller than char to its right int i; - for (i = strSize - 2; i >= 0 && str[i] >= str[i+1]; --i){} + for (i = strSize - 2; i >= 0 && str[i] >= str[i + 1]; --i) + { + } // if we couldn't find one, we're finished, else we can swap - if (i >= 0){ + if (i >= 0) + { // 3. find character at index j such that str[j] = min(str[k]) && str[k] > str[i] for all k > i - int j = i+1, k; - for(k=j; k str[i] && str[k] < str[j]) j = k; } // 3. Swap chars at i and j swap(&str[i], &str[j]); // 4. Sort string to the right of i - qsort(str+i+1, strSize-i-1, sizeof(char), compare); + qsort(str + i + 1, strSize - i - 1, sizeof(char), compare); } - else largerPermFound = 0; - } - while(largerPermFound); + else + largerPermFound = 0; + } while (largerPermFound); } -int main() { +int main() +{ int n; //size of string - scanf("%d\n",&n); - char str[n]; - scanf("%s",str); + scanf("%d\n", &n); + char *str = (char *)malloc(n * sizeof(char)); + scanf("%s", str); PrintSortedPermutations(str); + free(str); return 0; } diff --git a/misc/sudoku_solver.c b/misc/sudoku_solver.c index 30dc4ad9..5b976210 100644 --- a/misc/sudoku_solver.c +++ b/misc/sudoku_solver.c @@ -8,70 +8,86 @@ #include -const int M=144; +#define M 144 int N, R, C; -int OKrow(int a[M], int x, int y, int v) { +int OKrow(int a[M], int x, int y, int v) +{ int j; - for(j=0; j +#include +#include -int n,m; //size of the matrix +int n, m; //size of the matrix -// This function does Binary search for x in i-th row from j_low to j_high. -void binarySearch(int mat[n][m], int i, int j_low,int j_high, int x) +// This function does Binary search for x in i-th row from j_low to j_high. +void binarySearch(int **mat, int i, int j_low, int j_high, int x) { while (j_low <= j_high) { int j_mid = (j_low + j_high) / 2; - + // Element found - if (mat[i][j_mid] == x){ - printf("Found at (%d,%d)\n",i,j_mid); - return ; + if (mat[i][j_mid] == x) + { + printf("Found at (%d,%d)\n", i, j_mid); + return; } else if (mat[i][j_mid] > x) j_high = j_mid - 1; @@ -22,25 +24,27 @@ void binarySearch(int mat[n][m], int i, int j_low,int j_high, int x) // element not found printf("element not found\n"); } - + // Function to perform binary search on the mid values of row to get the desired pair of rows // where the element can be found -void modifiedBinarySearch(int mat[n][m], int n, int m, int x) -{ // If Single row matrix - if (n == 1){ - binarySearch(mat, 0, 0, m-1, x); +void modifiedBinarySearch(int **mat, int n, int m, int x) +{ // If Single row matrix + if (n == 1) + { + binarySearch(mat, 0, 0, m - 1, x); return; } - + // Do binary search in middle column. // Condition to terminate the loop when the 2 desired rows are found. - int i_low = 0, i_high = n-1, j_mid = m/2; - while ((i_low+1) < i_high) + int i_low = 0, i_high = n - 1, j_mid = m / 2; + while ((i_low + 1) < i_high) { int i_mid = (i_low + i_high) / 2; // element found - if (mat[i_mid][j_mid] == x){ - printf("Found at (%d,%d)\n",i_mid,j_mid); + if (mat[i_mid][j_mid] == x) + { + printf("Found at (%d,%d)\n", i_mid, j_mid); return; } else if (mat[i_mid][j_mid] > x) @@ -50,37 +54,46 @@ void modifiedBinarySearch(int mat[n][m], int n, int m, int x) } // If element is present on the mid of the two rows if (mat[i_low][j_mid] == x) - printf("Found at (%d,%d)\n",i_low,j_mid); - else if (mat[i_low+1][j_mid] == x) - printf("Found at (%d,%d)\n",i_low+1,j_mid); - + printf("Found at (%d,%d)\n", i_low, j_mid); + else if (mat[i_low + 1][j_mid] == x) + printf("Found at (%d,%d)\n", i_low + 1, j_mid); + // Search element on 1st half of 1st row - else if (x <= mat[i_low][j_mid-1]) - binarySearch(mat, i_low, 0, j_mid-1, x); - + else if (x <= mat[i_low][j_mid - 1]) + binarySearch(mat, i_low, 0, j_mid - 1, x); + // Search element on 2nd half of 1st row - else if (x >= mat[i_low][j_mid+1] && x <= mat[i_low][m-1]) - binarySearch(mat, i_low, j_mid+1, m-1, x); - + else if (x >= mat[i_low][j_mid + 1] && x <= mat[i_low][m - 1]) + binarySearch(mat, i_low, j_mid + 1, m - 1, x); + // Search element on 1st half of 2nd row - else if (x <= mat[i_low+1][j_mid-1]) - binarySearch(mat, i_low+1, 0, j_mid-1, x); - + else if (x <= mat[i_low + 1][j_mid - 1]) + binarySearch(mat, i_low + 1, 0, j_mid - 1, x); + // search element on 2nd half of 2nd row else - binarySearch(mat, i_low+1, j_mid+1, m-1, x); + binarySearch(mat, i_low + 1, j_mid + 1, m - 1, x); } int main() { - int x; //element to be searched - scanf("%d %d %d\n",&n,&m,&x); - int mat[n][m]; - for(int i=0; i +#include /*Displays the array, passed to this method*/ -void display(int arr[], int n){ - +void display(int *arr, int n) +{ + int i; - for(i = 0; i < n; i++){ + for (i = 0; i < n; i++) + { printf("%d ", arr[i]); } - + printf("\n"); - } /*Swap function to swap two values*/ -void swap(int *first, int *second){ - +void swap(int *first, int *second) +{ + int temp = *first; *first = *second; *second = temp; - } /*This is where the sorting of the array takes place arr[] --- Array to be sorted size --- Array Size */ -void selectionSort(int arr[], int size){ - - for(int i=0; i arr[j]) { + for (int j = i + 1; j < size; j++) + { + if (arr[min_index] > arr[j]) + { min_index = j; } } @@ -39,26 +45,28 @@ void selectionSort(int arr[], int size){ } } -int main(int argc, const char * argv[]) { +int main(int argc, const char *argv[]) +{ int n; printf("Enter size of array:\n"); scanf("%d", &n); // E.g. 8 - + printf("Enter the elements of the array\n"); int i; - int arr[n]; - for(i = 0; i < n; i++){ - scanf("%d", &arr[i] ); + int *arr = (int *)malloc(n * sizeof(int)); + for (i = 0; i < n; i++) + { + scanf("%d", &arr[i]); } - + printf("Original array: "); - display(arr, n); // Original array : 10 11 9 8 4 7 3 8 - + display(arr, n); // Original array : 10 11 9 8 4 7 3 8 + selectionSort(arr, n); - + printf("Sorted array: "); - display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 - + display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + + free(arr); return 0; } - diff --git a/sorting/binary_Insertion_Sort.c b/sorting/binary_Insertion_Sort.c index 4c3f2dd2..075bbe2b 100644 --- a/sorting/binary_Insertion_Sort.c +++ b/sorting/binary_Insertion_Sort.c @@ -2,42 +2,48 @@ * Using binary search to find the proper location for * inserting the selected item at each iteration. */ #include +#include /*Displays the array, passed to this method*/ -void display(int arr[], int n) { +void display(int *arr, int n) +{ int i; - for(i = 0; i < n; i++){ + for (i = 0; i < n; i++) + { printf("%d ", arr[i]); } printf("\n"); } -int binarySearch(int arr[], int key, int low, int high) { +int binarySearch(int *arr, int key, int low, int high) +{ if (low >= high) - return (key > arr[low]) ? (low + 1): low; + return (key > arr[low]) ? (low + 1) : low; int mid = low + (high - 1) / 2; - if(arr[mid] == key) + if (arr[mid] == key) return mid + 1; else if (arr[mid] > key) return binarySearch(arr, key, low, mid - 1); else return binarySearch(arr, key, mid + 1, high); - } /*This is where the sorting of the array takes place arr[] --- Array to be sorted size --- Array Size */ -void insertionSort(int arr[], int size) { +void insertionSort(int *arr, int size) +{ int i, j, key, index; - for(i = 0; i < size; i++) { + for (i = 0; i < size; i++) + { j = i - 1; key = arr[i]; /* Use binrary search to find exact key's index */ index = binarySearch(arr, key, 0, j); /* Move all elements greater than key from [index...j] * to one position */ - while(j >= index) { + while (j >= index) + { arr[j + 1] = arr[j]; j = j - 1; } @@ -46,16 +52,18 @@ void insertionSort(int arr[], int size) { } } -int main(int argc, const char * argv[]) { +int main(int argc, const char *argv[]) +{ int n; printf("Enter size of array:\n"); scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; - int arr[n]; - for(i = 0; i < n; i++) { - scanf("%d", &arr[i] ); + int *arr = (int *)malloc(n * sizeof(int)); + for (i = 0; i < n; i++) + { + scanf("%d", &arr[i]); } printf("Original array: "); @@ -66,6 +74,6 @@ int main(int argc, const char * argv[]) { printf("Sorted array: "); display(arr, n); + free(arr); return 0; } - diff --git a/sorting/counting_Sort.c b/sorting/counting_Sort.c index f70461b7..f1ff6dea 100644 --- a/sorting/counting_Sort.c +++ b/sorting/counting_Sort.c @@ -7,6 +7,7 @@ #include #include +#include int main() { @@ -15,26 +16,26 @@ int main() printf("Enter size of array = "); scanf("%d", &n); - int a[n]; + int *a = (int *)malloc(n * sizeof(int)); printf("Enter %d elements in array :\n", n); - for(i = 0; i < n; i++) + for (i = 0; i < n; i++) { scanf("%d", &a[i]); - if(a[i] > l) + if (a[i] > l) l = a[i]; } int b[l + 1]; memset(b, 0, (l + 1) * sizeof(b[0])); - for(i = 0; i < n; i++) + for (i = 0; i < n; i++) b[a[i]]++; //hashing number to array index - for(i = 0; i < (l + 1); i++) //unstable , stabilized by prefix sum array + for (i = 0; i < (l + 1); i++) //unstable , stabilized by prefix sum array { - if(b[i] > 0) + if (b[i] > 0) { - while(b[i] != 0) //for case when number exists more than once + while (b[i] != 0) //for case when number exists more than once { printf("%d ", i); b[i]--; @@ -42,5 +43,6 @@ int main() } } + free(a); return 0; } diff --git a/sorting/insertion_Sort.c b/sorting/insertion_Sort.c index 638915b9..05eef233 100644 --- a/sorting/insertion_Sort.c +++ b/sorting/insertion_Sort.c @@ -1,10 +1,13 @@ //sorting of array list using insertion sort #include +#include /*Displays the array, passed to this method*/ -void display(int arr[], int n) { +void display(int *arr, int n) +{ int i; - for(i = 0; i < n; i++){ + for (i = 0; i < n; i++) + { printf("%d ", arr[i]); } printf("\n"); @@ -14,13 +17,16 @@ void display(int arr[], int n) { arr[] --- Array to be sorted size --- Array Size */ -void insertionSort(int arr[], int size) { +void insertionSort(int *arr, int size) +{ int i, j, key; - for(i = 0; i < size; i++) { + for (i = 0; i < size; i++) + { j = i - 1; key = arr[i]; /* Move all elements greater than key to one position */ - while(j >= 0 && key < arr[j]) { + while (j >= 0 && key < arr[j]) + { arr[j + 1] = arr[j]; j = j - 1; } @@ -29,16 +35,18 @@ void insertionSort(int arr[], int size) { } } -int main(int argc, const char * argv[]) { +int main(int argc, const char *argv[]) +{ int n; printf("Enter size of array:\n"); scanf("%d", &n); // E.g. 8 printf("Enter the elements of the array\n"); int i; - int arr[n]; - for(i = 0; i < n; i++) { - scanf("%d", &arr[i] ); + int *arr = (int *)malloc(n * sizeof(int)); + for (i = 0; i < n; i++) + { + scanf("%d", &arr[i]); } printf("Original array: "); @@ -49,6 +57,6 @@ int main(int argc, const char * argv[]) { printf("Sorted array: "); display(arr, n); + free(arr); return 0; } - diff --git a/sorting/radix_sort_2.c b/sorting/radix_sort_2.c index 27428276..0ef5d7b8 100644 --- a/sorting/radix_sort_2.c +++ b/sorting/radix_sort_2.c @@ -1,10 +1,11 @@ //sorting of array list using Radix sort #include +#include #define range 10 // Range for integers is 10 as digits range from 0-9 // Utility function to get the maximum value in ar[] -int MAX(int ar[], int size) +int MAX(int *ar, int size) { int i, max = ar[0]; for (i = 0; i < size; i++) @@ -16,7 +17,7 @@ int MAX(int ar[], int size) } // Counting sort according to the digit represented by place -void countSort(int arr[], int n, int place) +void countSort(int *arr, int n, int place) { int i, freq[range] = {0}; int output[n]; @@ -46,7 +47,7 @@ void countSort(int arr[], int n, int place) n --- Array Size max --- Maximum element in Array */ -void radixsort(int arr[], int n, int max) //max is the maximum element in the array +void radixsort2(int *arr, int n, int max) //max is the maximum element in the array { int mul = 1; while (max) @@ -72,7 +73,7 @@ int main(int argc, const char *argv[]) printf("Enter the elements of the array\n"); int i; - int arr[n]; + int *arr = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); @@ -84,10 +85,11 @@ int main(int argc, const char *argv[]) int max; max = MAX(arr, n); - radixsort(arr, n, max); + radixsort2(arr, n, max); printf("Sorted array: "); display(arr, n); // Sorted array : 3 4 7 8 8 9 10 11 + free(arr); return 0; } diff --git a/sorting/random_quick_sort.c b/sorting/random_quick_sort.c index d4426a06..9d07736c 100644 --- a/sorting/random_quick_sort.c +++ b/sorting/random_quick_sort.c @@ -5,23 +5,23 @@ This can take time O(n*n) to sort in the worst case. Now in randomised quick sort, pivot is randomly chosen and then recursively sort the left and right sub-arrays. The expected running time of the algorithm is O(nlog(n)). */ -#include -#include -#include +#include +#include +#include -int getBig(int a[], int i, int right, int pivot) +int getBig(int *a, int i, int right, int pivot) { - for(int k = i; k <= right; k++) + for (int k = i; k <= right; k++) { if (a[k] > pivot) return k; } - return right+1; + return right + 1; } -int getSmall(int a[], int j, int left, int pivot) +int getSmall(int *a, int j, int left, int pivot) { - for(int k = j; k >= left; k--) + for (int k = j; k >= left; k--) { if (a[k] < pivot) return k; @@ -36,60 +36,62 @@ void swap(int *a, int *b) *b = t; } -void random_quick(int a[], int left, int right) +void random_quick(int *a, int left, int right) { - if (left>=right) + if (left >= right) return; - int index = left + (rand()%(right-left)), i = left, j = right; + int index = left + (rand() % (right - left)), i = left, j = right; int pivot_index = index; int pivot = a[index]; // storing index of element greater than pivot i = getBig(a, i, right, pivot); // storing index of element smaller than pivot j = getSmall(a, j, left, pivot); - while(i <= j) + while (i <= j) { swap(&a[i], &a[j]); i = getBig(a, i, right, pivot); j = getSmall(a, j, left, pivot); } // after separating the smaller and greater elements, there are 3 cases possible - if(pivot_index>j && pivot_index>i) + if (pivot_index > j && pivot_index > i) { // case 1. When the pivot element index is greater than both i and j swap(&a[i], &a[pivot_index]); - random_quick(a, left, i-1); - random_quick(a, i+1, right); + random_quick(a, left, i - 1); + random_quick(a, i + 1, right); } - else if (pivot_index +#include -void swap(int *a, int *b){ +void swap(int *a, int *b) +{ int temp; temp = *a; *a = *b; *b = temp; } -void shakersort(int a[], int n) +void shakersort(int *a, int n) { int p, i; for (p = 1; p <= n / 2; p++) { for (i = p - 1; i < n - p; i++) - if (a[i] > a[i+1]){ + if (a[i] > a[i + 1]) + { swap(&a[i], &a[i + 1]); - } + } for (i = n - p - 1; i >= p; i--) - if (a[i] < a[i-1]){ + if (a[i] < a[i - 1]) + { swap(&a[i], &a[i - 1]); - } + } } } int main() { int n; - scanf("%d",&n); - int arr[n] ,i; - for (i = 0 ; i < n; i++) + scanf("%d", &n); + int *arr = (int *)malloc(n * sizeof(int)); + int i; + for (i = 0; i < n; i++) scanf("%d ", &arr[i]); shakersort(arr, n); - for (i = 0 ; i < n; i++) + for (i = 0; i < n; i++) printf("%d ", arr[i]); + free(arr); return 0; }