From cd67581499e3d3ef94379601619240b0792ad076 Mon Sep 17 00:00:00 2001 From: roshan71 <91692581+roshan71@users.noreply.github.com> Date: Thu, 6 Oct 2022 13:14:53 +0530 Subject: [PATCH 1/2] Add Sorting Algorithms in C language. Bubble Sort, Insertion Sort, Merge Sort, Quick Sort, Selection Sort and Counting Sort --- algorithms/sorting/Counting_Sort.c | 51 +++++++++++++++++ algorithms/sorting/Insertion_Sort.c | 34 +++++++++++ algorithms/sorting/Merge_Sort.c | 68 ++++++++++++++++++++++ algorithms/sorting/Quick_Sort.c | 49 ++++++++++++++++ algorithms/sorting/Selection_Sort.c | 38 +++++++++++++ algorithms/sorting/bubble_sort.c | 88 +++++++++++------------------ 6 files changed, 272 insertions(+), 56 deletions(-) create mode 100644 algorithms/sorting/Counting_Sort.c create mode 100644 algorithms/sorting/Insertion_Sort.c create mode 100644 algorithms/sorting/Merge_Sort.c create mode 100644 algorithms/sorting/Quick_Sort.c create mode 100644 algorithms/sorting/Selection_Sort.c diff --git a/algorithms/sorting/Counting_Sort.c b/algorithms/sorting/Counting_Sort.c new file mode 100644 index 00000000..bb851024 --- /dev/null +++ b/algorithms/sorting/Counting_Sort.c @@ -0,0 +1,51 @@ +// Counting sort in C programming + +#include + +void counting_sort(int array[], int size) { + int output[10]; + + int max = array[0]; + for (int i = 1; i < size; i++) { + if (array[i] > max) + max = array[i]; + } + + int count[10]; + + for (int i = 0; i <= max; ++i) { + count[i] = 0; + } + + for (int i = 0; i < size; i++) { + count[array[i]]++; + } + + for (int i = 1; i <= max; i++) { + count[i] += count[i - 1]; + } + for (int i = size - 1; i >= 0; i--) { + output[count[array[i]] - 1] = array[i]; + count[array[i]]--; + } + for (int i = 0; i < size; i++) { + array[i] = output[i]; + } +} + +void main(){ + int l; + printf("Enter Number of Element in Array: "); + scanf("%d",&l); + int array[l],i; + for(i=0;i + +void insertion_sort(int array[],int l){ + int i,j,key; + for(i=1;i=0 && array[j]>key){ + array[j+1]=array[j]; + j=j-1; + } + array[j+1]=key; + + } +} + +void main(){ + int l; + printf("Enter Number of Element in Array: "); + scanf("%d",&l); + int array[l],i; + for(i=0;i + +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; + j = 0; + k = l; + 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 merge_sort(int array[], int l, int r) +{ + if (l < r) { + int m = l + (r - l) / 2; + merge_sort(array, l, m); + merge_sort(array, m + 1, r); + merge(array, l, m, r); + } +} + + +void main(){ + int l; + printf("Enter Number of Element in Array: "); + scanf("%d",&l); + int array[l],i; + for(i=0;i + +void swap(int *a, int *b) { + int t = *a; + *a = *b; + *b = t; +} + +int partition(int array[], int low, int high) { + + int pivot = array[high]; + + int i = (low - 1); + for (int j = low; j < high; j++) { + if (array[j] <= pivot) { + i++; + swap(&array[i], &array[j]); + } + } + swap(&array[i + 1], &array[high]); + + return (i + 1); +} + +void quick_sort(int array[], int low, int high) { + if (low < high) { + int pi = partition(array, low, high); + quick_sort(array, low, pi - 1); + quick_sort(array, pi + 1, high); + } +} + +void main(){ + int l; + printf("Enter Number of Element in Array: "); + scanf("%d",&l); + int array[l],i; + for(i=0;i + + +void selection_sort(int array[], int n) +{ + int i, j, min, t; + + for (i = 0; i < n-1; i++) + { + min = i; + for (j = i+1; j < n; j++) + if (array[j] < array[min]){ + min = j;} + if(min != i){ + t=array[min]; + array[min]=array[i]; + array[i]=t; + } + } +} + + +void main(){ + int l; + printf("Enter Number of Element in Array: "); + scanf("%d",&l); + int array[l],i; + for(i=0;i - -// Swap elements -void swap(int *x, int *y) -{ - int temp = *x; - *x = *y; - *y = temp; -} - -// Implement bubble sort -void bubbleSort(int arr[], int n) -{ - int i, j; - int swapping = 0; - for (i = 0; i < n-1; i++) - { - // last i elements are already in place - for (j = 0; j < n-i-1; j++) - { - if (arr[j] > arr[j+1]) - { - swap(&arr[j], &arr[j+1]); - swapping = 1; - } - } - if(swapping = 0) //if swapping is 0 that means the array is sorted - break; // so no need to go for the next - } -} - -// Function to print elements -void printArray(int arr[], int size) -{ - int i; - for (i=0; i < size; i++) - printf("%d ", arr[i]); - printf("\n"); -} - -// Driver Code -int main() -{ - int arr[] = {46, 24, 33, 10, 2, 81, 50}; - int n = sizeof(arr)/sizeof(arr[0]); - printf("Unsorted array: \n"); - printArray(arr, n); - bubbleSort(arr, n); - printf("Sorted array: \n"); - printArray(arr, n); - return 0; -} + +#include + +void bubble_sort(int array[],int len){ + int i,j,temp; + for(i=0; iarray[j+1]){ + temp=array[j]; + array[j]=array[j+1]; + array[j+1]=temp; + } + } + } +} + +void main(){ + int l; + printf("Enter Number of Element in Array: "); + scanf("%d",&l); + int array[l],i; + for(i=0;i Date: Thu, 6 Oct 2022 13:19:57 +0530 Subject: [PATCH 2/2] Rename bubble_sort.c to Bubble_Sort.c Change file name --- algorithms/sorting/{bubble_sort.c => Bubble_Sort.c} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename algorithms/sorting/{bubble_sort.c => Bubble_Sort.c} (95%) diff --git a/algorithms/sorting/bubble_sort.c b/algorithms/sorting/Bubble_Sort.c similarity index 95% rename from algorithms/sorting/bubble_sort.c rename to algorithms/sorting/Bubble_Sort.c index 63420bbe..34d11d20 100644 --- a/algorithms/sorting/bubble_sort.c +++ b/algorithms/sorting/Bubble_Sort.c @@ -29,4 +29,4 @@ void main(){ for(i=0;i