Sorting
Sorting
Sorting
23 May, 2024 3
Insertion Sort
Remove one card at a time from the table, and
insert it into the correct position in the left hand.
23 May, 2024 4
Insertion Sort
Pass 1: 77 by itself is trivially sorted
33 44 11
23 May, 2024 5
Insertion Sort
Pass 2: 33 is inserted either before or
after 77 so that: 33, 77 is sorted.
44 11
23 May, 2024 6
Insertion Sort
Pass 3: 44 is inserted into its proper place in
33, 77, that is , before 33, between 33 and
77, or after 77, so that: 33, 44, 77 is sorted.
11
23 May, 2024 7
Insertion Sort
Pass 4: 11 is inserted into its
proper place in 33,44,77. so that:
11, 33, 44, 77 is sorted.
77
23 May, 2024 8
Insertion Sort
Pass 5: 88 is inserted into its
proper place in 11,33,44,77. so
that: 11, 33, 44, 77, 88 is sorted.
77 88
23 May, 2024 9
Insertion Sort
Pass 6: 22 is inserted into its proper
place in 11,33,44,77,88. so that: 11,
22, 33, 44, 77, 88 is sorted.
44 77
23 May, 2024 10
Insertion Sort
Pass 7: 66is inserted into its proper
place in 11, 22, 33,44,77,88. so that:
11, 22, 33, 44, 66, 77, 88 is sorted.
44 66
23 May, 2024 11
Insertion Sort
Pass 8: 55 is inserted into its proper place in 11,
22, 33,44,66,77,88. so that: 11, 22, 33, 44, 55, 66,
77, 88 is sorted.
44 55
23 May, 2024 12
Insertion Sort
Suppose an array A with n elements
A[1], A[2],……,A [N] is in memory. The insertion
sort algorithm scans A from A[1] to A[N], inserting
each element A[K] into its proper position in the
previously sorted subarray A[1], A[2],…..A[K-1].
That is:
Pass 1: A[1] by itself is trivially sorted.
Pass 2: A[2] is inserted either before or after A[1] so that: A[1], A[2] is sorted.
Pass 3: A[3] is inserted into its proper place in A[1], A[2], that is , before A[1],
between A[1] and A[2], or after A[2], so that: A[1], A[2], A[3] is sorted.
Pass 4: A[4] is inserted into its proper place in A[1], A[2],A[3] so that:
…………….
K=2 -α 77 33 44 11 88 22 66 55
K=3 -α 33 77 44 11 88 22 66 55
K=4 -α 33 44 77 11 88 22 66 55
K=5 -α 11 33 44 77 88 22 66 55
K=6 -α 11 33 44 77 88 22 66 55
K=7 -α 11 22 33 44 77 88 66 55
K=8 -α 11 22 33 44 66 77 88 55
Sorted -α 11 22 33 44 55 66 77 88
Algorithm 9.1
(Insertion sort) INSERTION (A,N)
1. Set A[0]:=-α
2. Repeat Steps 3 to 5 for K=2,3,…,N:
3. Set TEMP:=A[K] and PTR:=K-1.
4. Repeat while TEMP<A[PTR]:
(a) Set A[PTR+1]:=A[PTR]: [Move element forward.]
(b). Set PTR:=PTR-1.
[End of loop]
5. Set A[PTR+1]:=TEMP. [Insert element in proper place]
[End of step 2 loop]
6. Return.
Selection Sort
• The selection sort is a combination of searching
and sorting.
• In selection sort, sorting is done after selecting a
particular smallest or largest element from an array
and shifted it to a particular location in an array.
• During each pass, the unsorted element with the
smallest (or largest) value is moved to its proper
position in the array.
Selection Sort
Suppose an array A with n elements A[1], A[2],……, A[N] is
in memory. The selection sort algorithm for sorting A works as
follows.
First find the smallest element in the list and put it in
the first position.
Then find the smallest element in the list and put it in
the second position. And so on. More precisely:
Selection Sort
min = out
1
3
Selection Sort Example
35 65 30 60 20 scan 0-4, smallest 20
swap 35 and 20
20 65 30 60 35 scan 1-4, smallest 30
swap 65 and 30
20 30 65 60 35 scan 2-4, smallest 35
swap 65 and 35
20 30 35 60 65 scan 3-4, smallest 60
swap 60 and 60
20 30 35 60 65 done
Array 40 50 60 10 30 20
1-pass 10 50 60 40 30 20
2-pass 10 20 60 40 30 50
3-pass 10 20 30 40 60 50
4-pass 10 20 30 40 60 50
5-pass 10 20 30 40 50 60
K=1, LOC=4 77 33 44 11 88 22 66 55
K=2, LOC=6 11 33 44 77 88 22 66 55
K=3, LOC=6 11 22 44 77 88 33 66 55
K=4, LOC=6 11 22 33 77 88 44 66 55
K=5, LOC=8 11 22 33 44 88 77 66 55
K=6, LOC=7 11 22 33 44 55 77 66 88
K=7, LOC=7 11 22 33 44 55 66 77 88
Sorted 11 22 33 44 55 66 77 88
Procedure 9.2
MIN (A,K,N,LOC)
An array A is in memory. This procedure finds the
location LOC of the smallest element among A[K],
A[K+1],…..A[N].
28
Quicksort
A[p…q] ≤ A[q+1…r]
• Conquer
• Recursively sort A[p..q] and A[q+1..r] using Quicksort
• Combine
• Trivial: the arrays are sorted in place
• No additional work is required to combine them
• The entire array is now sorted
29
Apply Quick Sort Algorithm
32 99 29 98 67 45 33 22 47
Example-1
44 33 11 55 77 90 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot
44 33 11 55 77 90 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot
44 33 11 55 77 90 99 22 88 66
Find Smaller Value from Right Side so that Item < Pivot
44 33 11 55 77 90 99 22 88 66
Interchange 44 and 22 to obtain the list
22 33 11 55 77 90 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot
22 33 11 55 77 90 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot
22 33 11 55 77 90 99 44 88 66
Find Greater Value from Left Side so that Item> Pivot
22 33 11 55 77 90 99 44 88 66
Interchange 44 and 55 to obtain the list
22 33 11 44 77 90 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot
22 33 11 44 77 90 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot
22 33 11 44 77 90 99 55 88 66
Find Smaller Value from Right Side so that Item < Pivot
22 33 11 44 77 90 99 55 88 66
Interchange 44 and 40 to obtain the list
22 33 11 40 77 90 99 55 88 66
Find Greater Value from Left Side so that Item> Pivot
22 33 11 40 77 90 99 55 88 66
Interchange 44 and 77 to obtain the list
22 33 11 40 44 90 99 55 88 66
44
22 33 11 40 90 99 55 88 66
22 33 11 40
Example-2
32 99 29 98 67 45 33 22 47
32 99 29 98 67 45 33 22 47
45
32 99 29 98 67 33 22 47
Find Greater Value from Left Side so that Item>
Pivot
32 45
99 29 98 67 33 22 47
Find Greater Value from Left Side so that Item>
Pivot
45
32 99 29 98 67 33 22 47
Find Greater Value from Left Side so that Item>
Pivot
99 45
32 29 98 67 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot
99 45
32 29 98 67 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot
99 45 47
32 29 98 67 33 22
Find Smaller Value from Right Side so that Item
< Pivot
99 45
32 29 98 67 33 22 47
Find Smaller Value from Right Side so that Item
< Pivot
99 45 22
32 29 98 67 33 47
Exchange
Occurred
45
22 99
32 29 98 67 33 47
Exchange
Occurred
And
Index Changed
45
32 22 29 98 67 33 99 47
Find Greater Value from Left Side so that Item>
Pivot
29 45
32 22 98 67 33 99 47
Find Greater Value from Left Side so that Item>
Pivot
45
32 22 29 98 67 33 99 47
Find Greater Value from Left Side so that Item>
Pivot
98 45
32 22 29 67 33 99 47
Find Smaller Value from Right Side so that Item
< Pivot
98 45 33
32 22 29 67 99 47
Exchange
Occurred
98 45 33
32 22 29 67 99 47
Exchange
Occurred
45
33 98
32 22 29 67 99 47
Exchange
Occurred
45
32 22 29 33 67 98 99 47
Exchange
Occurred
And
Index Changed
45
32 22 29 33 67 98 99 47
Find Greater Value from Left Side so that Item>
Pivot
45
32 22 29 33 67 98 99 47
Find Greater Value from Left Side so that Item>
Pivot
67 45
32 22 29 33 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot
67 45
32 22 29 33 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot
67 45
32 22 29 33 98 99 47
Find Smaller Value from Right Side so that Item
< Pivot
67 45
32 22 29 33 98 99 47
Exchange
Occurred
45 67
32 22 29 33 98 99 47
Exchange
Occurred
45
67
32 22 29 33 98 99 47
Left Side< 45 < Right
Side
45 67 98 99 47
32 22 29 33
Left Side< 45 < Right
Side
67 98 99 47
45
32 22 29 33
Apply Recursive Partitioning on
Left Side
32 22 29 33
Apply Recursive Partitioning on Right
Side
67 98 99 47
Merge sort
• Merge sort algorithm is one of two important divide-and-
conquer sorting algorithms (the other one is quicksort).
• It is a recursive algorithm.
• Divides the list into two equal parts,
• Sort each part separately, and
• Then merge the sorted halves into one sorted array.
80
Merge Sort
Original 24 13 26 1 12 27 38 15
Divide in 2 24 13 26 1 12 27 38 15
Divide in 4 24 13 26 1 12 27 38 15
Divide in 8 24 13 26 1 12 27 38 15
Merge 2 13 24 1 26 12 27 15 38
Merge 4 1 13 24 26 12 15 27 38
Merge 8 1 12 13 15 24 26 27 38
81
Execution Example
• Partition
7 2 9 43 8 6 1
82
Execution Example (cont.)
7 29 4
83
Execution Example (cont.)
7 29 4
72
84
Execution Example (cont.)
7 29 4
72
7→7
85
Execution Example (cont.)
7 29 4
72
7→7 2→2
86
Execution Example (cont.)
• Merge
7 2 9 43 8 6 1
7 29 4
72→2 7
7→7 2→2
87
Execution Example (cont.)
7 29 4
72→2 7 9 4 → 4 9
88
Execution Example (cont.)
• Merge
7 2 9 43 8 6 1
7 29 4→ 2 4 7 9
72→2 7 9 4 → 4 9
89
Execution Example (cont.)
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
90
Execution Example (cont.)
• Merge
7 2 9 43 8 6 1 → 1 2 3 4 6 7 8 9
7 29 4→ 2 4 7 9 3 8 6 1 → 1 3 6 8
72→2 7 9 4 → 4 9 3 8 → 3 8 6 1 → 1 6
91