Partitioning in Quicksort How do we partition inplace

  • Slides: 19
Download presentation
Partitioning in Quicksort How do we partition in-place efficiently? n n n Q Partition

Partitioning in Quicksort How do we partition in-place efficiently? n n n Q Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n C Partition

Partitioning in Quicksort How do we partition in-place efficiently? n n n C Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n C Partition

Partitioning in Quicksort How do we partition in-place efficiently? n n n C Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n C Partition

Partitioning in Quicksort How do we partition in-place efficiently? n n n C Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n C Partition

Partitioning in Quicksort How do we partition in-place efficiently? n n n C Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n C Partition

Partitioning in Quicksort How do we partition in-place efficiently? n n n C Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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? n n n Partition element

Partitioning in Quicksort How do we partition in-place efficiently? n n n Partition element = rightmost element. Scan from left for larger element. Scan from right for smaller element. Exchange. Repeat until pointers cross. 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