Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

Data Structures

Unit 5: Sorting

1
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Sorting

5.1. Elementary Sorting Algorithm:


5.1.1. Bubble Sort
5.1.2. Insertion Sort
5.1.3. Selection Sort

5.2. Efficient Sorting Algorithm:


5.2.1. Shell Sort
5.2.2. Quick Sort
5.2.3. Merge Sort
5.2.4. Radix Sort
5.2.5. Heap Sort
5.2.6. Counting Sort

2
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.0.
Introduction

3
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.0. Introduction to Sorting
▪ Sorting is known as a fundamental operation in computer science.

▪ Sorting means rearranging data in a specific order, such as ascending


or descending or lexicographic order.

▪ Data may be of any type like numerical, alphabetical or


alphanumeric.

▪ Sorting also refers to rearranging a set of records based on their key


values when the records are stored in a file.

▪ There are other several types of sorting, done internally and externally.
✓ When a set of data to be sorted in small enough, then the entire sorting
can be performed in a computer internal storage i.e. in primary memory,
it is known as internal sort.
4
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.0. Introduction to Sorting (Conti…)
✓ And when a large set of data to be sorted, it is stored in low speed
computer’s external memory like hard disk, magnetic tape, etc., then it is
known as external sort.

▪ The sorting task is important.


Why???
1. How to rearrange a given set of data?
2. Which data structures are more suitable to store data prior to their
sorting?
3. How fast can the sorting be achieved?
4. How can sorting be done in memory constraint situation?
5. How to sort various types of data?

▪ Here, we have classified the sorting technique algorithms as…


1. Elementary Sorting
2. Efficient Sorting 5
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.1.
Elementary Sorting Algorithm

6
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.1.1. Bubble Sort


▪ For example:

15 6 13 22 3 52 60
Step:1
15 6 13 22 3 52 60
6 15 13 22 3 52 60
Step:2
6 15 13 22 3 52 60
6 13 15 22 3 52 60
Step:3
6 13 15 22 3 52 60
6 13 15 22 3 52 60
7
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.1.1. Bubble Sort (Conti…)


▪ For example: (Conti…)
Step:4
6 13 15 22 3 52 60
6 13 15 3 22 52 60
Step:5
6 13 15 3 22 52 60
6 13 15 3 22 52 60
Step:6
6 13 15 3 22 52 60
6 13 15 3 22 52 60
Step:7
6 13 15 3 22 52 60
8
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.1.1. Bubble Sort (Conti…)


▪ For example: (Conti…)
6 13 15 3 22 52 60
Step:8
6 13 15 3 22 52 60
6 13 15 3 22 52 60
Step:9
6 13 15 3 22 52 60
6 13 3 15 22 52 60
Step:10
6 13 3 15 22 52 60
Step:11
6 13 3 15 22 52 60

9
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.1.1. Bubble Sort (Conti…)


▪ For example: (Conti…)
Step:12
6 13 3 15 22 52 60
Step:13
6 13 3 15 22 52 60
6 13 3 15 22 52 60
Step:14
6 13 3 15 22 52 60
6 3 12 15 22 52 60
Step:15
6 13 3 15 22 52 60
Step:16
6 3 12 15 22 52 60
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY 10
Demonstration

CE: 5.1.1. Bubble Sort (Conti…)


▪ For example: (Conti…)
Step:17
6 3 12 15 22 52 60
Step:18
6 3 12 15 22 52 60
Step:19
6 3 12 15 22 52 60
3 6 12 15 22 52 60
Step:20
3 6 12 15 22 52 60
Step:21
3 6 12 15 22 52 60

11
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.1.1. Bubble Sort (Conti…)


▪ For example: (Conti…)
Step:22
3 6 12 15 22 52 60
Step:23
3 6 12 15 22 52 60
Step:24
3 6 12 15 22 52 60

Output

3 6 12 15 22 52 60

12
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.1.1. Bubble Sort (Conti…)
▪ Algorithm:

Steps:
1. For i=1 to n-1 do
2. For j=1 to n-1 do
3. If (a[j] > a[j+1])
4. swap(a[j], a[j+1]) // Swap pair wise element
5. EndIf
6. EndFor
7. EndFor
8. Stop

The time complexity of bubble sort in terms of key comparisons is


