Professional Documents
Culture Documents
Insertion Sort
Insertion Sort
• Insertion sort is one of the most common sorting techniques used by card players.
• The list is divide into two sub lists as sorted and unsorted.
• In each pass, the first element of the unsorted sub list is picked up and
transformed into the sorted sub list by inserting at the appropriate place.
• A list of n elements, the insertion sort requires at most n-1 passes to sort the data.
1
1
Insertion Sort (2 of 4)
Example:
2
1
Insertion Sort (3 of 4)
Flowchart
3
1
Insertion Sort (4 of 4)
if ( A temp < A [ j ] )
{
A[j+1] = A[j];
j - -;
}//if
else
flag = true;
A[j+1] = temp;
}
}
4
1
Merge sort (1 of 6)
• Merge sort is a divide and conquer external sorting algorithm.
• The basic procedure of merge sort is to divide the list to be sorted into two smaller
sub lists (roughly equal in size) and recursively repeat this procedure till only one
element is left in the sub list.
• After this the various sorted sub lists are merged back to from the parent list
• The merging process also goes on recursively till the sorted original list is arrived
at.
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
99 6 86 15 58 35 86 4 0
Divide
1
4 06
Merge sort Example (3 of 6)
0 4 6 15 35 58 86 86 99
6 15 86 99 0 4 35 58 86
6 99 15 86 58 35 0 4 86
99 6 86 15 58 35 86 0 4
Merge 1
4 07
Merge sort Algorithm (4 of 6)
1.Start
6.Stop
1 8
Merge sort Algorithm (5 of 6)
1 9
Merge sort Algorithm (6 of 6)
merge(A, low, mid, high)
1. n1 ← mid – low + 1
2. n2 ← high – mid
3. create array L[n1], R[n2]
4. for i ← 0 to n1-1 do L[i] ← A[low +i]
5. for j ← 0 to n2-1 do R[j] ← A[mid+j]
6. k←i←j←0
7. while i < n1 & j < n2
8. if L[i] < R[j]
9. A[k++] ← L[i++]
10. else
11. A[k++] ← R[j++]
12. while i < n1
13. A[k++] ← L[i++]
14. while j < n2
15. A[k++] ← R[j++]
1 10
Shellsort
1 11
Shellsort
• Shellsort uses a sequence h1, h2, …, ht called
the increment sequence. Any increment
sequence is fine as long as h1 = 1 and some
other choices are better than others.
1 12
Shellsort
• Shellsort improves on the efficiency of
insertion sort by quickly shifting values to their
destination.
1 13
Shellsort
• Shellsort is also known as diminishing
increment sort.
• The distance between comparisons decreases
as the sorting algorithm runs until the last
phase in which adjacent elements are
compared
1 14
Shellsort
• After each phase and some increment hk, for
every i, we have a[ i ] ≤ a [ i + hk ] all elements
spaced hk apart are sorted.
• The file is said to be hk – sorted.
1 15
Shellsort Examples
Sort: 18 32 12 5 38 33 16 2
8 Numbers to be sorted, Shell’s increment will be floor(n/2)
* floor(8/2) floor(4) = 4
18 32 12 5 38 33 16 2
Sort: 18 32 12 5 38 33 16 2
8 Numbers to be sorted, Shell’s increment will be floor(n/2)
* floor(8/2) floor(4) = 4
18 32 12 5 38 33 16 2
increment 2: 1 2
18 32 12 2 38 33 16 5
Step 1) Look at 18, 12, 38, 16 and sort them in their appropriate location:
12 38 16 2 18 33 38 5
Step 2) Look at 32, 2, 33, 5 and sort them in their appropriate location:
12 2 16 5 1
18 32 38 33 18
Shellsort Examples (con’t)
Sort: 18 32 12 5 38 33 16 2
* floor(2/2) floor(1) = 1
increment 1: 1
12 2 16 5 18 32 38 33
2 5 12 16 18 32 33 38
1 20
Radix Sort
In input array A, each element is a number of d digit.
Radix - Sort ( A, d )
for i 1 to d
do " use a stable sort to sort array A on digit i;
329 720 720 329
457 355 329 355
657 436 436 436
839 457 839 457
436 657 355 657
720 329 457 720
355 839 657 839
1 21
Radix Sort Review
• Assumption: input taken from large set of numbers
• Basic idea:
– Sort the input on the basis of digits starting from unit’s
place.
– This gives the position of that number – similar to selection
sort.
• Pro’s:
– Fast
– Asymptotically fast - O(d(n+k))
– Simple to code
• Con’s:
– Doesn’t sort in place.
1 22
Comparison of Internal
Sorting Techniques
23
1
Comparison of External
Sorting Techniques
24
1