Internal Sorting Methods
Internal Sorting Methods
Internal Sorting Methods
"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
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 Algorithm
We assume the following declarations:
const max = 10;
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 Algorithm.
We assume the following declarations:
const max = 10;