12 - Selection & Insertion & Bubble Sort

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 2

void selection_sort(int *arr , int size)

{
int a , b , minloc , temp ;
for(a = 0 ; a <= size - 2 ; a++)
{
minloc = a ;
for(b = a+1 ; b <= size-1 ; b++)
if(arr[b] < arr[minloc]) minloc = b ;
temp = arr[a] ; arr[a] = arr[minloc] ; arr[minloc] = temp ;
}
}

void bubble_sort(int *arr , int size)


{
int a , b , temp , flag ;
b=2;
do{
flag = 0 ;
for(a = 0 ; a <= size - b ; a++)
{
if(arr[a] > arr[a+1])
{ temp = arr[a] ; arr[a] = arr[a+1] ; arr[a+1] = temp ; flag++;
}
}
b++;
}while(flag > 0);
}

void insertion_sort(int *arr , int size)


{
int a , b , temp ;
for( a = 1 ; a <= size -1 ; a++)
{
temp = arr[a];
for(b = a-1 ; b >= 0 ; b--)
if(temp < arr[b]) arr[b+1] = arr[b];
else break;
arr[b+1] = temp;
}
}

You might also like