given by O(n2).
13
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.1.2. Insertion Sort


▪ For example:

15 6 13 22 3 52 60

15
6 15
6 13 15
6 13 15 22
3 6 13 15 22
3 6 13 15 22 52
3 6 13 15 22 52 60

14
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.1.2. Insertion Sort (Conti…)
▪ Algorithm:

Steps:
1. For i=1 to n do
2. KEY = a[i]
3. POSITION = i
4. While (POSITION > 1) and (a[POSITION – 1] > KEY) do
5. a[POSITION] = a[POSITION–1]
6. POSITION = POSITION–1
7. a[POSITION] = KEY
8. EndWhile
9. EndFor

▪ KEY – is the element to be inserted.


▪ POSITION – is the location in the unordered array list.
15
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.1.2. Insertion Sort (Conti…)
▪ The best case complexity of insertion sort arises, when the list is
already sorted in the ascending order. In such a case, the complexity in
terms of comparisons is given by O(n).

▪ And the average case performance of insertion sort reports O(n2)


complexity.

16
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.1.3. Selection Sort


▪ For example:

15 6 13 22 3 52 2

2 6 13 22 3 52 15

2 3 13 22 6 52 15

2 3 6 22 13 52 15

17
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.1.3. Selection Sort (Conti…)


▪ For example: (Conti…)

2 3 6 13 22 52 15

2 3 6 13 15 52 22

2 3 6 13 15 22 52

Output

2 3 6 13 15 22 52

18
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.1.3. Selection Sort (Conti…)
▪ Algorithm:
Steps:
selection_sort(i,n)
1. For i=1 to n-1 do
2. min_index = find_min(i,n)
3. swap(a[i], a[min_index])
4. EndFor
find_min(i,n)
5. min_index = i
6. For j=i+1 to n do
7. If(a[j] < a[min_index])
8. min_index = j
9. EndIf
10. EndFor
11. return(min_index)
12. Stop 19
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.1.3. Selection Sort (Conti…)
▪ The time complexity of find_min() is O(n).

▪ And the time complexity of selection_sort() is O(n2).

20
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Class Work
▪ Sort the following data using bubble sort algorithm:
25, 37, 52, 38, 12, 86, 92

▪ Sort the following data using insertion sort algorithm:


25, 37, 52, 38, 12, 86, 92, 44, 36, 20

▪ Sort the following data using selection sort algorithm:


34, 41, 22, 46, 54, 29, 36, 56, 52

21
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.
Efficient Sorting Algorithm

22
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.1. Shell Sort
▪ Shell sort is a highly efficient sorting algorithm and is based on
insertion sort algorithm.

▪ This algorithm avoids large shifts as in case of insertion sort, if the


smaller value is to the far right and has to be moved to the far left.

▪ For the working of shell sort….


✓ Find the difference.
✓ Compare the elements with the difference.
✓ Reduce the difference by difference/2.
✓ After some iterations, difference will be 1.

23
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.1. Shell Sort (Conti…)


▪ For example: 24, 37, 46, 11, 85, 47, 33, 66
floor(n/2) = floor(8/2) = 4

Pass: 1 24 37 46 11 85 47 33 66

24 85

24 37 85 47
24 37 33 85 47 46

24 37 33 11 85 47 46 66

24
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.1. Shell Sort (Conti…)


▪ For example: 24, 37, 46, 11, 85, 47, 33, 66
floor(n/2) = floor(4/2) = 2

Pass: 2 24 37 33 11 85 47 46 66

24 33 46 85

11 37 47 66

24 11 33 37 46 47 85 66

25
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.1. Shell Sort (Conti…)


▪ For example: 24, 37, 46, 11, 85, 47, 33, 66
floor(n/2) = floor(2/2) = 1

Pass: 3 24 11 33 37 46 47 85 66

11 24 33 37 46 47 85 66

11 24 33 37 46 47 85 66

11 24 33 37 46 47 85 66

11 24 33 37 46 47 85 66

11 24 33 37 46 47 85 66
11 24 33 37 46 47 85 66
11 24 33 37 46 47 66 85
26
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Class Work
▪ Sort the following data using shell sort algorithm:
10, 8, 6, 20, 4, 3, 22, 1, 0, 15, 16

27
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.2. Quick Sort
▪ QuickSort is a Divide and Conquer algorithm.

