DS Lecture Week 4 Sorting
DS Lecture Week 4 Sorting
Sorting
Sorting
Sorting
Bubble sort
Selection Sort
Insertion Sort
Merge Sort
2
Selection Sort
Selection sort is a simple sorting algorithm.
The list is divided into two parts,
The sorted part at the left end and
The unsorted part at the right end.
Initially, the sorted part is empty and the unsorted part is the entire list.
The smallest element is selected from the unsorted array and swapped with the leftmost
element, and that element becomes a part of the sorted array.
This process continues moving unsorted array boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities
are of Ο(n2), where n is the number of items.
3
Selection Sort
Unsorted Array
5 1 12 -5 16 2 12 14
Step 1 :
Unsorted Array
5 1 12 -5 16 2 12 14
0 1 2 3 4 5 6 7
Step 2 :
Unsorted Array (elements 0 to 7) Min index = 0, value = 5
-5
5 1 12 -5
5 16 2 12 14 Find min value from
Unsorted array
0 1 2 3 4 5 6 7
Index = 3, value = -5
Swap
4
Selection Sort
Step 3 : Min index = 1, value = 1
Unsorted Array (elements 1 to 7)
Find min value from
-5 1 12 5 16 2 12 14 Unsorted array
0 1 2 3 4 5 6 7 Index = 1, value = 1
Step 4 :
Unsorted Array
(elements 2 to 7)
Min index = 2, value = 12
-5 1 12
2 5 16 12
2 12 14 Find min value from
0 1 2 3 4 5 6 7 Unsorted array
Index = 5, value = 2
Swap
5
Selection Sort
Step 5 :
Unsorted Array
(elements 3 to 7)
Min index = 3, value = 5
Step 6 :
Unsorted Array
(elements 5 to 7)
Min index = 4, value = 16
-5 1 2 5 12
16 16
12 12 14 Find min value from
0 1 2 3 4 5 6 7 Unsorted array
Index = 5, value = 12
Swap
6
Selection Sort
Step 7 :
Unsorted Array
(elements 5 to 7)
Min index = 5, value = 16
-5 1 2 5 12 12
16 16
12 14 Find min value from
0 1 2 3 4 5 6 7 Unsorted array
Index = 6, value = 12
Swap
Step 8 :
Unsorted Array
(elements 6 to 7)
Min index = 6, value = 16
-5 1 2 5 12 12 14
16 16
14 Find min value from
0 1 2 3 4 5 6 7 Unsorted array
Index = 7, value = 14
Swap
7
SELECTION_SORT(K,N)
Given a vector K of N elements
This procedure rearrange the vector in ascending order using Selection Sort
The variable PASS denotes the pass index and position of the first element in the vector
The variable MIN_INDEX denotes the position of the smallest element encountered
The variable I is used to index elements
8
SELECTION_SORT(K,N)
1. [Loop on the Pass index]
Repeat thru step 4 for PASS = 1,2,…….., N-1
2. [Initialize minimum index]
MIN_INDEX PASS
3. [Make a pass and obtain element with smallest value]
Repeat for I = PASS + 1, PASS + 2, …………….., N
If K[I] < K[MIN_INDEX]
Then MIN_INDEX I
4. [Exchange elements]
IF MIN_INDEX <> PASS
Then K[PASS] → K[MIN_INDEX]
5. [Finished]
Return
9
Bubble Sort
Unlike selection sort, instead of finding the smallest record and performing the interchange,
two records are interchanged immediately upon discovering that they are out of order
During the first pass R1 and R2 are compared and interchanged in case of our of order, this
process is repeated for records R2 and R3, and so on.
This method will cause records with small key to move “bubble up”,
After the first pass, the record with largest key will be in the nth position.
On each successive pass, the records with the next largest key will be placed in position n-1, n-
2 ….., 2 respectively
This approached required at most n–1 passes, The complexity of bubble sort is O(n2)
10
Bubble Sort
Unsorted Array
45 34 56 23 12
Pass 1 : Pass 2 : Pass 3 : Pass 4 :
34
45 34 34 34 34 34 34 23
34 23 12
23
swap
swap
swap
34
45 45 45 45 45 45
23 23 23
34 34
12 12
23
swap
swap
56 56 56
23 23 23 23
45 45
12 12 12
34 34
swap
swap
23 23 23
56 56
12 12 12 12
45 45 45 45
swap
12 12 12 12
56 56 56 56 56 56 56
11
BUBBLE_SORT(K,N)
Given a vector K of N elements
This procedure rearrange the vector in ascending order using Bubble Sort
The variable PASS & LAST denotes the pass index and position of the first element in the
vector
The variable EXCHS is used to count number of exchanges made on any pass
The variable I is used to index elements
12
Procedure: BUBBLE_SORT (K, N)
1. [Initialize]
LAST N
2. [Loop on pass index]
Repeat thru step 5 for PASS = 1, 2, 3, …. , N-1
3. [Initialize exchange counter for this pass]
EXCHS 0
4. [Perform pairwise comparisons on unsorted elements]
Repeat for I = 1, 2, ……….., LAST – 1
IF K[I] > K [I+1]
Then K[I] ➔ K[I+1]
EXCHS EXCHS + 1
5. [Any exchange made in this pass?]
IF EXCHS = 0
Then Return (Vector is sorted, early return)
ELSE LAST LAST - 1
6. [Finished]
Return
13
Merge Sort
The operation of sorting is closely related to process of merging
Merge Sort is a divide and conquer algorithm
It is based on the idea of breaking down a list into several sub-lists until each sub list consists
of a single element
Merging those sub lists in a manner that results into a sorted list
Procedure
Divide the unsorted list into N sub lists, each containing 1 element
Take adjacent pairs of two singleton lists and merge them to form a list of 2 elements. N will now convert
into N/2 lists of size 2
Repeat the process till a single sorted list of obtained
14
Merge Sort
Unsorted Array
724 521 2 98 529 31 189 451
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
724 521 2 98 529 31 189 451
15
Merge Sort
Step: Select the left subarray, Split the selected array (as evenly as possible)
0 1 2 3 0 1 2 3
724 521 2 98 529 31 189 451
0 1 0 1 0 1 0 1
724 521 2 98 529 31 189 451
0 0 0 0 0 0 0 0
724 521 2 98 529 31 189 451
17
Insertion Sort cont.
Complexity of the Insertion Sort Algorithm
To sort a unsorted list with 'n' number of elements we need to make (1+2+3+......+n-1) =
(n (n-1))/2 number of comparisons in the worst case.
If the list already sorted, then it requires 'n' number of comparisons.
• Worst Case : Θ(n2)
• Best Case : Ω(n)
• Average Case : Θ(n2)
18
Insertion Sort Example
Sort given array using Insertion Sort
6 5 3 1 8 7 2 4
Pass - 1 : Select First Record and considered as Sorter Sub-array
6 5 3 1 8 7 2 4
Sorted Unsorted
Pass - 2 : Select Second Record and Insert at proper place in sorted array
6 5 3 1 8 7 2 4
5 6 3 1 8 7 2 4
Sorted Unsorted
19
Insertion Sort Example Cont.
Pass - 3 : Select Third record and Insert at proper place in sorted array
5 6 3 1 8 7 2 4
3 5 6 1 8 7 2 4
Sorted Unsorted
Pass - 4 : Select Forth record and Insert at proper place in sorted array
3 5 6 1 8 7 2 4
1 3 5 6 8 7 2 4
Sorted Unsorted
20
Insertion Sort Example Cont.
Pass - 5 : Select Fifth record and Insert at proper place in sorted array
1 3 5 6 8 7 2 4
8 is at proper position
1 3 5 6 8 7 2 4
Sorted Unsorted
Pass - 6 : Select Sixth Record and Insert at proper place in sorted array
1 3 5 6 8 7 2 4
1 3 5 6 7 8 2 4
Sorted Unsorted
21
Insertion Sort Example Cont.
Pass - 7 : Select Seventh record and Insert at proper place in sorted array
1 3 5 6 7 8 2 4
1 2 3 5 6 7 8 4
Sorted Unsorted
Pass - 8 : Select Eighth Record and Insert at proper place in sorted array
1 2 3 5 6 7 8 4
1 2 3 4 5 6 7 8
Sorted Unsorted
22