2. Sorting
Sorting methods can be divided into two types based
upon the complexity of their algorithms.
One type of sorting algorithms includes
Bubble Sort
Insertion Sort
Selection Sort
Other type consists is
Merge Sort
Choice of a method depends upon the type and size of
data to be sorted.
3. Truth in CS Act (proof is ur job)
NOBODY EVER USES BUBBLE SORT
NOBODY
NOT EVER
BECAUSE IT IS EXTREMELY
INEFFICIENT
LB
4. Insertion sort
The insertion sort works just like its name
suggests –
it inserts each item into its proper place in the
final list.
The simplest implementation of this requires
two list structures –
the source list and the list into which sorted items are
inserted.
5. Insertion sort
The insertion sort is a good middle-of-the-road choice for
sorting lists of a few thousand items or less.
Insertion sort is over twice as fast as the bubble sort and
almost 40% faster than the selection sort. The insertion
sort shouldn't be used for sorting lists larger than a
couple thousand items or repetitive sorting of lists larger
than a couple hundred items.
6. Insertion sort
Suppose an array A with n elements
A[1], A[2],……..A[N] in memory.
The insertion sort algorithm scan A form
A[1] to A[N], inserting each elements
A[K] into its proper position in the
previously sorted sub array A[1],
A[2]……….A[K-1] that is,
7. Insertion sort
Pass1: A[1] by itself is trivially sorted.
Pass 2: A[2] in inserted either before or after A[1] so
that A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper position in A[1],
A[2], so that all the three elements will be sorted.
Pass 4. A[4] is inserted into its proper place so that…..
Pass N. A[N] is inserted into its proper place so that
A[1], A[2], …………A[N] is sorted.
8. Insertion Sort cont…..
• The insertion sort algorithm sorts the list by moving
each element to its proper place
Figure 6: Array list to be sorted
Figure 7: Sorted and unsorted portions of the array list
9. Insertion Sort Algorithm (Cont’d)
Figure 8: Move list[4] into list[2]
Figure 9: Copy list[4] into temp
10. Insertion Sort Algorithm (Cont’d)
Figure 10: Array list before copying list[3] into list[4], then
list[2] into list[3]
Figure 11: Array list after copying list[3] into list[4], and then
list[2] into list[3]
12. 12
INSERTION SORT
The basic idea of Insertion Sort for
the items A (1 : n) is as follows:
A (1) is already sorted
for j2 to n do
place A(j) in its correct position in the
sorted set A(1 : j-1)
repeat.
13. Insertion sort algorithm
INSERTION ( A, N).
this algorithm sorts the array A with N elements.
1. Set A[0]:= -infinity.
2. Repeat step 3 to 5 for K=: 2 to N
3. Set temp:= A[K] and PTR:= K-1
4. Repeat while TEMP < A[PTR]
a. Set A[PTR+1]:= A[PTR] [moves elements forward]
b. Set PTR:= PTR-1
[end of loop]
5. Set A[PTR + 1]:=TEMP. [insert elements in proper
position]
[end of step 2 loop]
6. exit
14. 14
INSERTION SORT (Contd..)
Procedure Insertion sort (A, n)
A(0) -infinity // create a smallest //
// value to exit while loop //
for j2 to n do // A(1 : j-1) is sorted //
Item A(j) ;
ij-1
while item < A (i) do // 0<=i<j //
A (i+1)A(i) ; ii-1
Repeat
A (i+1)item
Repeat
End Insertion sort
15. 15
INSERTION SORT (Contd..)
Example: Sort A = ( 15, 10, 5, 6, 8 )
1, 2, 3, 4, 5
n = 5 A(0) = -
15 is already sorted
Now j2 itemA(2) = 10
10 is to be inserted in (15)
ij-1 = 1 item = 10 < A(i) = 15
so A(i+1 = 2)A(i) i1-1=0
Now item>A(1) = - so while loop is exited
and A(0+1)=A(1)item
16. 16
INSERTION SORT (Contd..)
So, the current sorted list is (10, 15)
Now j3 itemA(j) =5
5 is to be inserted in (10 15)
i2, item=5<A(1)=15 so
A(i+1)=A(3)A(i)=15, i2-1=1
item=5<A(i)=10 so A(2)10
i0, item = 5 >A(0) = -
so A(1)5
Similarly 6 is inserted at 2nd place and 8 at 3rd
place
So, final sorted list is (5, 6, 8, 10, 15)
17. Algorithm: INSERTIONSORT
Input: An array A[1..n] of n elements.
Output: A[1..n] sorted in nondecreasing
order.
1. for i 2 to n
2. x A[i]
3. j i - 1
4. while (j >0) and (A[j] > x)
5. A[j + 1] A[j]
6. j j - 1
7. end while
8. A[j + 1] x
9. end for
Example sort : 34 8 64 51 32 21
18. An Example: Insertion Sort
InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 10 40 20
1 2 3 4
i = j = key =
A[j] = A[j+1] =
19. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 10 40 20
1 2 3 4
i = 2 j = 1 key = 10
A[j] = 30 A[j+1] = 10
20. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 1 key = 10
A[j] = 30 A[j+1] = 30
21. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 1 key = 10
A[j] = 30 A[j+1] = 30
22. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 0 key = 10
A[j] = A[j+1] = 30
23. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
30 30 40 20
1 2 3 4
i = 2 j = 0 key = 10
A[j] = A[j+1] = 30
24. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 2 j = 0 key = 10
A[j] = A[j+1] = 10
25. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 10
A[j] = A[j+1] = 10
26. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 40
A[j] = A[j+1] = 10
27. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 0 key = 40
A[j] = A[j+1] = 10
28. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40
A[j] = 30 A[j+1] = 40
29. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40
A[j] = 30 A[j+1] = 40
30. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 3 j = 2 key = 40
A[j] = 30 A[j+1] = 40
31. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 40
A[j] = 30 A[j+1] = 40
32. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 40
33. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 40
34. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 20
35. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 20
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 20
36. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 40
37. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 40
38. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 3 key = 20
A[j] = 40 A[j+1] = 40
39. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 40
40. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 40 40
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 40
41. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 30
42. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 2 key = 20
A[j] = 30 A[j+1] = 30
43. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 1 key = 20
A[j] = 10 A[j+1] = 30
44. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 30 30 40
1 2 3 4
i = 4 j = 1 key = 20
A[j] = 10 A[j+1] = 30
45. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 20 30 40
1 2 3 4
i = 4 j = 1 key = 20
A[j] = 10 A[j+1] = 20
46. InsertionSort(A, n) {
for i = 2 to n {
key = A[i]
j = i - 1;
while (j > 0) and (A[j] > key) {
A[j+1] = A[j]
j = j - 1
}
A[j+1] = key
}
}
10 20 30 40
1 2 3 4
i = 4 j = 1 key = 20
A[j] = 10 A[j+1] = 20
Done!
47. comparison
Take an array of 1000 items,
compare both the bubble sort and
insertion sort,
How many comparison will be made each
sorting algorithm
How many swaps will be made each
sorting algorithm.
Write a general formula for comparison
and swaps for both the algorithm.