Sorting Lecture

download Sorting Lecture

of 72

Transcript of Sorting Lecture

  • 8/3/2019 Sorting Lecture

    1/72

    1

    SortingAPS105: Computer Fundamentals

    Jason Anderson

    Dept. of Electrical and Computer Engineering

    Faculty of Applied Science and Engineering

    University of Toronto

  • 8/3/2019 Sorting Lecture

    2/72

    2

    Sorting

    Phonebook useless if names

    were not in alphabetical(sorted) order

  • 8/3/2019 Sorting Lecture

    3/72

    3

    Why Sorting?

    Sorting used in many computer programs: iPod sorts songs based on title or artist.

    Facebook sorts friends in alphabetical order.

    Facebook gives you the most-recent status. Excel spreadsheet can sort column by values.

    Online dating: sort dating matches based on

    how close by they live. Youtube sorts based on popularity.

    .

  • 8/3/2019 Sorting Lecture

    4/72

    4

    Sorting Objective

    Given: list of items (numbers, names, etc). Want to put the items sorted order.

    Alpha order

    Largest-to-smallest Smallest-to-largest

    Darkest colour to lightest colour

    Sometimes there are so many items, weneed computers help to do the sorting!

  • 8/3/2019 Sorting Lecture

    5/72

    5

    What is an Algorithm?

    Set of steps for completing a task.

    You have already been designing

    algorithms in this class. You already use algorithms all the time inyour daily life!

  • 8/3/2019 Sorting Lecture

    6/72

    6

    Algorithm for Baking a Cake

    Cream the butter. Mix the dry ingredients

    separately.

    Combine the dry and wetingredients.

    Mix until smooth.

    Put into baking pan.

    Bake for 30 mins at 350 F. Is cake done?

    If yes, remove from oven.

    If no, bake for another 5 minutes.

  • 8/3/2019 Sorting Lecture

    7/72

    7

    Algorithm Efficiency

    Washer, dryer take 30 mins each. Have one load of light clothes, one load

    dark clothes.

    Algorithm 1:2 pm : Light clothes into

    washer

    2:30 pm: Light clothesinto dryer

    3 pm: Darks into washer3:30 pm: Darks into dryer

    Algorithm 2:2 pm: Light clothes into

    washer

    2:30 pm: Light clothesinto dryer AND

    Darks into washer3 pm: Darks into dryer

    All done at 4 pm! All done at 3:30 pm!

  • 8/3/2019 Sorting Lecture

    8/72

    8

    Sorting Algorithms

    Sequence of steps computer takes forgetting items into the right (sorted) order.

    Many different sorting algorithms invented:

    But they have different efficiencies! Some take more steps to get things into the right

    order.

    Some work better for different types of items.

    Want fast sorting algorithms:

    Sort GTA phonebook with 6 million names.

  • 8/3/2019 Sorting Lecture

    9/72

    9

    Sorting Algorithms

    Research on sorting algorithms: Started in 1950s

    Still active today

    (new algorithm invented in 2004)! Well discuss three classic sorting

    approaches today:

    Bubble sort Insertion sort

    Quicksort

  • 8/3/2019 Sorting Lecture

    10/72

    10

    Bubble Sort

    Walk through the list of numbers,comparing two items at a time. Swap the two items if theyre out of order.

    The biggest item bubbles up to the top.

    Walk the list of numbers several times untilcompletely sorted!

  • 8/3/2019 Sorting Lecture

    11/72

    11

    Bubble Sort

    Sort Children from Shortest to Tallest

  • 8/3/2019 Sorting Lecture

    12/72

    12

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at first two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    13/72

    13

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at NEXT two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    14/72

    14

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at NEXT two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    15/72

    15

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at NEXT two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    16/72

    16

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at NEXT two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    17/72

    17

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at LAST two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    18/72

    18

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at LAST two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    19/72

    19

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at FIRST two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    20/72

    20

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at FIRST two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    21/72

    21

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at NEXT two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    22/72

    22

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at NEXT two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    23/72

    23

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at NEXT two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    24/72

    24

    Bubble Sort

    Sort Children from Shortest to Tallest

    We dont need to look at the last TWO, as in each pass

    the biggest bubbles up to the top spot.

  • 8/3/2019 Sorting Lecture

    25/72

    25

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at FIRST two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    26/72

    26

    Bubble Sort

    Sort Children from Shortest to Tallest

    Look at NEXT two children andswap them, if necessary.

  • 8/3/2019 Sorting Lecture

    27/72

    27

    Bubble Sort

    Sort Children from Shortest to Tallest

    We dont need to look at these TWO, as in each pass

    the biggest bubbles up to the top spot.

  • 8/3/2019 Sorting Lecture

    28/72

    28

    Bubble Sort

    Sort Children from Shortest to Tallest

    We dont need to look at these TWO, as in each pass

    the biggest bubbles up to the top spot.

    Since we didnt make ANY swaps in this pass,

    were DONE!

  • 8/3/2019 Sorting Lecture

    29/72

    29

    Bubble Sort

    How many comparisons?

    Sort 1000 items: 1st pass: 999 comparisons/swaps

    2nd pass: 998 comparisons/swaps

    999th pass: 1 comparison/swap

    Sort n items:

    (n-1)*(n-2)/2 comparison/swaps

  • 8/3/2019 Sorting Lecture

    30/72

    30

    Insertion Sort

    Like sorting playing cards in hand: Draw a first card card is sorted.

    Draw second card compare with first card.

    Draw third card compare with two cards.

    Draw nth

    card

    compare with n-1 cards. Can speed-up using property that hand is

    kept sorted.

  • 8/3/2019 Sorting Lecture

    31/72

    31

    Quicksort

    Invented in 1962 by C. Hoare.

    Much more efficient than previousalgorithms. Fewer steps needed to get items into order.

    Still widely used today.

    Uses recursion.

  • 8/3/2019 Sorting Lecture

    32/72

    32

    Recursion

    Containssmaller

    version ofsame picture

  • 8/3/2019 Sorting Lecture

    33/72

    33

    Recursion in Computing

    Task is broken down into smaller/easiertasks solved in a similar way.

    Ends when we hit a BASE/END case.

  • 8/3/2019 Sorting Lecture

    34/72

    34

    Recursion in Computing

    Task is broken down into smaller/easiertasks solved in a similar way.

    Ends when we hit a BASE/END case.

    Invest $1000 at 8% interest. How much do I have in 3 years?

    Cash(3 years) = Cash(2 years) + 8% x Cash(2 years)

    Cash(2 years) = Cash(1 years) + 8% x Cash(1 years)

    Cash(1 year) = Cash(0 years) + 8% x Cash(0 years) Cash(0 years) = $1000.00

    Cash(3 years) = $1259.71!

  • 8/3/2019 Sorting Lecture

    35/72

    35

    Quicksort Core Action

    Want to sort 10 cards:

    6 3 9 0 2 1 7 4 5 8

    Choose a pivot card: 6

  • 8/3/2019 Sorting Lecture

    36/72

    36

    Quicksort Core Action

    Walk along the cards and partition the cardsinto two groups: Cards smaller than the pivot. Cards larger than the pivot.

    4 3 5 0 2 1 7 8 9 76

    Left partition Right partition

    Pivot card is where

    it should be!Left partition is all less than 6,

    but is still unsorted!

  • 8/3/2019 Sorting Lecture

    37/72

    37

    Quicksort Core Action

    Now take the left partition & repeat action!

    4 3 5 0 2 1

    Choose a pivot card: 4

    Partition the left partition:

    2 3 1 0 2 54

    Pivot is in its

    correct place

  • 8/3/2019 Sorting Lecture

    38/72

    38

    Quicksort Core Action

    Likewise, take the right partition andrepeat the process.

    8 9 7

    Choose a pivot card: 8

    Partition the right partition:

    7 9 98

  • 8/3/2019 Sorting Lecture

    39/72

    39

    Quicksort Overview

    Pivot and partition the left and right half.

  • 8/3/2019 Sorting Lecture

    40/72

    40

    Quicksort Overview

  • 8/3/2019 Sorting Lecture

    41/72

    41

    Quicksort Overview

  • 8/3/2019 Sorting Lecture

    42/72

    42

    Quicksort Overview

  • 8/3/2019 Sorting Lecture

    43/72

    43

    Quicksort Overview

  • 8/3/2019 Sorting Lecture

    44/72

    44

    Quicksort Overview

    Continue until the pieces are so smallthere is nothing to do! SORTED

  • 8/3/2019 Sorting Lecture

    45/72

    45

    Nitty Gritty Details

    How do we do the partitioning?

    6 3 9 0 2 1 7 4 5 8

    6

    LOW HIGH

    Consider two arrows LOW and HIGH.

  • 8/3/2019 Sorting Lecture

    46/72

    46

    Nitty Gritty Details

    How do we do the partitioning?

    6 3 9 0 2 1 7 4 5 8

    6

    LOW HIGH

    Consider two arrows LOW and HIGH. HIGH will march LEFT until it finds a

    number less than PIVOT.

  • 8/3/2019 Sorting Lecture

    47/72

    47

    Nitty Gritty Details

    How do we do the partitioning?

    6 3 9 0 2 1 7 4 5 8

    6

    LOW HIGH

    Consider two arrows LOW and HIGH. HIGH marches LEFT until it finds a number

    less than PIVOT.

  • 8/3/2019 Sorting Lecture

    48/72

    48

    Nitty Gritty Details

    How do we do the partitioning?

    6 3 9 0 2 1 7 4 5 8

    6

    LOW HIGH

    The number at HIGH is moved to theposition LOW.

  • 8/3/2019 Sorting Lecture

    49/72

    49

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 9 0 2 1 7 4 5 8

    6

    LOW HIGH

    The number at HIGH is moved to theposition LOW.

  • 8/3/2019 Sorting Lecture

    50/72

    50

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 9 0 2 1 7 4 5 8

    6

    LOW HIGH

    LOW marches RIGHT until it finds anumber greater than PIVOT

  • 8/3/2019 Sorting Lecture

    51/72

    51

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 9 0 2 1 7 4 5 8

    6

    LOW HIGH

    LOW marches RIGHT until it finds anumber greater than PIVOT

  • 8/3/2019 Sorting Lecture

    52/72

    52

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 9 0 2 1 7 4 5 8

    6

    LOW HIGH

    The number at position LOW is moved toposition HIGH.

  • 8/3/2019 Sorting Lecture

    53/72

    53

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 9 0 2 1 7 4 9 8

    6

    LOW HIGH

    The number at position LOW is moved toposition HIGH.

  • 8/3/2019 Sorting Lecture

    54/72

    54

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 9 0 2 1 7 4 9 8

    6

    LOW HIGH

    HIGH marches left until it finds a numberless than PIVOT.

  • 8/3/2019 Sorting Lecture

    55/72

    55

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 9 0 2 1 7 4 9 8

    6

    LOW HIGH

    HIGH marches left until it finds a numberless than PIVOT.

  • 8/3/2019 Sorting Lecture

    56/72

    56

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 9 0 2 1 7 4 9 8

    6

    LOW HIGH

    The number at position HIGH is moved toposition LOW.

  • 8/3/2019 Sorting Lecture

    57/72

    57

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 4 9 8

    6

    LOW HIGH

    The number at position HIGH is moved toposition LOW.

  • 8/3/2019 Sorting Lecture

    58/72

    58

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 4 9 8

    6

    LOW HIGH

    LOW marches right until it finds a numberlarger than PIVOT.

  • 8/3/2019 Sorting Lecture

    59/72

    59

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 4 9 8

    6

    LOW HIGH

    LOW marches right until it finds a numberlarger than PIVOT.

  • 8/3/2019 Sorting Lecture

    60/72

    60

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 4 9 8

    6

    LOW HIGH

    The number at position LOW is moved toposition HIGH.

  • 8/3/2019 Sorting Lecture

    61/72

    61

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 7 9 8

    6

    LOW HIGH

    The number at position LOW is moved toposition HIGH.

  • 8/3/2019 Sorting Lecture

    62/72

    62

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 7 9 8

    6

    LOW HIGH

    HIGH marches LEFT until it aligns withLOW.

  • 8/3/2019 Sorting Lecture

    63/72

    63

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 7 9 8

    6

    LOW HIGH

    HIGH marches LEFT until it aligns withLOW.

  • 8/3/2019 Sorting Lecture

    64/72

    64

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 7 9 8

    6

    LOW HIGH

    The PIVOT is inserted at the positionLOW/HIGH.

  • 8/3/2019 Sorting Lecture

    65/72

    65

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 7 9 8

    6

    LOW HIGH

    The PIVOT is inserted at the positionLOW/HIGH.

    6

  • 8/3/2019 Sorting Lecture

    66/72

    66

    Nitty Gritty Details

    How do we do the partitioning?

    5 3 4 0 2 1 7 7 9 8

    6

    LOW HIGH

    The PIVOT is inserted at the positionLOW/HIGH.

    6

    PARTI

    TIONINGCO

    MPLETE!

    Quicksort

  • 8/3/2019 Sorting Lecture

    67/72

    67

    Quicksort

    How many comparisons/swaps?

    Sort 1000 items:1000 items

    Quicksort

  • 8/3/2019 Sorting Lecture

    68/72

    68

    Quicksort

    How many comparisons/swaps?

    Sort 1000 items:1000 items

    ~500 items ~500 items

    Quicksort

  • 8/3/2019 Sorting Lecture

    69/72

    69

    Quicksort

    How many comparisons/swaps?

    Sort 1000 items:1000 items

    ~500 items ~500 items

    ~250 items ~250 items ~250 items ~250 items

    Quicksort

  • 8/3/2019 Sorting Lecture

    70/72

    70

    Quicksort How many comparisons/swaps?

    Sort 1000 items:1000 items

    ~500 items ~500 items

    ~250 items ~250 items ~250 items ~250 items

    ~1 item ~1 item ~1 item ~1 item ~1 item~1 item. . . . .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    .

    ~10

    levels

    oft

    he

    tree

    .

    .

    .

    .

    .

  • 8/3/2019 Sorting Lecture

    71/72

    71

    Comparing Algorithms

    To sort 1000 times: Quicksort requires ~10,000 comparisons

    (on average).

    Bubble sort may require ~500,000comparisons.

    Insertion sort may require ~500,000

    comparisons.

  • 8/3/2019 Sorting Lecture

    72/72

    Sorting Summary

    Sorting: key part of many computer programs!! Algorithm: set of steps for completing a task.

    Different algorithms for same task may have

    different efficiencies! Talked about three sorting algorithms: Bubble sort, Insertion Sort and Quicksort

    Bubble sort and insertion sort are simple to

    build, but need manysteps to sort the list. Quicksort is more complex to build,

    sorts list muchfaster.