▪ It picks an element as pivot; and partitions the given array element


around the picked pivot.

▪ There are many different versions of QuickSort that pick pivot in


different ways.
1. Always pick first element as pivot.
2. Always pick last element as pivot
3. Pick median as pivot.
4. Pick a random element as pivot.

▪ The key process in QuickSort is partition().

28
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.2. Quick Sort (Conti…)


▪ For example:
34 26 1 45 18 78 12 89 27
Pivot i j
i <= pivot 26 <= 34 ✓ i++

34 26 1 45 18 78 12 89 27
i j
i <= pivot 1 <= 34 ✓ i++

34 26 1 45 18 78 12 89 27
i j
i <= pivot 45 <= 34  STOP and 27>34  STOP Swap(i , j)

34 26 1 27 18 78 12 89 45
i j 29
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.2. Quick Sort (Conti…)


▪ For example: (Conti…)
34 26 1 27 18 78 12 89 45
i j
i <= pivot 27 <= 34 ✓ i++

34 26 1 27 18 78 12 89 45
i j
i <= pivot 18 <= 34 ✓ i++

34 26 1 27 18 78 12 89 45
i j
i <= pivot 78 <= 34  STOP and 45>34 ✓ J--

34 26 1 27 18 78 12 89 45
i j
30
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.2. Quick Sort (Conti…)


▪ For example: (Conti…)
34 26 1 27 18
78 12 89 45
i j
i <= pivot 78 <= 34  STOP and 89>34 ✓ J--

34 26 1 27 18
78 12 89 45
i j
i <= pivot 78 <= 34  STOP and 12>34  STOP Swap(i , j)

34 26 1 27 18 12 78 89 45
i j

12 26 1 27 18 34 78 89 45
31
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.2. Quick Sort (Conti…)


▪ For example:

32
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.2. Quick Sort (Conti…)


Partition(a, lower, upper)
{
pivot = a[lower];
start = lower;
end = upper;
while(start < end)
{
while(a[start] <= pivot) if(start < end)
{ {
start++; swap(a[start], a[end])
} }
while(a[end] > pivot) }
{ swap(a[lower], a[end]);
end--; return end;
} }
33
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.2. Quick Sort (Conti…)


QuickSort(a, first, last)
{
if (first < last) then
{
location = partition(a, first, last)
QuickSort(a, first, location – 1)
QuickSort(a, location + 1, last)
}
}

34
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.3. Merge Sort
▪ Merging or organizing is a process by which two ordered lists of
elements are combined or merged into a single ordered list.

▪ Merge sort makes use of the principle of merge to sort an unordered


list of elements.

35
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.3. Merge Sort (Conti…)


▪ For example:
34 26 1 45 18 78 12

34 26 1 45 18 78 12

34 26 1 45 18 78 12

34 26 1 45 18 78 12

26 34 1 45 18 78 12

36
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.3. Merge Sort (Conti…)


▪ For example: (Conti…)
26 34 1 45 18 78 12

1 26 34 45 12 18 78

1 12 18 26 34 45 78

37
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Class Work
▪ Consider the following data and perform the quick sort algorithm:
5, 1, 26, 15, 76, 34, 15

▪ Sort the following data using merge sort algorithm:


12, 56, 1, 34, 89, 78, 43, 10

38
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.4. Radix Sort
▪ The radix sort is a method of sorting which precedes any digital
computer.

▪ It is a sorting technique that sorts the elements by first grouping the


individual digits of the same place value. Then, sort the elements
according to their increasing/decreasing order.

39
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.4. Radix Sort (Conti…)


▪ For example:
Suppose, we have an array of 7 elements.
✓ First, we will sort elements based on the value of the once place.
✓ Then, we will sort elements based on the value of the tens place.
✓ This process goes on until the last significant place.

387 690 123 123


690 441 234 234
234 123 435 387
435 234 441 435
567 435 567 441
123 387 387 567
441 567 690 690
Array Pass 1 Pass 2 Pass 3
40
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.4. Radix Sort (Conti…)


▪ Radix sort is also known as bin sort or bucket sort or digital sort.

▪ For example:
387 690 234 435 567 123 441

The number of elements = 7


