0% found this document useful (0 votes)
9 views8 pages

CS203 Week5

The document describes algorithms for building a max heap, heap sort, priority queues, and quicksort. It includes pseudocode and explanations of the time complexity for each algorithm. Building a max heap takes O(n) time by calling max-heapify on each node. Heapsort uses a max heap to sort in O(n log n) time by extracting elements from the heap. Priority queues support insert, extract-max, and increase-key operations in O(log n) time using heap operations. Quicksort partitions elements around a pivot in O(n) time and recursively sorts subarrays, resulting in overall O(n log n) time complexity.

Uploaded by

Rajan Kumar
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)
9 views8 pages

CS203 Week5

The document describes algorithms for building a max heap, heap sort, priority queues, and quicksort. It includes pseudocode and explanations of the time complexity for each algorithm. Building a max heap takes O(n) time by calling max-heapify on each node. Heapsort uses a max heap to sort in O(n log n) time by extracting elements from the heap. Priority queues support insert, extract-max, and increase-key operations in O(log n) time using heap operations. Quicksort partitions elements around a pivot in O(n) time and recursively sorts subarrays, resulting in overall O(n log n) time complexity.

Uploaded by

Rajan Kumar
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/ 8

[CS203] Design and Analysis of Algorithms

Course Instructor: Dr. Dibyendu Roy Autumn 2023-24


Scribed by: Diya Garg(202251044) Lectures(Week 5)

Contents

1 Build max-heap 2
1.1 Correctness Of Algorithm . . . . . . . . . . . . . . . . . . . . 3
1.2 Time Complexity . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Heap-Sort (A) 4
2.1 Time Complexity . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Solving Time Complexity . . . . . . . . . . . . . . . . . . . . . 5

3 Priority Queue 5
3.1 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Priority Queue Example . . . . . . . . . . . . . . . . . . . . . 6

4 Quick Sort 7

1
1 Build max-heap

1. A.heapsize = a.length
2. for i = [a.length/2] to 1
3. max-Heapify(A , i)

i=5
4

1 3

2 16 9 10

14 8 7

i=4
4

1 3

14 16 9 10

2 8 7

2
i=3
4

1 10

14 16 9 3

2 8 7

i=2
4

16 10

14 7 9 3

2 8 1

i=1
16

14 10

8 7 9 3

2 4 1

1.1 Correctness Of Algorithm

Initialization: Before the first iteration of the loop, the index i is set to
⌊n/2⌋. At this point, every node from ⌊n/2⌋ + 1 to n represents a leaf node

3
and is trivially the root of a maximum heap.
Maintenance: In each iteration of the loop, the algorithm ensures that
the node at index i and its children satisfy the max-heap property. Since
the children of node i have indices greater than i, and based on our loop
invariant, they are both roots of max-heaps. This condition is crucial for
the MAX-HEAPIFY operation to work correctly and maintain the max-heap
property. After the MAX-HEAPIFY operation, the nodes i + 1, i + 2, . . ., n still
maintain the max-heap property. Decreasing the value of i in the loop update
step ensures that the loop invariant is reestablished for the next iteration.
Termination: When the loop terminates, i becomes 0. According to our
loop invariant, every node from index 1 to n is now the root of a max-heap.
This includes node 1, which satisfies the max-heap property.

1.2 Time Complexity

⌊log n⌋ l
X n m
ch = T (n)
2h+1
h=0
l n m 1

2h+1 2
⌊log n⌋ l ⌊log ⌊log n⌋
X n m Xn⌋ l n m X h
ch ≥ ch = cn
2h+1 2h 2h
h=0 h=0 h=0
∞ 1
X h 2
= cn = = 2cn
h=0
2h (1 − 21 )2

∴ O(n) → Build-Max-Heap

2 Heap-Sort (A)

1. Build-Max-Heap(A)
2. for i=A.length to 2
3. exchange A[1] and A[i]
4. A.heapsize=A.heapsize-1
5. Max-Heapify(A,1)

4
2.1 Time Complexity

T (n) = T (n − 1) + log n
T (n − 1) = T (n − 2) + log n − 1
So similarly, T (n − 1) = T (n − 2) + T (n − 3)
∴ T (n) = T (1) + log(n) + log(n − 1) + log(n − 2) . . . + log 2 = T (1) + log(n!)
∴ T (n) = O(log(n!))

2.2 Solving Time Complexity


Pn Rn
i=1 log i ≈ 1 log x dx
= n log n − n + 1
∴ T (n) = O(log(n!))
can be written as = O(n log n)

3 Priority Queue

Data Structure to maintain set S every element of S is a associated with one


value called key.

3.1 Method

• Insert (x)
S=S∪{x}
• Maximum (S)
return (maximum element of S)
• Extract-Max (S)
return Head-Node
• Increase Key (S,x,key)
Condition: key > S[x]
• Heap-Max (A):
return (A[1])→ runtime:O (1)
• Heap-Extract-Max (A)
exchange A[1] with A[A.heapsize]
A.heapsize=A.heapsize-1→ runtime: O (log n)

5
• Heap-Increase-Key (A,i,key)
if key ≤ A[i]
error (’Key Is Invalid’)
A[i]=key
Compare A[i] with A[Parent (i)] to maintain the Max-Heap property
runtime: O (log n)

3.2 Priority Queue Example

Example : A = 6 , 4 , 3 , 8 , 9 , 16

Step : 1
6

4 3

8 9 16

Step : 2
6

4 16

8 9 3

step : 3
6

9 16

8 4 3

6
Step : 4
16

9 6

8 4 3

Step : 5
16

9 6

8 4 20

Step : 6
16

9 20

8 4 6

Step : 7
20

9 16

8 4 6

7
4 Quick Sort

Algorithm 1 Quick Sort


procedure QuickSort(arr, lef t, right)
if lef t < right then
pivot ← Partition(arr, lef t, right)
QuickSort(arr, lef t, pivot − 1)
QuickSort(arr, pivot + 1, right)
end if
end procedure
procedure Partition(arr, lef t, right)
pivot ← arr[right]
i ← lef t − 1
for j ← lef t to right − 1 do
if arr[j] ≤ pivot then
i←i+1
Swap arr[i] and arr[j]
end if
end for
Swap arr[i + 1] and arr[right]
return i + 1
end procedure

You might also like