Chapter 2.1 Sorting Techniques
Chapter 2.1 Sorting Techniques
Insertion sort
Selection sort
Sorting Algorithm
Sorting takes an unordered collection
and makes it an ordered one.
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
5 12 35 42 77 101
2
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end
using pair-wise comparisons and swapping
1 2 3 4 5 6
77 42 35 12 101 5
3
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end
using pair-wise comparisons and swapping
1 2 3 4 5 6
42Swap42
77 77 35 12 101 5
4
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end
using pair-wise comparisons and swapping
1 2 3 4 5 6
42 35Swap35
77 77 12 101 5
5
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end
using pair-wise comparisons and swapping
1 2 3 4 5 6
42 35 12 Swap 12
77 77 101 5
6
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end
using pair-wise comparisons and swapping
1 2 3 4 5 6
42 35 12 77 101 5
No need to swap
7
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end
using pair-wise comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 Swap 101
101 5
8
"Bubbling Up" the Largest Element
Traverse a collection of elements
Move from the front to the end
“Bubble” the largest value to the end
using pair-wise comparisons and swapping
1 2 3 4 5 6
42 35 12 77 5 101
9
Repeat “Bubble Up” How Many
Times?
If we have N elements…
35 12 42 5 77 101
N-1
12 35 5 42 77 101
12 5 35 42 77 101
5 12 35 42 77 101
11
Bubble Sort
BubbleSORT(A,N): it will sort the elements of an array A
with N elements in ascending order.
1. Repeat step 2 to 3 for I = 1 to n-1 do
2. Repeat step 3 for J = 1 to n-i do
3. if (A[j+1] < A[j]),then:
swap A[j] and A[j+1] .
[End of if structure]
[End of inner for loop]
[End of outer for loop]
4. EXIT
Analysis:
In general, if the list has n elements, we will have to do
(n-1) + (n-2) …. + 2 +1 = (n-1) n / 2 comparisons.
=O(n^2)
12
Insertion Sort
INSERTION_SORT (A, N):it will sort the elements of
an array A with N elements in ascending order.
f(n) = 1 + 2 + 3 + ….+ (n – 1)
= n(n – 1)/2
= O(n2)
MIN(A, K, N, LOC).
1.Set MIN := A[K] and LOC:= K.
2.Repeat for j=k+1 to N:
If Min>A[j], then: Set Min:= A[j] and LOC:=J.
[End of if structure]
3.Return.
Method 2: Selection sort (1
algo)
SELECTION(A, N)it will sort the elements of an array A with N
elements in ascending order.
1.Repeat steps 2 and 3 for k=1 to N-1:
2. Set MIN:=a[K] and Set LOC:=k
3. Repeat step 4 for j=k+1 to N:
4. If Min>A[j], then:
Set Min:= A[j] and LOC:=J.
[End of if structure]
[end of inner for loop]
5. [Interchange A[k] and A[LOC]]
Set Temp:= A[k].
A[k]:= A[LOC]
A[LOC]:=Temp.
[End of outer for Loop.]
6. EXIT.
Selection Sort Example
8 4 6 9 2 3 1 1 2 3 4 9 6 8
1 4 6 9 2 3 8 1 2 3 4 6 9 8
1 2 6 9 4 3 8 1 2 3 4 6 8 9
1 2 3 9 4 6 8 1 2 3 4 6 8 9
Selection Sort Complexity
The number f(n) of comparisons in selection sort
algorithm is independent of original order of
elements. There are n-1 comparisons during
pass 1 to find the smallest element, n-2
comparisons during pass 2 to find the second
smallest element, and so on.
Accordingly,
f (n) = (n-1)+(n-2)+-----+2+1
= n(n-1)/2
= O(n2)
The f (n) holds the same value O(n 2) both for
worst case and average case.
Comparing the Algorithms
Best Average Worst
Case Case Case
Bubble Sort O(n) O(n2) O(n2)
Insertion Sort O(n) O(n2) O(n2)
Selection Sort O(n2) O(n2) O(n2)
Thank You