Partitioning in Quicksort How do we partition inplace

  • Slides: 19
Download presentation
Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n Q U I C K partition element S O R T I S C O O unpartitioned left partitioned right L 1

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me Q U I C K partition element S O R T I S C O O unpartitioned left partitioned right L 2

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me Q U I C K partition element S O R T I S C O O unpartitioned left partitioned right L 3

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me Q U I C K partition element S O R T I S C O O unpartitioned left partitioned right L 4

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me Q swap me U I C K partition element S O R T I S C O O unpartitioned left partitioned right L 5

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n C U I C K partition element S O R T I S Q O O unpartitioned left partitioned right L 6

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me C U I C K partition element S O R T I S Q O O unpartitioned left partitioned right L 7

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me C U I C K partition element S O R T I S Q O O unpartitioned left partitioned right L 8

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me C U I swap me C K partition element S O R T I S Q O O unpartitioned left partitioned right L 9

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 10

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 11

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 12

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 13

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 14

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 15

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 16

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap me C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 17

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n swap with partitioning element pointers cross C I I C K partition element S O R T U S Q O O unpartitioned left partitioned right L 18

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element.

Partitioning in Quicksort How do we partition in-place efficiently? Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. n n n partition is complete C I I C K partition element L O R T U S Q O O unpartitioned left partitioned right S 19