Unit III-V - Sorting at CSJMU - 6 Slides Handouts
Unit III-V - Sorting at CSJMU - 6 Slides Handouts
Sorting Sorting
• One fundamental problem of computer
science is ordering a list of items. Algorithms are divided into two categories:
Sorting Unit III. 1 Dr. Rabins Porwal Sorting Unit III. 2 Dr. Rabins Porwal
1 2
Sorting Sorting
Internal Sort: External Sort:
• Any sort algorithm which uses main • Any sort algorithm which uses external
memory exclusively during the sort. memory, such as tape or disk, during the
sort.
• This assumes high-speed random access
to all memory.
Sorting Unit III. 3 Dr. Rabins Porwal Sorting Unit III. 4 Dr. Rabins Porwal
3 4
Sorting Sorting
Note: Sort Stable
Algorithms may read the initial values from
magnetic tape or write sorted values to A sort algorithm is said to be “stable” if
disk, but this is not using external memory multiple items which compare as equal will
during the sort. Note that even though stay in the same order they were in after a
virtual memory may mask the use of disk, sort.
sorting sets of data much larger than main
memory may be much faster using an
explicit external sort.
Sorting Unit III. 5 Dr. Rabins Porwal Sorting Unit III. 6 Dr. Rabins Porwal
5 6
Cont’d…
Sorting Unit III. 7 Dr. Rabins Porwal Sorting Unit III. 8 Dr. Rabins Porwal
7 8
9 10
The oldest and simplest sort in use. The algorithm repeats this process until it
makes a pass all the way through the list
Unfortunately, also the slowest.
without swapping any items.
Works by comparing each item in the list with
the item next to it, and swapping them if This causes larger values to "bubble" to the
required. end of the list while smaller values "sink"
towards the beginning of the list.
Sorting Unit III. 11 Dr. Rabins Porwal Sorting Unit III. 12 Dr. Rabins Porwal
11 12
13 14
Sorting Unit III. 15 Dr. Rabins Porwal Sorting Unit III. 16 Dr. Rabins Porwal
15 16
17 18
19 20
Sorting Unit III. 21 Dr. Rabins Porwal Sorting Unit III. 22 Dr. Rabins Porwal
21 22
23 24
Sorting Unit III. 25 Dr. Rabins Porwal Sorting Unit III. 26 Dr. Rabins Porwal
25 26
• Algorithm Partition
Input: A list of elements stored in an array A. The leftmost and rightmost
• Algorithm QuickSort elements of the list under partition are located at left and right,
Input: A list of elements stored in an array A[low … high], respectively.
where low and high are two boundaries. Output: Returns loc as the final position of the pivot element.
Steps:
Output: Elements in A are stored in ascending order. 1. loc = left // The leftmost element is chosen as the pivot element
2. While (left < right) do
Steps: 3. While (A[loc] ≤ A[right]) and (loc < right) do
1. left = low, right = high 4. right = right – 1
5. EndWhile
2. If (left < right) then 6. If (A[loc] > A[right]) then
7. Swap(A[loc], A[right])
3. loc = Partition(A, left, right) 8. loc = right
9. left = left + 1
4. QuickSort(A, left, loc-1) 10. EndIf
11. While (A[loc] ≥ A[left]) and (loc > left) do
5. QuickSort(A, loc+1, right) 12. left = left + 1
6. EndIf 13. EndWhile
14. If (A[loc] < A[left]) then
7. Stop 15. Swap(A[loc], A[left])
16. loc = left
17. right = right – 1
18. EndIf
19. EndWhile
Sorting Unit III. 27 Dr. Rabins Porwal Sorting20. Return (loc) Unit III. 28 Dr. Rabins Porwal
21. Stop
27 28
Sorting Unit III. 29 Dr. Rabins Porwal Sorting Unit III. 30 Dr. Rabins Porwal
29 30
Sorting Unit III. 31 Dr. Rabins Porwal Sorting Unit III. 32 Dr. Rabins Porwal
31 32
33 34
Sorting Unit III. 35 Dr. Rabins Porwal Sorting Unit III. 36 Dr. Rabins Porwal
35 36
37 38
Bucket Sort
Now, pull the elements from
the buckets into the array
39