diff --git a/c/Sorting/Bubble_Sort.c b/c/Sorting/Bubble_Sort.c new file mode 100644 index 00000000..0c0c02ab --- /dev/null +++ b/c/Sorting/Bubble_Sort.c @@ -0,0 +1,47 @@ +/* Bubble Sort + * Time Complexity:- + * Best || Average || Worst + * O(N) O(N^2) O(N^2) + * Auxiliary Space:- O(1) + * Sorting In Place:- Yes + * Stable:- Yes +*/ + +#include + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void bubbleSort(int arr[], int n) +{ +int i, j; +for (i = 0; i < n-1; i++) + + for (j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + swap(&arr[j], &arr[j+1]); +} + + +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +int main() +{ + int arr[] = {64, 34, 25, 12, 22, 11, 90}; + int n = sizeof(arr)/sizeof(arr[0]); + bubbleSort(arr, n); + printf("Sorted array: \n"); + printArray(arr, n); + return 0; +} + diff --git a/c/Sorting/Heap_Sort.c b/c/Sorting/Heap_Sort.c new file mode 100644 index 00000000..80cc835a --- /dev/null +++ b/c/Sorting/Heap_Sort.c @@ -0,0 +1,64 @@ +/* Heap Sort + * Time Complexity:- + * Best || Average || Worst + * Ω(N log N) Θ(N log N) O(N log N) + * Auxilary Space:- O(1) + * In-Place:-Yes + * Stable:- No but can be Made +*/ + + +#include +int temp; + +void heapify(int arr[], int size, int i) +{ +int largest = i; +int left = 2*i + 1; +int right = 2*i + 2; + +if (left < size && arr[left] >arr[largest]) +largest = left; + +if (right < size && arr[right] > arr[largest]) +largest = right; + +if (largest != i) +{ +temp = arr[i]; + arr[i]= arr[largest]; + arr[largest] = temp; +heapify(arr, size, largest); +} +} + +void heapSort(int arr[], int size) +{ +int i; +for (i = size / 2 - 1; i >= 0; i--) +heapify(arr, size, i); + for (i=size-1; i>=0; i--){ + temp = arr[0]; + arr[0]= arr[i]; + arr[i] = temp; + heapify(arr, i, 0); + } +} + +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +void main() +{ +int arr[] = {1, 10, 2, 3, 4, 1, 2, 100,23, 2}; +int size = sizeof(arr)/sizeof(arr[0]); +heapSort(arr, size); +printf("Sorted Array is:-\n"); +printArray(arr,size); + +} diff --git a/c/Sorting/Insertion_Sort.c b/c/Sorting/Insertion_Sort.c new file mode 100644 index 00000000..ab75f179 --- /dev/null +++ b/c/Sorting/Insertion_Sort.c @@ -0,0 +1,51 @@ +/* Insertion Sort + * Time Complexity:- + * Best || Average || Worst + * O(N) O(N^2) O(N^2) + * Auxiliary Space: O(1) + * Sorting In Place: Yes + * Stable: Yes +*/ + + +#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; + + /* 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 printArray(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 }; + int n = sizeof(arr) / sizeof(arr[0]); + + insertionSort(arr, n); + printArray(arr, n); + + return 0; +} + diff --git a/c/Sorting/Merge_Sort.c b/c/Sorting/Merge_Sort.c new file mode 100644 index 00000000..0726fbb4 --- /dev/null +++ b/c/Sorting/Merge_Sort.c @@ -0,0 +1,106 @@ +/* MergeSort + * Time Complexity:- + * Best || Average || Worst + * O(N logN) O(N log N) O(N log N) + * Auxiliary Space: O(n) + * Algorithmic Paradigm: Divide and Conquer + * Sorting In Place: No in a typical implementation +*/ + +#include +#include + +// Merges two subarrays of arr[]. +// First subarray is arr[l..m] +// Second subarray is arr[m+1..r] +void merge(int arr[], int l, int m, int r) +{ + int i, j, k; + int n1 = m - l + 1; + int n2 = r - m; + + /* create temp arrays */ + int L[n1], R[n2]; + + /* Copy data to temp arrays L[] and R[] */ + for (i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (j = 0; j < n2; j++) + R[j] = arr[m + 1 + j]; + + /* Merge the temp arrays back into arr[l..r]*/ + 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++; + } + + /* Copy the remaining elements of L[], if there + are any */ + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + /* Copy the remaining elements of R[], if there + are any */ + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } +} + +/* l is for left index and r is right index of the +sub-array of arr to be sorted */ +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); + } +} + +/* UTILITY FUNCTIONS */ +/* Function to print an array */ +void printArray(int A[], int size) +{ + int i; + for (i = 0; i < size; i++) + printf("%d ", A[i]); + printf("\n"); +} + +/* Driver code */ +int main() +{ + int arr[] = { 12, 11, 13, 5, 6, 7 }; + int arr_size = sizeof(arr) / sizeof(arr[0]); + + printf("Given array is \n"); + printArray(arr, arr_size); + + mergeSort(arr, 0, arr_size - 1); + + printf("\nSorted array is \n"); + printArray(arr, arr_size); + return 0; +} + diff --git a/c/Sorting/Quick_Sort.c b/c/Sorting/Quick_Sort.c new file mode 100644 index 00000000..78b606b6 --- /dev/null +++ b/c/Sorting/Quick_Sort.c @@ -0,0 +1,76 @@ +/* QuickSort + * Time Complexity + * Best || Average || Worst + * O(N) O(N^logN) O(N^2) + * Auxiliary Space:- O(LogN) + * In Place :-As per the broad definition of in-place algorithm it qualifies as an in-place sorting + * algorithm as it uses extra space only for storing recursive function calls but not for manipulating the input. + * Stable:-No +*/ + +# include + +// to swap two numbers +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + +int partition (int arr[], int low, int high) +{ + int pivot = arr[high]; // selecting last element as pivot + int i = (low - 1); // index of smaller element + + for (int j = low; j <= high- 1; j++) + { + // If the current element is smaller than or equal to pivot + if (arr[j] <= pivot) + { + i++; // increment index of smaller element + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} +/* + a[] is the array, p is starting index, that is 0, + and r is the last index of array. +*/ +void quicksort(int a[], int p, int r) +{ + if(p < r) + { + int q; + q = partition(a, p, r); + quicksort(a, p, q-1); + quicksort(a, q+1, r); + } +} + + +// function to print the array +void printArray(int a[], int size) +{ + int i; + for (i=0; i < size; i++) + { + printf("%d ", a[i]); + } + printf("\n"); +} + +int main() +{ + int arr[] = {9, 7, 5, 11, 12, 2, 14, 3, 10, 6}; + int n = sizeof(arr)/sizeof(arr[0]); + + // call quickSort function + quicksort(arr, 0, n-1); + + printf("Sorted array: \n"); + printArray(arr, n); + return 0; +} diff --git a/c/Sorting/Selection_Sort.c b/c/Sorting/Selection_Sort.c new file mode 100644 index 00000000..236f6ec7 --- /dev/null +++ b/c/Sorting/Selection_Sort.c @@ -0,0 +1,54 @@ +/* Selection Sort + * Time Complexity:- O(n^2) + * Auxiliary Space:- O(1) + * In Place :- Yes + * Stable:- No + */ + +#include + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +void selectionSort(int arr[], int n) +{ + int i, j, min_idx; + + // 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 + swap(&arr[min_idx], &arr[i]); + } +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +// Driver program to test above functions +int main() +{ + int arr[] = {64, 25, 12, 22, 11}; + int n = sizeof(arr)/sizeof(arr[0]); + selectionSort(arr, n); + printf("Sorted array: \n"); + printArray(arr, n); + return 0; +} + diff --git a/c/Sorting/bubbleSort_optimized.c b/c/Sorting/bubbleSort_optimized.c new file mode 100644 index 00000000..20029d55 --- /dev/null +++ b/c/Sorting/bubbleSort_optimized.c @@ -0,0 +1,46 @@ +#include + +void bubbleSort(int arr[],int size) +{ + for(int i=0;iarr[j+1]) + { + //swapping with adjacent element + int temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + }if(flag==0) + //i.e. no swapping in entire iteration,implies the array is sorted + break; + } + + //print the output + for(int i=0;i