The number of digits of each elements = 3
i.e. The radix sort would require 10 auxiliary arrays and
would complete the sorting in 3 passes.

41
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.4. Radix Sort (Conti…)


▪ For example: 387 690 234 435 567 123 441

Pass 1 0 690
1 441
2
3 123
4 234
5 435
6
7 387 567
8
9

690 441 123 234 435 387 567 42


BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.4. Radix Sort (Conti…)


▪ For example: 690 441 123 234 435 387 567
Conti….
Pass 2 0
1

2 123
3 234 435
4 441
5
6 567
7
8 387
9 690
123 234 435 441 567 387 690 43
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.4. Radix Sort (Conti…)


▪ For example: 123 234 435 441 567 387 690
Conti….
Pass 3 0
1 123
2 234
3 387
4 435 441
5 567
6 690
7
8
9

123 234 387 435 441 567 690 44


BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Bin Sort Demonstration

CE: 5.2.4. Radix Sort (Conti…)


▪ For example: 387 690 234 435 567 123 441
Pass 1

567
690 441 123 234 435 387
0 1 2 3 4 5 6 7 8 9
{ 690, 441, 123, 234, 435, 387, 567 }
Pass 2

435
123 234 441 567 387 690
0 1 2 3 4 5 6 7 8 9
{ 123, 234, 435, 441, 567, 387, 690 }
Pass 3

441
123 234 387 435 567 690
0 1 2 3 4 5 6 7 8 9
{ 123, 234, 387, 435, 441, 567, 690 }
45
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.4. Radix Sort (Conti…)
▪ During the implementation of radix sort procedure in the computer, it
is convenient to make use of linked list for the representation of bins.

▪ Each of the headed linked lists representing the bins they could be
implemented as a linked queue with two pointers front and rear, each
pointing to the first and last node of the single linked list respectively.

▪ At the end of each pass, the elements from each list could be appended
to the output list by undertaking deletions in each of the linear queues
representing the bins until they are empty.

46
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Linked List implementation of Radix Sort - Bucket Sort Demonstration

CE: 5.2.4. Radix Sort (Conti…)


▪ For example: 387 690 234 435 567 123 441
Pass 1 Pass 2
0 690 NULL 0 NULL

1 441 NULL 1 NULL

2 NULL 2 123 NULL

3 123 NULL 3 234 435 NULL

4 234 NULL 4 441 NULL

5 435 NULL 5 NULL

6 NULL 6 567 NULL

7 387 567 NULL 7 NULL

8 NULL 8 387 NULL

9 NULL 9 690 NULL

{ 690, 441, 123, 234, 435, 387, 567 } { 123, 234, 435, 441, 567, 387, 690 }
47
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Linked List implementation of Radix Sort - Bucket Sort Demonstration

CE: 5.2.4. Radix Sort (Conti…)


▪ For example: 387 690 234 435 567 123 441
Pass 3
0 NULL

1 123 NULL

2 234 NULL

3 387 NULL

4 435 441 NULL

5 567 NULL

6 690 NULL

7 NULL

8 NULL

9 NULL

{ 123, 234, 387, 435, 441, 567, 690 }


48
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Class Work
▪ Trace radix sort on the following list:
5678, 2341, 90, 3219, 7676, 8704, 4561, 5000

▪ By considering the following data, perform the radix sort algorithm


taking radix as 2:
001, 101, 010, 000, 111, 110, 011, 100

49
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.5. Heap Sort
▪ Heap sort is performed on the heap data structure.

▪ Heap is a complete binary tree.

▪ Heap tree can be of two types: Min-heap or Max-heap.

▪ For min-heap the root element is minimum and for max-heap the root
is maximum.

▪ After forming a heap,


- we can delete an element from the root and
- send the last element to the root.

▪ After these swapping procedure, we need to re-heap the whole array.


and by deleting the elements from root we can sort the whole array.
50
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.5. Heap Sort (Conti…)
▪ The basic steps in the heap sort are listed below:
1. Create Heap
2. Remove Max
3. Rebuild Heap

51
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Create Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25

15 35 55
15

35 15 55 15 35

75

55 75 75

75 35 55 35 95
55

15 15 05 95 15 05 35
52
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Create Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)
95 95

55 85
55 75

15 05 35 75
15 05 35 85
65
95

55 85

65 05 35 75

15
53
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Create Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)

