sorting in data structure

 

Num[i] = num[loc];

Num[loc]= temp;

}

}

}

 

SELECTION SORT (Program)

/*selection sort*/

#include<stdio.h>

#include<conio.h>

Void main()

{

Int num[6],int i,j,loc,min,temp;

/*Accept numbers in the array */

Clrscr();

Printf(“enter the numbers …\n\n”);

For(i = 0; i <6; i++ )

{

Scanf(“%d”, &num[i]);

}

/*sort the number*/

For (i= 0; i=<5; i++)

{

Min =num[i];

Loc = I;

For(j=i+1; j<=5; j++)

{

If(num[j]<min)

{ min = num[j];

Loc = j;

}

}

If(loc! = i)

{

Temp = num[i];

Num[i] = num[loc];

Num[loc] = temp;

}

}

/*display the sorted numbers from  the array */

Printf (“\n the sorted numbers are  : \n\n”);

For(i=0;i<6;i++)

{

Prontf(“%d\n”,num[i]);

}

Getch();

}

 

 

Insertion sort (Algorithm)

 

GIVEN  : An array NUM of N elements

Objective : arrange the elements in ascending  order

INSERTION _SORT(NUM, N)

Where

NUM : THE ARRAY OF ELEMENTS

N      : The number of elements

STEP1 : REPEAT STEP2 THRU STEP3 FOR I = 0,1,2,….N-1

STEP2 :Store the current element

TEMP = NUM [i]

J = i+1

STEP3 : Compare all the elements in the  sublist

WHILE TEMP<NUM[J] AND  J>=0

DO

NUM[J+1] = NUM [J]

J = J-1

DONE

NUM [J+1] = TEMP

STEP4 END

INSERTION SORT(FUNCTION)

VOID insertion_sort(int num [], int n)

{

Int I, temp;

For(I = 0; i<6; i++)

{

Temp = num[i];

J = i-1;

While(temp<num[j]&& j>=0)

{

Num[j+1] = num[j];

J = j-1;

}

Num[j+1] = temp;

}

}