You are on page 1of 6

Chapter 3.

Internal sorting methods (using tables)

"Sorting" means to arrange a sequence of data items so that the values of their key-fields
form a proper sequence.

1. Bubble sort works by repeatedly stepping through the list to be sorted, comparing each
pair of adjacent items and swapping them if they are in the wrong order. The pass through
the list is repeated until no swaps are needed, which indicates that the list is sorted. The
algorithm gets its name from the way smaller elements bubble to the top of the list.
Because it only uses comparisons to operate on elements, it is a comparison sort.

-Observe that after one pass, the smallest element will be in place. If we repeat this process
N-1 times then the whole array will be sorted! See the example below:

1 2 3 4 5 6 7 8 9 10
11 76 4 14 40 9 66 61 5 12

1 2 3 4 5 6 7 8 9 10
4 11 76 5 14 40 9 66 61 12

1 2 3 4 5 6 7 8 9 10
4 11 76 5 14 40 9 66 61 12

1 2 3 4 5 6 7 8 9 10
4 5 11 76 9 14 40 12 66 61

1 2 3 4 5 6 7 8 9 10
4 5 9 11 76 12 14 40 61 66
2 3 4 5 6 7 8 9 10
1
4 5 9 11 12 76 14 40 61 66
2 3 4 5 6 7 8 9 10
1
4 5 9 11 12 14 76 40 61 66

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 40 76 61 66

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 40 61 76 66

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 40 61 66 76

The complete table is sorted after N-1 passes


The Algorithm
We assume the following declarations:
const max = 10;

type table = array[1..max] of integer;

We need a procedure to swap the contents of two variables:


procedure swap(var x, y: integer);
var temp: integer;
begin
temp := x;
x := y;
y := temp;
end;

The procedure to sort a table using the bubble sort strategy:


procedure bubbleSort(var t: table);
var pass, index: integer;
begin
for pass := 1 to max 1 do
for index := max downto pass + 1 do
if T[index] < T[index 1]
then Swap(T[index], T[index 1]);
end;

2. Insertion sort (a.k.a Linear insertion sort)

Sorts by repeatedly taking the next item and inserting it into the final data structure in
its proper order with respect to items already inserted.

An example
1 2 3 4 5 6 7 8 9 10
11 76 4 14 40 9 66 61 5 12

1 2 3 4 5 6 7 8 9 10
11 76 4 14 40 9 66 61 5 12
1 2 3 4 5 6 7 8 9 10
4 11 76 14 40 9 66 61 5 12

1 2 3 4 5 6 7 8 9 10
4 11 14 76 40 9 66 61 5 12

1 2 3 4 5 6 7 8 9 10
4 11 14 40 76 9 66 61 5 12

1 2 3 4 5 6 7 8 9 10
4 9 11 14 40 76 66 61 5 12

1 2 3 4 5 6 7 8 9 10
4 9 11 14 40 66 76 61 5 12

1 2 3 4 5 6 7 8 9 10
4 9 11 14 40 61 66 76 5 12

1 2 3 4 5 6 7 8 9 10
4 5 9 11 14 40 61 66 76 12

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 40 61 66 76

The complete table is sorted after N-1 passes

The Algorithm
We assume the following declarations:
const max = 10;

type table = array[1..max] of integer;


We need a procedure to insert one element into a list of already sorted elements:
{ the procedure insert puts element T[index] in its correct }
{ place between elements T[1] and T[index 1] }
procedure insert(var t: table; index: integer);
begin
while (index > 1) and (t[index] < t[index-1]) do
begin
swap(T[index], T[index-1]);
index := index 1;
end;
end;

The procedure to sort a table using the insertion sort strategy:


procedure insertionSort(var t: Table);
var pass: integer;
begin
for pass := 2 to max do
insert(t, pass);
end;

3. Selection Sort

The algorithm works as follows: Find the minimum value in the list
Swap it with the value in the first position
Repeat the steps above for the remainder of the list (starting at
the second position and advancing each time).
Example

1 2 3 4 5 6 7 8 9 10
11 76 4 14 40 9 66 61 5 12

1 2 3 4 5 6 7 8 9 10
4 76 11 14 40 9 66 61 5 12
1 2 3 4 5 6 7 8 9 10
4 5 11 14 40 9 66 61 76 12

1 2 3 4 5 6 7 8 9 10
4 5 9 14 40 11 66 61 76 12

1 2 3 4 5 6 7 8 9 10
4 5 9 11 40 14 66 61 76 12

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 66 61 76 40

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 66 61 76 40

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 40 61 76 66

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 40 61 76 66

1 2 3 4 5 6 7 8 9 10
4 5 9 11 12 14 40 61 66 76

The complete table is sorted after N-1 passes

The Algorithm.
We assume the following declarations:
const max = 10;

type table = array[1..max] of integer;


We need a function that finds the smallest element in a part of the table:
{ the function findSmallest returns the index of the }
{ smallest element to be found in cells T[index] .. T[max]}
function findSmallest(t: Table; index: integer): integer;
var currentSmall, x: integer;
begin
currentSmall := index;
for x := index + 1 to max do
if t[x] < t[currentSmall]
then currentSmall := x;
findSmallest := currentSmall;
end;

The procedure to sort a table using the selection sort strategy:


procedure selectionSort(var t: Table);
var pass, index: integer;
begin
for pass := 1 to max - 1 do begin
index := findSmallest(t, pass);
swap(t[pass], t[index]);
end;
end;

You might also like