void bubble_sort(list_type list,
int n)
{
int j,k;
boolean exchange_made = TRUE;
element temp;
k=0;
// Make up to n-1 passes through
array exit early
//if no exchanges were made
while ((k <n-1) &&
exchange_made)
{
exchange_made = FALSE;
++k;
// Number of comparisons on kth
pass
for (j=0; j < n-k; ++j)
if (list[j] > list[j+1])
{
// Exchange must be made
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
exchange_made = TRUE;
}
}
}
void
selection_sort(list_type
list, int n)
{
int index;
element temp;
// Make n -1
passes through
successively
smaller
// segments
for (int j=0;
j<n-1; ++j)
{
index = j;
// Find index
of the smallest
element
for (int k =
j+1; k < n;
++k)
if (list[k]
< list[index])
index = k;
if (index !=j)
{
// Exchange
must be made
temp =
list[j];
list[j] =
list[j+1];
list[j+1] =
temp;
exchange_made =
TRUE;
}
}
}
void insertion_sort(list_type list,
int n)
{
int i,j;
element item_to_insert;
boolean still_looking;
// On the kth pass, insert item k
into its correct position among
// the first k entries into
the array.
for (k = 1; k<n; ++k)
{
// Walk backwards through list
looking for slot to insert list[k]
item_to_insert = list[k];
j=k-1;
still_looking = TRUE;
while ((j >=0) &&
still_looking )
if (item_to_insert < list[j])
{
list[j+1] = list[j];
--j;
}
else
still_looking = FALSE;
// Upon leaving loop, j+1 is
the index
// where item_to_insert
belongs
list [j+1] = item_to_insert;
}
}