Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Quicksort Algorithm: Department of Computer and Information Science, School of Science, IUPUI

The document discusses Quicksort, an efficient sorting algorithm invented in 1960 by C. A. R. Hoare. It is a divide and conquer algorithm that works by partitioning an array around a pivot element and recursively sorting the subarrays. The key step is partitioning the array such that elements smaller than the pivot are moved to the left and larger elements to the right. Quicksort has average case performance of O(n log n) time and O(log n) space and is one of the most widely used sorting algorithms.

Uploaded by

Ajith Cristiano
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Quicksort Algorithm: Department of Computer and Information Science, School of Science, IUPUI

The document discusses Quicksort, an efficient sorting algorithm invented in 1960 by C. A. R. Hoare. It is a divide and conquer algorithm that works by partitioning an array around a pivot element and recursively sorting the subarrays. The key step is partitioning the array such that elements smaller than the pivot are moved to the left and larger elements to the right. Quicksort has average case performance of O(n log n) time and O(log n) space and is one of the most widely used sorting algorithms.

Uploaded by

Ajith Cristiano
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Department of Computer and Information Science, School of Science, IUPUI

Quicksort

Algorithm

Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

Dale Roberts

Dale Roberts

Dale Roberts

Dale Roberts

Notes on Quicksort
Quicksort was invented in 1960 by C. A. R. Hoare. Quicksort is more widely used than any other sort. Quicksort is well-studied, not difficult to implement, works well on a variety of data, and consumes fewer resources that other sorts in nearly all situations. Quicksort is O(n*log n) time, and O(log n) additional space due to recursion.

Dale Roberts

Notes on Quicksort
Quicksort has withstood the test of time. It has been thoroughly analyzed. The analysis has been verified through extensive empirical experience. Quicksort is not stable. Quicksort performance can degenerate under special circumstances. It is possible modify the algorithm to handle these cases, but at the expense of making the algorithm more complicated. Sedgewick states that tuning Quicksort is the better mousetrap of computer science. Many ideas have been tried, but its easy to be deceived because the algorithm is so balanced that a perceived improvement in one area can be more than offset by poor performance in another area.
Dale Roberts

Quicksort Algorithm
Quicksort is a divide-and-conquer method for sorting. It works by partitioning an array into parts, then sorting each part independently. The crux of the problem is how to partition the array such that the following conditions are true:
There is some element, a[i], where a[i] is in its final position. For all l < i, a[l] < a[i]. For all i < r, a[i] < a[r].

Dale Roberts

Quicksort Algorithm (cont)


As is typical with a recursive program, once you figure out how to divide your problem into smaller subproblems, the implementation is amazingly simple. int partition(Item a[], int l, int r); void quicksort(Item a[], int l, int r) { int i; if (r <= l) return; i = partition(a, l, r); quicksort(a, l, i-1); quicksort(a, i+1, r); }

Dale Roberts

Dale Roberts

Dale Roberts

10

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross

O
left
right

partition element

unpartitioned
partitioned

Dale Roberts

11

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
12

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
13

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
14

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me swap me

partition element

unpartitioned partitioned

left right
15

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross

partition element

unpartitioned partitioned

left right
16

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
17

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
18

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me swap me

partition element

unpartitioned partitioned

left right
19

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross

partition element

unpartitioned partitioned

left right
20

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross

partition element

unpartitioned partitioned

left right
21

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross

partition element

unpartitioned partitioned

left right
22

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross

partition element

unpartitioned partitioned

left right
23

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
24

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
25

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
26

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
swap me

partition element

unpartitioned partitioned

left right
27

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
pointers cross swap with partitioning element

partition element

unpartitioned partitioned

left right
28

Dale Roberts

Partitioning in Quicksort
How do we partition the array efficiently?
choose partition element to be rightmost element scan from left for larger element scan from right for smaller element exchange repeat until pointers cross
partition is complete

partition element

unpartitioned partitioned

left right
29

Dale Roberts

Partitioning in Quicksort
int partition(Item a[], int l, int r) { int i = l-1, j = r; Item v = a[r]; for (;;) { while (less(a[++i], v)) ; while (less(v, a[--j])) if (j == l) break; if (i >= j) break; exch(a[i], a[j]); } exch(a[i], a[r]); return i;

Dale Roberts

30

Quicksort Demo
Quicksort illustrates the operation of the basic algorithm. When the array is partitioned, one element is in place on the diagonal, the left subarray has its upper corner at that element, and the right subarray has its lower corner at that element. The original file is divided into two smaller parts that are sorted independently. The left subarray is always sorted first, so the sorted result emerges as a line of black dots moving right and up the diagonal.

Dale Roberts

31

You might also like