Design and Analysis of Algorithms PowerPoint Presentation

Design  and Analysis of Algorithms PowerPoint Presentation

2018-12-24 8K 8 0 0

Description

Quicksort. Haidong. . Xue. Summer 2012, at GSU. Review of insertion sort and merge sort. Insertion sort. Algorithm. Worst case number of comparisons = O(?). Merge sort. Algorithm. Worst case number of comparisons = O(?). ID: 745212

Embed code:

Download this presentation



DownloadNote - The PPT/PDF document "Design and Analysis of Algorithms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Presentations text content in Design and Analysis of Algorithms

Slide1

Design and Analysis of AlgorithmsQuicksort

Haidong

Xue

Summer 2012, at GSU

Slide2

Review of insertion sort and merge sortInsertion sortAlgorithmWorst case number of comparisons = O(?)Merge sortAlgorithmWorst case number of comparisons = O(?)

Slide3

Sorting Algorithms

Algorithm

Worst Time

Expected Time

Extra Memory

Insertion sortO(1)Merge sortO(n)Quick sort O(1)Heap sortO(1)

Algorithm

Worst Time

Expected Time

Extra Memory

Insertion sort

O(1)

Merge sort

O(n)

Quick

sort

O(1)

Heap sort

O(1)

Slide4

Quicksort AlgorithmInput: A[1, …, n] Output: A[1, .., n], where A[1]<=A[2]…<=A[n]Quicksort:if(n<=1) return

;

Choose the pivot p = A[

n

]

Put all elements less than p on the left; put all elements lager than p on the right; put p at the middle. (Partition) Quicksort(the array on the left of p) Quicksort(the array on the right of p)

Slide5

Quicksort AlgorithmQuicksort example

2

8

7

1

356428713564287

1

3

5

6

4

Current pivots

Previous pivots

Quicksort

Hi, I am nothing

Nothing Jr.

Nothing 3rd

2

8

7

1

3

5

6

4

2

8

7

1

3

5

6

4

2

8

7

1

3

5

6

4

Slide6

Quicksort AlgorithmMore detail about partitionInput: A[1, …, n] (p=A[n])Output: A[1,…k-1, k, k+1, … n], where A[1, …, k-1]<A[k] and A[k+1, … n] > A[k], A[k]=pPartition:

t = the tail of smaller array

from

i

= 1 to n-1{

if(A[i]<p) { exchange A[t+1] with A[i]; update t to the new tail; }exchange A[t+1] with A[n];

Slide7

Quicksort AlgorithmPartition example

2

8

7

1

356428713564287

1

3

5

6

4

tail

2

8

7

1

3

5

6

4

tail

tail

tail

Exchange 2 with A[tail+1]

Do nothing

Do nothing

Slide8

Quicksort AlgorithmPartition example

2

8

7

1

356428713564287

1

3

5

6

4

tail

tail

Exchange 1 with A[tail+1]

tail

Exchange 3 with A[tail+1]

Do nothing

2

8

7

1

3

5

6

4

tail

Do nothing

2

8

7

1

3

5

6

4

Final step: exchange A[n] with A[tail+1]

Slide9

Quicksort AlgorithmThe final version of quick sort:Quicksort(A, p, r){ if(p<r){ //if(n

<=1

)

return

;

q = partition(A, p, r); //small ones on left; //lager ones on right Quicksort(A, p, q-1); Quicksort(A, q+1, r); }}

Slide10

Analysis of QuicksortTime complexityWorst caseExpectedSpace complexity - extra memory0 = O(1)

Slide11

Analysis of QuicksortWorst caseThe most unbalanced one ---

Is it the worst case?

Strict proof

Expected time complexity

Strict proof

 

Slide12

Strict proof of the worst case time complexity

Proof that

When n=1,

(1)=

Hypothesis: when induction statement: when  

Slide13

Strict proof of the worst case time complexity

Since

,

+

 

Slide14

Strict proof of the expected time complexityGiven A[1, …, n], after sorting them to

, the chance for 2 elements, A[

i

] and A[j], to be compared is

.

The total comparison is calculated as: 

Slide15

Java implementation of QuicksortProfessional programmers DO NOT implement sorting algorithms by themselvesWe do it for practicing algorithm implementation techniques and understanding those algorithmsCode quicksortSort.java // the abstract classQuicksort_Haydon.java // my quicksort implementation

SortingTester.java // correctness tester

Slide16


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.