Write a Modular C Programming code for CP Sorting Algorithm DSA
This a special type of sorting which deals with dividing according to need and merging it !!!
CODE:
#include<stdio.h> void printArray(int a[], int n) { for (int i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); } void combine(int A[], int mid, int low, int high) { int i, j, k, B[1000]; i = low; j = mid + 1; k = low; while (i <= mid && j <= high) { if (A[i] < A[j]) B[k++] = A[i++]; else B[k++] = A[j++]; } while (i <= mid) B[k++] = A[i++]; while (j <= high) B[k++] = A[j++]; for (int i = low; i <= high; i++) A[i] = B[i]; } void chinmay(int a[], int ub, int high) { int mid=ub,i=ub+1; ub=ub+1; if(ub<high) { while(a[i]<=a[i+1]) ub=++i; combine(a, mid, 0, ub); chinmay(a,ub,high); } } void specialSort(int a[], int low, int high){ int mid=0,i=0,ub=1; if(ub<high) { while(a[i]<=a[i+1]) mid=++i; i++; while(a[i]<=a[i+1]) ub=++i; if(ub<high) { combine(a, mid, low, ub); chinmay(a,ub,high); } else return ; } } int main() { int a[1000],n; printf("Enter the size: "); scanf("%d",&n); printf("Enter Numbers: "); for(int i=0;i<n;i++) a[i]=(rand()%10); printArray(a, n); specialSort(a, 0, n); printf("Sorted array is: "); printArray(a, n); }
OUTPUT
Enter the size: 15 Enter Numbers: 1 7 4 0 9 4 8 8 2 4 5 5 1 7 1 Sorted array is: 0 1 1 1 2 4 4 4 5 5 7 7 8 8 9
Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:
Top 100+ C Programming codes – KLE Technological University