95

65 85

55 05 35 75 95

15 45 25 65 85

55 25 35 75

15 45 05

{ 95, 65, 85, 55, 25, 35, 75, 15, 45, 05 }


54
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Remove Max and Rebuild Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)
95 05

65 85 65 85

55 25 35 75 55 25 35 75

15 45 05 15 45 95
85
85

65 05 65 75

55 25 35 75 55 25 35 05

15 45 95 15 45 95
55
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Remove Max and Rebuild Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)
85 45

65 75 65 75

55 25 35 05 55 25 35 05

15 45 95 15 85 95
75

65 45

55 25 35 05

15 85 95
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Remove Max and Rebuild Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)
15 65

65 45 15 45

55 25 35 05 55 25 35 05

75 85 95 75 85 95
65

55 45

15 25 35 05

75 85 95
57
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Remove Max and Rebuild Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)
05 55

55 45 05 45

15 25 35 65 15 25 35 65

75 85 95 75 85 95
55 35

25 45 25 45

15 05 35 65 15 05 55 65

75 85 95 75 85 95
58
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Remove Max and Rebuild Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)
45 05

25 35 25 35

15 05 55 65 15 45 55 65

75 85 95 75 85 95
35 15

25 05 25 05

15 45 55 65 35 45 55 65

75 85 95 75 85 95
59
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Remove Max and Rebuild Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)
25 05

15 05 15 25

35 45 55 65 35 45 55 65

75 85 95 75 85 95
15
05

05 25
15 25

35 45 55 65 35 45 55 65

75 85 95
75 85 95 60
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Remove Max and Rebuild Heap Demonstration

CE: 5.2.5. Heap Sort (Conti…)


▪ For example: 15, 35, 55, 75, 05, 95, 85, 65, 45, 25 (Conti…)

05

15 25

35 45 55 65

75 85 95

{ 05, 15, 25, 35, 45, 55, 65, 75, 85, 95 }

61
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Home Work
▪ Trace the following list of elements to construct the heap tree:
12, 45, 21, 67, 34

62
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.6. Counting Sort
▪ Counting sort is a sorting algorithm that sorts the elements of an array
by counting the number of occurrences of each unique element in the
array.

▪ The count is stored in an auxiliary array and the sorting is done by


mapping the count as an index of the auxiliary array.

▪ Suppose n input element are to be sorted.


▪ All elements are within the range of 0 to k and stored in array a[1...n].
▪ Let another array C[0…k] be an array such that, C[i], 0 ≤ i ≤ k stores
the number of occurrences (count) of the elements having value.

63
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 5.2.6. Counting Sort (Conti…)
▪ Thus,
✓ Let the element be i and
✓ then increase the value in the location C[i].
✓ Next, for all i, 0 ≤ i ≤ k, summing C[i] = C[i – 1] + C[i], we get the ‘index’, that
is the number of elements less then or equal to i.
✓ The values in the array C, thus obtained give the indices (final position) of all
elements in the sorted list.

64
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.6. Counting Sort (Conti…)


▪ For example:
a[i] 4 2 2 8 3 3 1 2 n=8

So, the counting


length will be n+1 =
8+1=9

Step:01 C[i] 0 1 3 2 1 0 0 0 1 Count array


0 1 2 3 4 5 6 7 8
C[i] =
C[i – 1] + C[i]

Step:02 C[i] 0 1 4 6 7 7 7 7 8
0 1 2 3 4 5 6 7 8
65
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 5.2.6. Counting Sort (Conti…)


▪ For example: (Conti…)
a[i] 4 2 2 8 3 3 1 2 n=8

Step:02 C[i] 0 1 4 6 7 7 7 7 8
0 1 2 3 4 5 6 7 8
1-1=0 4-1=3 6-1=5 7-1=6 8-1=7 Place each
3-1=2 5-1=4 element of a[i] in
2-1=1 output array
C[ C[i] – 1 ]

Step:03 C[i] 1 2 2 2 3 3 4 8 Output array


0 1 2 3 4 5 6 7

66
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Home Work
▪ Trace the following list of elements to construct the heap tree:
12, 45, 21, 67, 34

▪ Sort the following data using counting sort algorithm:


4, 8, 4, 2, 9, 9, 6, 2, 9

67
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
68
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY

You might also like