0% found this document useful (0 votes)
11 views2 pages

12 - Selection & Insertion & Bubble Sort

The document contains code for three sorting algorithms: selection sort, bubble sort, and insertion sort. Each algorithm is defined in its own function that accepts an integer array and size as parameters and sorts the array in place.

Uploaded by

Youssef Ebrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

12 - Selection & Insertion & Bubble Sort

The document contains code for three sorting algorithms: selection sort, bubble sort, and insertion sort. Each algorithm is defined in its own function that accepts an integer array and size as parameters and sorts the array in place.

Uploaded by

Youssef Ebrahim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 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