This is a program of the bubble method:
# Include
Void sort (int array [], int size)
{
Int I, j, temp;
/* The following uses adjacent comparisons to place large numbers on top ;*/
For (I = 0; I
For (j = I + 1; j
If (array [I]> array [j])
{
Temp = array [I];
Array [I] = array [j];
Array [j] = temp;
}
}
Void main ()
{
Int I;
Int a [10] = {787,213,132, 21 };
Sort (a, 10 );
For (I = 0; I <10; I ++)
Printf (% 6d, a [I]);
}
This is a selection procedure:
# Include
Void sort (int array [], int size)
{
Int I, j, k, temp;
For (I = 0; I
{
K = I;
/* Find the smallest one and write it down with a [k], and then exchange it with a [I ;*/
For (j = I + 1; j
If (array [k]> array [j])
K = j;
Temp = array [k];
Array [k] = array [I];
Array [I] = temp;
}
}
Void main ()
{
Int a [] = };
Int I;
Sort (a, 10 );
For (I = 0; I <10; I ++)
Printf (% dt, a [I]);
}
This is a fast sorting program:
# Include
Void quick_sort (int array [], int first, int last) // first, last is the range of the objects under the array;
{
Int temp, low, high, list_separator;
Low = first;
High = last;
/* The following is the size of the array. Compare the number in the array with the number in the middle. Put the larger value in the second half, and put the smaller value in the first half ,*/
List_separator = array [(first + last)/2]; // intermediate number;
Do {
While (array [low]
Low ++;
While (array [high]> list_separator) // compare the intermediate number with the second half;
High --;
If (low <= high) // The first half is exchanged with the second half;
{
Temp = array [low];
Array [low ++] = array [high];
Array [high --] = temp;
}
} While (low <= high );
If (first
Quick_sort (array, first, high); // implement a loop using recursive methods;
If (low
Quick_sort (array, low, last); // implement a loop using recursive methods;
}
Void main ()
{
Int a [9] = {, 8 };
Quick_sort (a, 0, 8 );
For (int I = 0; I <9; I ++)
Printf (% dt, a [I]);
Printf (n );
}
This is a shell sorting program:
# Include
Void shell_sort (int array [], int size)
{
Int temp, gap, I, flag;
Gap = size/2;
Do {
Do {
Flag = 0;/* use tags to compare the numbers of the first half and the second half, when the number of the first half is smaller than the number of the first half corresponding to the first one, exit the while LOOP */
For (I = 0; I
If (array [I]> array [I + gap])
{
Temp = array [I];
Array [I] = array [I + gap];
Array [I + gap] = temp;
Flag = 1;
}
} While (flag );
} While (gap = gap/2 );
}
Void main ()
{
Int array [] = {,}, I;
Shell_sort (array, 15 );
For (I = 0; I <15; I ++)
Printf (% 10d, array [I]);
}