0% found this document useful (0 votes)
21 views7 pages

Unit III-V - Sorting at CSJMU - 6 Slides Handouts

Uploaded by

raghvendrac210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Unit III-V - Sorting at CSJMU - 6 Slides Handouts

Uploaded by

raghvendrac210
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit - III

Sorting Sorting
• One fundamental problem of computer
science is ordering a list of items. Algorithms are divided into two categories:

• Many solutions exist to this problem, • Internal Sorts


known as sorting algorithms.
and
• Some sorting algorithms are simple and
intuitive, such as the bubble sort. Others,
such as the quick sort are extremely • External sorts
complicated, but produce fast results.

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

Prepared by: Dr. Rabins Porwal 1


Unit - III

Sorting Internal Sorting


All the George names
Sort Stable maintain their Bucket Sort
position, although
Example: Arthur is less than
Peter
• Possibly the simplest distribution sorting
algorithm.
Tom Greene Ann George
Ann George Peter George • The essential requirement is that the size
Sorting on
Peter George
surnames
Arthur George of the universe, from which the elements
Arthur George Tom Greene to be sorted are drawn, is a small, fixed
Joe Summers Joe Summers constant, say m.

Cont’d…
Sorting Unit III. 7 Dr. Rabins Porwal Sorting Unit III. 8 Dr. Rabins Porwal

7 8

Internal Sorting Internal Sorting


Bucket Sort
Bucket Sort (Example)
For example, suppose that we are sorting
elements drawn from {0, 1, . . ., m-1}, i.e.,
the set of integers in the interval [0, m-1].

Bucket sort uses m counters. The ith


counter keeps track of the number of
occurrences of the ith element of the
universe.
Sorting Unit III. 9 Dr. Rabins Porwal Sorting Unit III. 10 Dr. Rabins Porwal

9 10

Internal Sorting Internal Sorting


Bubble Sort Bubble Sort

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

Prepared by: Dr. Rabins Porwal 2


Unit - III

Internal Sorting Algorithm BubbleSort


Input: An array A[1, 2, …, n], where n is the number
Bubble Sort of elements.
Output: An array A with all elements in sorted order.
Pros: Steps:
Simple and easy 1. For i = 1 to n – 1 do
to implement. 2. For j = 1 to n – i do
3. If (A[j] > A[j+1]) then
4. Swap (A[j], A[j+1])
Cons: 5. EndIf
Horribly inefficient. 6. EndFor
7. EndFor
8. Stop
Sorting Unit III. 13 Dr. Rabins Porwal Sorting Unit III. 14 Dr. Rabins Porwal

13 14

Internal Sorting Internal Sorting


Selection Sorting Selection Sorting
• Algorithm constructs the sorted sequence • Because the elements are added to the
one element at a time by adding elements sorted sequence in order, they are always
to the sorted sequence in order. added at one end.

• At each step, the next element to be


added to the sorted sequence is selected
from the remaining elements.

Sorting Unit III. 15 Dr. Rabins Porwal Sorting Unit III. 16 Dr. Rabins Porwal

15 16

Internal Sorting • Algorithm SelectionSort


Input: An input list A[1, …, N].
Output: The list A[1, …, N] in sorted order.
Selection Sorting Steps:
1. For i = 1 to (n – 1) do
Pros: 2. k=i
Simple and easy 3. For j = i+1 to n do
to implement. 4. If (A[k] > A[j]) then
5. k=j
6. EndIf
Cons: 7. EndFor
Inefficient for 8. If (i ≠ k) then
large lists 9. Swap (A[i], A[k])
10. EndIf
11. EndFor
12. Stop
Sorting Unit III. 17 Dr. Rabins Porwal Sorting Unit III. 18 Dr. Rabins Porwal

17 18

Prepared by: Dr. Rabins Porwal 3


Unit - III

Internal Sorting Internal Sorting


Insertion Sorting Insertion Sorting
• Works just like its name suggests - it • To save memory, most implementations
inserts each item into its proper place in use an in-place sort that works by moving
the final list. the current item past the already sorted
items and repeatedly swapping it with the
• The simplest implementation of this preceding item until it is in place.
requires two list structures - the source list
and the list into which sorted items are
inserted.
Sorting Unit III. 19 Dr. Rabins Porwal Sorting Unit III. 20 Dr. Rabins Porwal

