Sorting Techniques
Sorting Techniques
and Algorithm
Sorting of list A refers to the operation of rearranging the contents of A so that they are in
increasing (or decreasing) order.
Sorting Techniques
Sorting can be performed in many ways.
Over a time several methods (or algorithms) are being developed to sort data(s).
Bubble sort,
Selection sort,
Quick sort,
Merge sort,
Heap sort
Binary sort,
Shell sort
Radix sort are the few sorting techniques
Bubble Sort
If N elements are given in memory then for sorting we do the following steps
1. First compare the 1st and 2nd element of array if 1st is greater than 2nd then compare 2nd with
3rd.
2. If 2nd >3rd then interchange the value of 2nd and 3rd
3. Now compare the value of 3rd(which has the value of 2nd) with 4th.
4. Similarly compare until the N-th element is compared with Nth element.
5. Now the highest value element is reached at the Nth place.
6. Now the elements will be compared until N-1 elements.
Bubble Sort
Bubble Sort
Bubble Sort
Bubble Sort
Bubble Sort
Bubble Sort
sorted unsorted
19
Insertion Sort
20
Insertion Sort
INSERTION-SORT 1 2 3 4 5 6 7 8
a1 a2 a3 a4 a5 a6 a7 a8
Alg.: INSERTION-SORT(A)
for j = 2 to n
key
do key =A[ j ]
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i=j-1
while i > 0 and A[i] > key
do A[i + 1] = A[i]
i=i–1
A[i + 1] = key
Insertion sort – sorts the elements in place
22
Example (ascending order)
Original
Array 76 3 94 55 21 1
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>55
Interchange 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
Task :
Cont . . .
◦ Sort the following into descending order
Un Sorted
Array 7 5 2 4 3 9
Merging
◦ If there are two sorted lists of array then process of combining these sorted
lists into sorted order is called merging.
◦ We can take this process with two approaches.
◦ First one, take the first array, after that take second array and sort them with
any sorting.
◦ But it is not useful because both the lists are sorted and we are taking them as
unsorted list.
◦ The second approach is to take one element of each array, compare them and
then take the smaller one in third array. Repeat this process until the elements
of any array are finished. Then take the remaining elements of unfinished array
in third 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 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
Merge Sort
In second approach of merging, we can take the pair of consecutive array
elements, merge them in sorted array and then take adjacent pair of array
elements and so on until the all elements of array are in single list.
Merge Sort
Based on divide and conquer strategy
• Right: The Array element on Right Mark must be greater than the
pivot value.
Step 4: Rule for Pivot value
• Left: The array element on the leftmark must be less than the
pivot value.
• Right: The pivot value must be greater than the array element on
the right side right side.
Step 5: Rule for Pivot value
• Left: The array element on the leftmark must be less than
the pivot value.
10 80 30 90 40 50 70