Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Merge Sort, Quick Sort-Algorithm Design and Analysis for Sorting-Lecture Slides, Slides of Design and Analysis of Algorithms

This lecture is part of lecture series for Design and Analysis of Algorithms course. This course was taught by Dr. Bhaskar Sanyal at Maulana Azad National Institute of Technology. It includes: Sorting, Algorithms, Merge, Quick, Divide, Conquer, Base, Case, Subproblems, Recursive, Combine, Array

Typology: Slides

2011/2012

Uploaded on 07/11/2012

dharmadaas
dharmadaas ๐Ÿ‡ฎ๐Ÿ‡ณ

4.3

(55)

267 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sorting Algorithms
Merge Sort
Quick Sort
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Merge Sort, Quick Sort-Algorithm Design and Analysis for Sorting-Lecture Slides and more Slides Design and Analysis of Algorithms in PDF only on Docsity!

Sorting Algorithms

Merge Sort

Quick Sort

2

Overview

๏ฌ Divide and Conquer

๏ฌ Merge Sort

๏ฌ Quick Sort

4

Divide and Conquer - Sort

Problem:

๏ฌ Input: A[left..right] โ€“ unsorted array of integers

๏ฌ Output: A[left..right] โ€“ sorted in non-decreasing

order

5

Divide and Conquer - Sort

1. Base case

at most one element (left โ‰ฅ right), return

2. Divide A into two subarrays: FirstPart, SecondPart

Two Subproblems:

sort the FirstPart sort the SecondPart

3. Recursively

sort FirstPart sort SecondPart

4. Combine sorted FirstPart and sorted SecondPart

7

Merge Sort: Idea

Merge

Recursively sort

Divide into two halves

FirstPart (^) SecondPart

FirstPart SecondPart

A

A is sorted!

8

Merge Sort: Algorithm

Merge-Sort (A, left, right)

if left โ‰ฅ right return

else

middle โ† b(left+right)/2๏ƒป

Merge-Sort (A, left, middle)

Merge-Sort (A, middle+1, right)

Merge (A, left, middle, right)

Recursive Call

10

L: R:

Temporary Arrays

Merge-Sort: Merge Example

A: 2 3 7 8 1 4 5 6

11

Merge-Sort: Merge Example

L:

A:

R:

i=0 (^) j=

k=

13

Merge-Sort: Merge Example

L:

A:

R:

i=

k=

j=

14

Merge-Sort: Merge Example

L:

A:

R:

i=2 j=

k=

16

Merge-Sort: Merge Example

L:

A:

R:

i=2 (^) j=

k=

17

Merge-Sort: Merge Example

L:

A:

R:

k=

i=2 (^) j=

19

Merge-Sort: Merge Example

L:

A:

R:

i=4 j=

k=

20

Merge(A, left, middle, right)

**1. n 1 โ† middle โ€“ left + 1

  1. n 2 โ† right โ€“ middle
  2. create array L[n 1 ], R[n 2 ]
  3. for i โ† 0 to n 1 - 1 do L[i] โ† A[left +i]
  4. for j โ† 0 to n 2 - 1 do R[j] โ† A[middle+j]
  5. k โ† i โ† j โ† 0
  6. while i < n 1 & j < n 2
  7. if L[i] < R[j]
  8. A[k++] โ† L[i++]
  9. else
  10. A[k++] โ† R[j++]
  11. while i < n 1
  12. A[k++] โ† L[i++]
  13. while j < n 2
  14. A[k++] โ† R[j++]** (^) n = n 1 +n 2

Space: n Time : cn for some constant c