A Continuation of DSA RTU Part 3
A Continuation of DSA RTU Part 3
CO1: Apply the concepts of data structure, data type and array data structure
and analyze the algorithms and determine their time complexity.
CO2: Apply various data structure such as stacks, Linked List, queues, trees
and graphs to solve various computing problems using C-programming
language.
CO3: Implement standard algorithms for searching & sorting and identify
when to choose which technique.
CO4: Apply the data structure that efficiently models the information in a
problem.
1 1
Searching Techniques
(UNIT III)
Linear Search
• The linear search is a sequential search, which uses a loop to step through
an array,starting with the first element.
• It compares each element with the value being searched for, and stops
when either the value is found or the end of the array is encountered.
• If the value being searched is not in the array, the algorithm will
unsuccessfully search to the end of the array.
Linear Search
• Since the array elements are stored in linear order searching the
element in the linear order make it easy and efficient.
• The search may be successful or unsuccessfully. That is, if the required
element is found them the search is successful other wise it is
unsuccessfully.
Advantages
element
element
element
element
element
element
Inthe best case, the target value is in the first element of the array.
So the search takes some tiny,and constant, amount of time.
Inthe worst case, the target value is in the last element of the array.
So the search takes an amount of time proportional to the length of the
array.
Analysis of Linear Search
The general term for a smart search through sorted data is a binary search.
1. The initial search region is the whole array.
2. Look at the data value in the middle of the search region.
3. Ifyou’ve found your target, stop.
4. Ifyour target is less than the middle data value, the new search region is the lower half
of the data.
5. Ifyour target is greater than the middle data value, the new
search region is the higher half of the data.
6. Continue from Step 2.
Binary Search Example
low high
middle
Searching for 18.
17
Binary Search Example
low high
middle
2
0
Sorting Algorithms
Bubble Sort
SelectionSort
Insertion Sort
Sorting Algorithm isanalgorithmmadeupofaseriesofinstructionsthattakesan
array asinput,andoutputsasortedarray.
Therearemanysortingalgorithms,suchas:
SelectionSort, BubbleSort, InsertionSort, Merge Sort, Heap Sort,
QuickSort,RadixSort,CountingSort,BucketSort,ShellSort,CombSort,
PigeonholeSort,Cycle Sort
Analysis and Design of Algorithms
Bubble
Sort
5 1 4 2
Compare
5 1 4 2
j j+1
Swap
1 5 4 2
j j+1
Compare
1 5 4 2
j j+1
Swap
1 4 5 2
j j+1
Compare
1 4 5 2
j j+1
Swap
1 4 2 5
j j+1
Compare
1 4 2 5
j j+1
Compare
1 4 2 5
j j+1
Swap
1 2 4 5
j j+1
Compare
1 2 4 5
j j+1
1 2 3 4
a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63
c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63
a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63
c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63
Exampleof worstcase
5 4 3 2 1
8 12 5 9 2
8 12 5 9 2
i j
min
8 12 5 9 2
i j
min
8 12 5 9 2
i j
min
8 12 5 9 2
i min j
8 12 5 9 2
i min j
8 12 5 9 2
i j
min
8 12 5 9 2
i min
8 12 5 9 2
i min
Un Sorted
2 12 5 9 8
Sorted Un Sorted
2 12 5 9 8
Sorted i j
min
2 12 5 9 8
Sorted i j
min
2 12 5 9 8
Sorted i min j
2 12 5 9 8
Sorted i min j
2 12 5 9 8
Sorted i min
2 12 5 9 8
Sorted i min
Un Sorted
2 5 12 9 8
Sorted Un Sorted
2 5 12 9 8
Sorted i j
min
2 5 12 9 8
Sorted i j
min
2 5 12 9 8
Sorted i min j
2 5 12 9 8
Sorted i j
min
2 5 12 9 8
Sorted i min
2 5 12 9 8
Sorted i min
Un Sorted
2 5 8 9 12
Sorted Un Sorted
2 5 8 9 12
Sorted i j
min
Un Sorted
2 5 8 9 12
Sorted Un Sorted
Un Sorted
2 5 8 9 12
Sorted i
min
2 5 8 9 12
Sorted
a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63
c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63
a) 2 4 9 13 18 37 45 63
b) 2 9 4 13 18 37 45 63
c) 13 2 4 9 18 45 37 63
d) 2 4 9 13 18 45 37 63
Exampleof worstcase
2 3 4 5 1
5 1 4 2
Store= 1
5 1 4 2
j i
j+1
Store= 1
5 4 2
j i
j+1
Store=
1 5 4 2
j+1 i
Store= 4
1 5 4 2
j i
j+1
Store= 4
1 5 2
j i
j+1
Store= 4
1 5 2
j j+1 i
Store=
1 4 5 2
j j+1 i
Store= 2
1 4 5 2
j i
j+1
Store= 2
1 4 5
j i
j+1
Store= 2
1 4 5
j j+1 i
Store= 2
1 4 5
j j+1 i
Store= 2
1 4 5
j j+1 i
Store=
1 2 4 5
j j+1 i
1 2 4 5
a) 3 7 5 1 9 8 4 6
b) 1 3 7 5 9 8 4 6
c) 3 4 1 5 6 8 7 9
d) 1 3 4 5 6 7 8 9
a) 3 7 5 1 9 8 4 6
b) 1 3 7 5 9 8 4 6
c) 3 4 1 5 6 8 7 9
d) 1 3 4 5 6 7 8 9
a) 3 5 7 1 9 8 4 6
b) 1 3 7 5 9 8 4 6
c) 3 4 1 5 6 8 7 9
d) 1 3 4 5 6 7 8 9
a) 3 5 7 1 9 8 4 6
b) 1 3 7 5 9 8 4 6
c) 3 4 1 5 6 8 7 9
d) 1 3 4 5 6 7 8 9
Exampleof worstcase
5 4 3 2 1
1 4 3 2 3 5 2
1 4 3 2 3 5 2
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 0 0 0 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 0 0 0 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 0 0 0 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 0 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 0 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 0 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 0 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 0 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 1 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 1 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 0 1 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 1 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 1 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 0
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 1
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 1
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 1 2 1 1
Analysis and Design of Algorithms
Create a countarray tostorethecountof eachunique object:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 2 2 1 1
Analysis and Design of Algorithms
Modify countarray by adding theprevious number:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 2 2 1 1
Analysis and Design of Algorithms
Modify countarray by adding theprevious number:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 2 2 1 1
Analysis and Design of Algorithms
Modify countarray by adding theprevious number:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 2 1 1
Analysis and Design of Algorithms
Modify countarray by adding theprevious number:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 1 1
Analysis and Design of Algorithms
Modify countarray by adding theprevious number:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 1
Analysis and Design of Algorithms
Modify countarray by adding theprevious number:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
1 2 3 4 5 6 7
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
1 2 3 4 5 6 7
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
1 2 3 4 5 6 7
1 4 3 2 3 5 2
0 1 2 3 4 5
0 1 3 5 6 7
1 2 3 4 5 6 7
1
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 6 7
1 2 3 4 5 6 7
1
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 6 7
1 2 3 4 5 6 7
1
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7
1 2 3 4 5 6 7
1 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7
1 2 3 4 5 6 7
1 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7
1 2 3 4 5 6 7
1 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 5 5 7
1 2 3 4 5 6 7
1 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7
1 2 3 4 5 6 7
1 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7
1 2 3 4 5 6 7
1 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7
1 2 3 4 5 6 7
1 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 3 4 5 7
1 2 3 4 5 6 7
1 2 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7
1 2 3 4 5 6 7
1 2 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7
1 2 3 4 5 6 7
1 2 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7
1 2 3 4 5 6 7
1 2 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 4 5 7
1 2 3 4 5 6 7
1 2 3 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7
1 2 3 4 5 6 7
1 2 3 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7
1 2 3 4 5 6 7
1 2 3 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7
1 2 3 4 5 6 7
1 2 3 3 4
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 7
1 2 3 4 5 6 7
1 2 3 3 4 5
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6
1 2 3 4 5 6 7
1 2 3 3 4 5
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6
1 2 3 4 5 6 7
1 2 3 3 4 5
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6
1 2 3 4 5 6 7
1 2 3 3 4 5
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 2 3 5 6
1 2 3 4 5 6 7
1 2 2 3 3 4 5
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 1 3 5 6
1 2 3 4 5 6 7
1 2 2 3 3 4 5
Analysis and Design of Algorithms
Output each object from the input sequence followed
by decreasing itscountby 1:
1 4 3 2 3 5 2
0 1 2 3 4 5
0 0 1 3 5 6
1 2 3 4 5 6 7
1 2 2 3 3 4 5
Analysis and Design of Algorithms
Array is now sorted
1 2 2 3 3 4 5
Example 2: Reduce 0 2 2 4 4
2 4 1 1 3 1 2 3 4 5
Range=[0-4] Output 1 2 4
0 1 2 3 4
0 1 2 3 4
Count 0 2 1 1 1 Reduce 0 1 2 4 4
0 1 2 3 4 1 2 3 4 5
Add 0 2 3 4 5 Output 1 1 2 4
1 2 3 4 5 0 1 2 3 4
Output 2 Reduce 0 0 2 4 4
0 1 2 3 4 1 2 3 4 5
Reduce 0 2 2 4 5 Output 1 1 2 3 4
1 2 3 4 5 1 2 3 4 5
Output
Analysis and Design of Algorithms
2 4 Sorted 1 1 2 3 4
Time Complexity: O(n+k) wheren isthenumberof elementsin input
array, and kistherange of input.
Exampleof worstcase
Rangebetween1 to 10K
10 5 10k 5k 200
Analysis and Design of Algorithms
Radix
Sort
Theidea of Radix Sort is todo digit by digit sort starting from least
significant digit to mostsignificant digit. Radix sort uses counting
sortasa subroutine to sort.
Step3 : Repeatthesortwitheachmoresignificantdigit
170 45 75 90 802 24 2 66
170 45 75 90 802 24 2 66
170 90 802 2 24 45 75 66
802 2 24 45 66 170 75 90
2 24 45 66 75 90 170 802
Analysis and Design of Algorithms
Step1: Sorting by leastsignificant digit (1splace)
170 45 75 90 802 24 2 66
170 90 802 2 24 45 75 66
170 90 802 2 24 45 75 66
802 2 24 45 66 170 75 90
802 2 24 45 66 170 75 90
2 24 45 66 75 90 170 802
2 24 45 66 75 90 170 802
1 2 3 1 2 3 1 2 3 1 2 3
5 8 3 5 8 3 6 2 5 1 5 4
1 5 4 1 5 4 1 5 4 4 5 6
5 6 7 1s 6 2 5 10s 4 5 6 100s 5 6 7
6 8 9 4 5 6 5 6 7 5 8 3
6 2 5 5 6 7 5 8 3 6 2 5
4 5 6 6 8 9 6 8 9 6 8 9
Analysis and Design of Algorithms
Time Complexity: O(n+k/d) wheren is thenumber of
elementsin input array, kis therange of input, and d
isnumberof digits.
85 24 63 45 17 31 96 50
85 24 63 45 17 31 96 50
85 24 63 45 17 31 96 50
85 24 63 45 17 31 96 50
85 24 63 45 17 31 96 50
85 24 63 45 17 31 96 50
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 85
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 85
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 85 45 63
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 85 45 63
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 85 45 63 17 31
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 85 45 63 17 31
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 85 45 63 17 31 50 96
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 85 45 63 17 31 50 96
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24
24 85 45 63 17 31 50 96
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 45
24 85 45 63 17 31 50 96
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 45 63
24 85 45 63 17 31 50 96
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 45 63 85
24 85 45 63 17 31 50 96
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
24 45 63 85 17 31 50 96
24 85 45 63 17 31 50 96
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Sort&Merge
17 24 31 45 50 63 85 96
24 45 63 85 17 31 50 96
24 85 45 63 17 31 50 96
85 24 63 45 17 31 96 50
Analysis and Design of Algorithms
Array is now sorted
17 24 31 45 50 63 85 96
i. The first major step involves transforming the complete tree into a heap.
ii. The second major step is to perform the actual sort by extracting the
largest or lowerst element from the root and transforming the remaining
tree into a heap.
Types of heap
Max Heap
Min Heap
Max Heap Example
19
12 16
1 4 7
19 12 16 1 4 7
Array A
Min heap example
1
4 16
7 12 19
1 4 16 7 12 19
Array A
1-Max heap :
max-heap Definition:
is a complete binary tree in which the value in each internal
node is greater than or equal to the values in the children of
that node.
Max-heap property:
The key of a node is ≥ than the keys of its children.
Max heap Operation
A heap can be stored as an array A.
Root of tree is A[1]
Left child of A[i] = A[2i]
Right child of A[i] = A[2i + 1]
Parent of A[i] = A[ i/2 ]
21
2
4 1 3 2 16 9 10 14 8 7
Example Explaining : A
i=5 i=4 i=3
1 1 1
4 4 4
2 3 2 3 2 3
1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
2 9 10
16 9 10 8
2 9 10
16 9 10 8 14 16 9 10
8 9 10
14 8 7 14 8 7 2 8 7
i=2 i=1
1 1 1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
14 9 10
16 9 3 8
14 9 10
7 9 3 8 8 7 9 3
8 9 10
2 8 7 2 8 1 2 4 1
21
3
Build Max-heap
Heapify() Example
16
4 10
14 7 9 3
2 8 1
A= 16 4 10 14 7 9 3 2 8 1
David Luebke
214
11/20/2017
Heapify() Example
16
4 10
14 7 9 3
2 8 1
A= 16 4 10 14 7 9 3 2 8 1
David Luebke
215
11/20/2017
Heapify() Example
16
4 10
14 7 9 3
2 8 1
A= 16 4 10 14 7 9 3 2 8 1
David Luebke
216
11/20/2017
Heapify() Example
16
14 10
4 7 9 3
2 8 1
A= 16 14 10 4 7 9 3 2 8 1
David Luebke
217
11/20/2017
Heapify() Example
16
14 10
4 7 9 3
2 8 1
A= 16 14 10 4 7 9 3 2 8 1
David Luebke
218
11/20/2017
Heapify() Example
16
14 10
4 7 9 3
2 8 1
A= 16 14 10 4 7 9 3 2 8 1
David Luebke
219
11/20/2017
Heapify() Example
16
14 10
8 7 9 3
2 4 1
A= 16 14 10 8 7 9 3 2 4 1
David Luebke
220
11/20/2017
Heapify() Example
16
14 10
8 7 9 3
2 4 1
A= 16 14 10 8 7 9 3 2 4 1
David Luebke
221
11/20/2017
Heapify() Example
16
14 10
8 7 9 3
2 4 1
A= 16 14 10 8 7 9 3 2 4 1
David Luebke
222
11/20/2017
Heap-Sort
sorting strategy:
1. Build Max Heap from unordered array;
2. Find maximum element A[1];
3. Swap elements A[n] and A[1] :
now max element is at the end of the array! .
4. Discard node n from heap
(by decrementing heap-size variable).
5. New root may violate max heap property, but its children are max heaps.
Run max_heapify to fix this.
6. Go to Step 2 unless heap is empty.
HeapSort() Example
A = {16, 14, 10, 8, 7, 9, 3, 2, 4, 1}
1
16
2 3
14 10
4 5 6 7
8 7 9 3
8 9 10
2 4 1
20
HeapSort() Example
A = {14, 8, 10, 4, 7, 9, 3, 2, 1, 16}
1
14
2 3
8 10
4 5 6 7
4 7 9 3
8 9
2 1 16
i = 10
21
HeapSort() Example
A = {10, 8, 9, 4, 7, 1, 3, 2, 14, 16}
1
10
2 3
8 9
4 5 6 7
4 7 1 3
8
2 14 16
i=9 10
22
HeapSort() Example
A = {9, 8, 3, 4, 7, 1, 2, 10, 14, 16}
1
9
2 3
8 3
4 5 6 7
4 7 1 2
10 14 16
i=8 9 10
23
HeapSort() Example
A = {8, 7, 3, 4, 2, 1, 9, 10, 14, 16}
1
8
2 3
7 3
4 5 6
4 2 1 9
i=7
10 14 16
8 9 10
24
HeapSort() Example
A = {7, 4, 3, 1, 2, 8, 9, 10, 14, 16}
1
7
2 3
4 3
4 5
1 2 8 9
i=6 7
10 14 16
8 9 10
25
HeapSort() Example
A = {4, 2, 3, 1, 7, 8, 9, 10, 14, 16}
1
4
2 3
2 3
4 i=5
1 7 8 9
6 7
10 14 16
8 9 10
26
HeapSort() Example
A = {3, 2, 1, 4, 7, 8, 9, 10, 14, 16}
1
3
2 3
2 1
i=4 4 7 8 9
5 6 7
10 14 16
8 9 10
27
HeapSort() Example
A = {2, 1, 3, 4, 7, 8, 9, 10, 14, 16}
1
2
2 i=3
1 3
4
4 7 8 9
5 6 7
10 14 16
8 9 10
28
HeapSort() Example
A = {1, 2, 3, 4, 7, 8, 9, 10, 14, 16} >>orederd
1
1
i =2 3
2 3
4
4 7 8 9
5 6 7
10 14 16
8 9 10
29
Heap Sort pseducode
Heapsort(A as array)
BuildHeap(A)
for i = n to 1 swap(A[1], A[i]) n
= n - 1 Heapify(A, 1)
B u i l d H e a p ( A a s a r r a y ) n =
elements_in(A) for i = f loor(n/2)
to 1
Heapify(A,i)
Heapify(A as array, i as int) left = 2i
right = 2i+1
Min-heap property:
The key of a node is <= than the keys of its children.
Min heap Operation
A heap can be stored as an array A.
Root of tree is A[0]
Left child of A[i] = A[2i+1]
Right child of A[i] = A[2i + 2]
Parent of A[i] = A[( i/2)-1]
33
Min Heap
19
12 16
1 4 7
19 12 16 1 4 7
Array A
Min Heap phase 1
19
1 16
12 4 7
Min Heap phase 1
19 7
12 4 16
Min Heap phase 1
4 7
12 19 16
1,
Min Heap phase 2
16
4 7
12 19
Min Heap phase 2
16 7
12 19
Min Heap phase 2
12 7
16 19
1,4
Min Heap phase 3
19
12 7
16
Min Heap phase 3
12 19
16
1,4,7
Min Heap phase 4
16
12 19
Min Heap phase 4
12
16 19
1,4,7,12
Min Heap phase 5
19
16
Min Heap phase 5
16
19
1,4,7,12,16
Min Heap phase 7
19
1,4,7,12,16,19
Min heap final tree
1
4 7
12 16 19
1 4 7 12 16 19
Array A
Insertion
Algorithm
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
General strategy is percolate up (or bubble up): if the parent of the
element is smaller than the element, then interchange the parent and
child.
OR
12 16 12 16
4 7 1 4 7 17
1
Insert 17
19
12 17
swap
1 4 7 16
The memory efficiency of the heap sort, unlike the other n log n sorts, is constant,
O(1), because the heap sort algorithm is not recursive.