Posts

Showing posts with the label Algorithm

Selection Sort with step-by-step Output

Source Code #include <stdio.h> //defining the constant ASIZE as array's size #define ASIZE 10 void main() {     int i,j,key;          //var just for displaying the in-between arrays     int l;          //unsorted array     int a[ASIZE]={0,5,4,8,1,9,7,3,6,2};          // printing the unsorted array     printf("Unsorted Array: ");     for(i=0;i<ASIZE;i++)         printf("%d ",a[i]);     printf("\n\n");          //start of Selection Sort         //traversing the whole array     for(i=0;i<ASIZE;i++){                //taking position of i in a key variable         key=i;                  //gives the smaller element everytime one is found ...

Insertion Sort with Run-Time Input & step-by-step Output

Source Code #include <stdio.h> #include <string.h> //Array size defined #define SIZE 8 //Insertion sort function int ins_sort(int a[],int n){     int j,key,i,k;     printf("Array_Before_Insertion_Sort:\n");     for(i=0;i<n;i++)             printf("%d ",a[i]);         printf("\n\n");          //Sorting Algorithm     for(j=1;j<n;j++){                  key=a[j];         printf("\nKey- %d\n",key);         i=j-1;                  while(i>=0 && a[i]>key){             a[i+1]=a[i];             i=i-1;             for(k=0;k<n;k++)                 printf("%d ",a[k]...