19 20

Internal Sorting • Algorithm InsertionSort


Input: An array A[1, 2, …, n], where n is the number
of elements.
Insertion Sorting
Output: An array A with all elements in sorted order.
Steps:
Pros:
1. For i = 2 to n – 1 do
Relatively simple
2. k = A[i]
and easy to
3. For j = i – 1 to 1 do
implement.
4. If (k < A[j]) then
5. A[j+1] = A[j]
Cons: 6. EndIf
Inefficient for 7. EndFor
large lists. 8. A[j] = k
9. EndFor
10. Stop

Sorting Unit III. 21 Dr. Rabins Porwal Sorting Unit III. 22 Dr. Rabins Porwal

21 22

Internal Sorting Internal Sorting


Quicksort Quicksort

An in-place, divide-and-conquer, massively Recursive algorithm consists of four steps:


recursive sort.
1. If there is one or less element in the array
to be sorted, return immediately.
Algorithm is simple in theory, but very difficult
to put into code.
2. Pick an element in the array to serve as a
"pivot" point. (Usually the left-most
element in the array is used.)
Sorting Unit III. 23 Dr. Rabins Porwal Sorting Unit III. 24 Dr. Rabins Porwal

23 24

Prepared by: Dr. Rabins Porwal 4


Unit - III

Internal Sorting Internal Sorting


Quicksort
Quicksort
3. Split the array into two parts - one with Pros:
elements larger than the pivot and the Extremely fast.
other with elements smaller than the pivot.
Cons:
4. Recursively repeat the algorithm for both Very complex
halves of the original array. algorithm, massively
recursive.

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

Internal Sorting Internal Sorting


Heap Sort Heap Sort
• The slowest of the fast sorting algorithms, • Works as it’s name suggests:
but does not require massive recursion or
multiple arrays to work. • Begins by building a heap out of the data
set, and then removing the largest item
• This makes it the most attractive option for and placing it at the end of the sorted
very large data sets of millions of items. array.

Sorting Unit III. 29 Dr. Rabins Porwal Sorting Unit III. 30 Dr. Rabins Porwal

29 30

Prepared by: Dr. Rabins Porwal 5


Unit - III

Internal Sorting Internal Sorting


Heap Sort Heap Sort
• After removing the largest item, it
reconstructs the heap and removes the • This is repeated until there are no items
largest remaining item and places it in the left in the heap and the sorted array is full.
next open position from the end of the
sorted array.

Sorting Unit III. 31 Dr. Rabins Porwal Sorting Unit III. 32 Dr. Rabins Porwal

31 32

Internal Sorting External Sorting


Heap Sort Generally carried out when serial files
need to be converted to sequential files
Pros: and there is insufficient space within the
In-place and non-recursive, making it a good main memory of the system to hold all the
choice for extremely large data sets. data at once.

Cons: There are numerous algorithms which are


Slower than the merge and quick sorts. used to perform sorts external to the
computer’s main memory.
Sorting Unit III. 33 Dr. Rabins Porwal Sorting Unit III. 34 Dr. Rabins Porwal

33 34

External Sorting External Sorting


Among the more popular algorithms are: Polyphase sort is the most efficient in
• Tag Sorts terms of speed and utilisation of
• Four Tape Sort resources.
• Polyphase Sort However, it is also most complicated.
• External Radix Sort
• External Merge. In practice these sorting methods are
already being supplemented by internal
sorts.

Sorting Unit III. 35 Dr. Rabins Porwal Sorting Unit III. 36 Dr. Rabins Porwal

35 36

Prepared by: Dr. Rabins Porwal 6


Unit - III

External Sorting Bucket Sort


Thus, a number of records from each tape Each element of the array is put in one of the N “buckets”
would be read into main memory and
sorted using an internal sort and then
output to the tape rather than one record
at a time as was the case initially.

Sorting Unit III. 37 Dr. Rabins Porwal Sorting

37 38

Bucket Sort
Now, pull the elements from
the buckets into the array

At last, the sorted array


(sorted in a stable way):
Sorting

39

Prepared by: Dr. Rabins Porwal 7

You might also like