DSA Lec 15
DSA Lec 15
Sorting Techniques
Course Instructor
Engr. Anum Raza
Sorting- Introduction
Sorting is used to arrange names and numbers in meaningful ways.
Bubble sort,
Selection sort,
Quick sort,
Merge sort,
Heap sort
Binary sort,
Shell sort
Radix sort are the few sorting techniques
Insertion Sort
Insertion sort algorithm sorts a set of values by inserting values into an
existing sorted file.
You pick an unsorted card one at a time and insert it into its correct
position in your hand.
Working of insertion sort
Insertion sort splits an array into two sub-arrays.
The first sub-array is always sorted and gets larger as the sort continues.
When the sort first begins the first elements in the array is considered
to sorted array.
With each iteration of the loop, the next value in the unsorted section
is placed into its proper position in the sorted section.
Iteration 1
76 3 94 55 21 1
Iteration 2
S0: Swap 3 76 94 55 21 1
Iteration 3
3 76 94 55 21 1
94<76 No
leave 3 76 94 55 21 1
Cont . . .
Iteration 4
S0:Swap 3 76 55 94 21 1
55<76 True
Swap 3 55 76 94 21 1
55<3 No
Leave 3 55 76 94 21 1
Cont . . . .
Iteration 5
3 55 76 94 21 1
S0: True
Swap 3 55 76 21 94 1
21<76 True
Swap 3 55 21 76 94 1
21<55 True
Swap 3 21 55 76 94 1
21<3 No
Leave 3 21 55 76 94 1
Cont . . .
Iteration 6
3 21 55 76 94 1
S0: true
Swap 3 21 55 76 1 94
1<76 true
Swap 3 21 55 1 76 94
1<55 true
Swap 3 21 1 55 76 94
1<21 true
Swap 3 1 21 55 76 94
1<3 true
Swap 1 3 21 55 76 94
Sorted
1 3 21 55 76 94
Cont . . .
Task :
Sort the following into descending order
Un Sorted
Array 7 5 2 4 3 9
Algorithm for Insertion Sort
for i = 1 to n-1
j =1
while j > 0 and A[ j ] < A[ j -1]
swap (A[ j ] , A[ j – 1 ])
j=j-1
Merge Sort
Based on divide and conquer strategy
A L G O R I T H M S
A L G O R I T H M S divide
Merge sort
Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
Merge sort
Mergesort (divide-and-conquer)
Divide array into two halves.
Recursively sort each half.
Merge two halves to make sorted whole.
A L G O R I T H M S
A L G O R I T H M S divide
A G L O R H I M S T sort
A G H I L M O R S T merge
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
smallest smallest
A G L O R H I M S T
A G H I L M O R auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done. first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S auxiliary array
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
first half
exhausted smallest
A G L O R H I M S T
A G H I L M O R S T auxiliary array
Algorithm for Merge Sort
MERGE-SORT(A, p, r) MERGE(A, p, q, r)
1. Compute n1 and n2
if p < r 2. Copy the first n1 elements into
Check for base case L[1 . . n1 + 1] and the next
n2 elements into R[1 . . n2 + 1]
then q ← (p + r)/2 3. L[n1 + 1] ← ; R[n2 + 1] ←
MERGE-SORT(A, p, q) 4. i ← 1; j ← 1
MERGE-SORT(A, q + 1, r) 5. for k ← p to r
6. do if L[ i ] ≤ R[ j ]
MERGE(A, p, q, r)
7. then A[k] ← L[ i ]
8. i ←i + 1
p q r 9. else A[k] ← R[ j ]
1 2 3 4 5 6 7 8 10. j←j+1
5 2 4 7 1 3 2 6
p q r p q
1 2 3 4 5 6 7 8
L 2 4 5 7
2 4 5 7 1 2 3 6 q+1 r
R 1 2 3 6
n1 n2