Sorting - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Sorting

Description:

The algorithms are trivial to modify if we want to sort into descending order ... Pat. Sam. 90. 98. 98. 86. 75. 86. 90. stably sorted. Unstable sort algorithms ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 23
Provided by: davidma
Category:
Tags: pat | sorting

less

Transcript and Presenter's Notes

Title: Sorting


1
Sorting
2
Sorting
  • When we just say sorting, we mean in ascending
    order (smallest to largest)
  • The algorithms are trivial to modify if we want
    to sort into descending order
  • There are many, many sorting algorithms
  • Some are much faster than others

3
Stable sort algorithms
  • A stable sort keeps equal elements in the same
    order
  • This may matter when you are sorting data
    according to some characteristic
  • Example sorting students by test scores

4
Unstable sort algorithms
  • An unstable sort may or may not keep equal
    elements in the same order
  • Stability is usually not important, but sometimes
    it is important

5
Selection sort I (p. 49)
  • To sort aleft...right
  • 1. for l left, ..., right-1, repeat
  • 1.1. Set p so that ap is the least of
    al...right
  • 1.2. If p ! l, swap ap and al
  • 2. Terminate
  • Is this sort stable?
  • How fast is this sort?

6
Selection sort II
  • 1. for l left, ..., right-1, repeat
  • 1.1. Set p so that ap is the least of
    al...right
  • 1.2. If p ! l, swap ap and al
  • 2. Terminate
  • Notice that step 1.1 conceals a loop
  • At first, it loops through (almost) the entire
    array
  • Near the end, it loops through very few locations
  • The average is about 1/2 the array length (1/2 n)
  • The outer loop (step 1) says to do this n times
  • Hence time is (1/2 n)n, , that is, O(n2)

7
Selection sort III
  • For selection sort, the best case, average case,
    and worst case are all O(n2)
  • We always go through the outer loop O(n) times
  • Each time we get to the inner loop, we execute it
    on average n/2 times this is also O(n)
  • O(n) O(n) O(n2)
  • This is not a very fast sorting algorithm!

8
Quicksort I (p. 56)
  • To sort aleft...right
  • 1. if left lt right
  • 1.1. Partition aleft...right such that
  • all aleft...p-1 are less than ap, and
  • all ap1...right are gt ap
  • 1.2. Quicksort aleft...p-1
  • 1.3. Quicksort ap1...right
  • 2. Terminate

9
Partitioning (Quicksort II)
  • A key step in the Quicksort algorithm is
    partitioning the array
  • We choose some (any) number p in the array to use
    as a pivot
  • We partition the array into three parts

10
Partitioning II
  • My partitioning algorithm differs slightly from
    the one in the textbook
  • Heres the basic idea
  • Choose an array value (say, the first) to use as
    the pivot
  • Starting from the left end, find the first
    element that is greater than or equal to the
    pivot
  • Searching backward from the right end, find the
    first element that is less than the pivot
  • Interchange (swap) these two elements
  • Repeat, searching from where we left off, until
    done

11
Partitioning
  • To partition aleft...right
  • 1. Set p aleft, l left 1, r right
  • 2. while l lt r, do
  • 2.1. while l lt right al lt p, set l l 1
  • 2.2. while r gt left ar gt p, set r r - 1
  • 2.3. if l lt r, swap al and ar
  • 3. Set aleft ar, ar p
  • 4. Terminate

12
Example of partitioning
  • choose pivot 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
  • search 4 3 6 9 2 4 3 1 2 1 8 9 3 5 6
  • swap 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6
  • search 4 3 3 9 2 4 3 1 2 1 8 9 6 5 6
  • swap 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6
  • search 4 3 3 1 2 4 3 1 2 9 8 9 6 5 6
  • swap 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6
  • search 4 3 3 1 2 2 3 1 4 9 8 9 6 5 6 (left gt
    right)
  • swap with pivot 1 3 3 1 2 2 3 4 4 9 8 9 6 5 6

13
Partitioning in Java
static void partition(int a) int left
0, right a.length - 1 int p aleft, l
left 1, r right while (l lt r)
while (l lt right al lt p) l while
(r gt left ar gt p) r-- if (l lt r)
int temp al al ar ar
temp aleft ar
ar p
14
Analysis of quicksortbest case
  • Suppose each partition operation divides the
    array almost exactly in half
  • Then the depth of the recursion in log2n
  • Because thats how many times we can halve n
  • However, there are many recursions!
  • How can we figure this out?
  • We note that
  • Each partition is linear over its subarray
  • All the partitions at one level cover the array

15
Partitioning at various levels
16
Best case II
  • So the depth of the recursion in log2n
  • At each level of the recursion, all the
    partitions at that level do work that is linear
    in n
  • O(log2n) O(n) O(n log2n)
  • Hence in the average case, quicksort has time
    complexity O(n log2n)
  • What about the worst case?

17
Worst case
  • In the worst case, partitioning always divides
    the size n array into these three parts
  • A length one part, containing the pivot itself
  • A length zero part, and
  • A length n-1 part, containing everything else
  • We dont recur on the zero-length part
  • Recurring on the length n-1 part requires (in the
    worst case) recurring to depth n-1

18
Worst case partitioning
19
Worst case for quicksort
  • In the worst case, recursion may be n levels deep
    (for an array of size n)
  • But the partitioning work done at each level is
    still n
  • O(n) O(n) O(n2)
  • So worst case for Quicksort is O(n2)
  • When does this happen?
  • When the array is sorted to begin with!

20
Typical case for quicksort
  • If the array is sorted to begin with, Quicksort
    is terrible O(n2)
  • It is possible to construct other bad cases
  • However, Quicksort is usually O(n log2n)
  • The constants are so good that Quicksort is
    generally the fastest algorithm known
  • Most real-world sorting is done by Quicksort

21
Tweaking Quicksort
  • Almost anything you can try to improve
    Quicksort will actually slow it down
  • One good tweak is to switch to a different
    sorting method when the subarrays get small (say,
    10 or 12)
  • Quicksort has too much overhead for small array
    sizes
  • For large arrays, it might be a good idea to
    check beforehand if the array is already sorted

22
The End
Write a Comment
User Comments (0)
About PowerShow.com