Computer Science 1620 - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Science 1620

Description:

Computer Science 1620 Sorting – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 214
Provided by: Kev995
Category:

less

Transcript and Presenter's Notes

Title: Computer Science 1620


1
Computer Science 1620
  • Sorting

2
  • Sorting
  • cases exist where we would like our data to be in
    ascending (descending order)
  • binary searching
  • printing purposes
  • selection (e.g. select 5 students with highest
    gpas)
  • etc
  • the sorting problem
  • given a list of non-sorted data, rearrange the
    data into ascending or descending order

3
Sorting
85
24
63
45
17
31
96
50
Sort
85
24
63
45
17
31
96
50
4
  • Sorting
  • a well-studied area of Computer Science
  • many clever algorithms exist
  • Simple
  • bubble sort
  • selection sort
  • insertion sort
  • Complex
  • merge sort
  • quick sort (very popular!!)

We will focus on the simple sorting algorithms,
as the complex sorts involve recursion.
5
  • Sorting the general idea
  • the sort will be conducted using a series of
    swaps (discussed on next slide)
  • we will not use any extra arrays
  • inline
  • the quality of a sorting algorithm is typically
    based on two metrics
  • of comparisons
  • of swaps

6
Simple Sorting
  • The swap procedure
  • swap(ai, aj)
  • swaps the elements i and j in a, so that ai
    becomes aj and vice versa
  • eg. swap(a3, a5)
  • provided by algorithm header file
  • include ltalgorithmgt

85
24
63
45
17
31
96
50
Before
85
24
63
31
17
45
96
50
After
7
  • Bubble Sort
  • a very simple sorting algorithm
  • the basic idea
  • when two adjacent items are found to be out of
    order, swap them
  • adjacent in this case means two items that are
    side by side in an array (a1, a2) or (bi,
    bi1)

8
  • Bubble Sort Algorithm
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
    (that is, (1,2), (2,3), (3,4), and so on)
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

9
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

85
24
63
45
17
31
96
50
10
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

85
24
63
45
17
31
96
50
Out of order, so we swap
11
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

85
63
24
45
17
31
96
50
Out of order, so we swap
12
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

85
45
24
63
17
31
96
50
Out of order, so we swap
13
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

85
17
24
63
45
31
96
50
Out of order, so we swap
14
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

85
31
24
63
45
17
96
50
Out of order, so we swap
15
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

85
96
24
63
45
17
31
50
In order, so we don't swap
16
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
50
24
63
45
17
31
85
Out of order, so we swap.
17
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

50
96
24
63
45
17
31
85
  • Are items still out of order?
  • yes, so we repeat

18
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

24
63
45
17
31
85
50
96
In order, no swap
19
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
45
24
17
31
85
50
96
Out of order, so we swap
20
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
17
24
45
31
85
50
96
Out of order, so we swap
21
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
24
45
17
85
50
96
Out of order, so we swap
22
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
85
24
45
17
31
50
96
In order, no swap
23
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

85
50
24
45
17
31
63
96
Out of order, so we swap.
24
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

50
85
24
45
17
31
63
96
In order, no swap
25
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

50
85
24
45
17
31
63
96
  • Are items still out of order?
  • yes, so we repeat

26
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

24
45
17
31
63
50
85
96
In order, no swap
27
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

24
31
63
50
85
96
45
17
Out of order, so we swap
28
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

31
24
17
63
50
85
96
45
Out of order, so we swap
29
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
24
17
31
50
85
96
45
In order, no swap.
30
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

50
24
17
31
45
85
96
63
Out of order, so we swap
31
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
24
17
31
45
85
96
50
In order, no swap
32
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
24
17
31
45
85
96
50
In order, no swap
33
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
24
17
31
45
85
96
50
  • Are items still out of order?
  • yes, so we repeat

34
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
24
17
Out of order, so we swap
35
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
36
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
37
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
38
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
39
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
40
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
41
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
  • Are items still out of order?
  • no, we are done

42
  • Bubble Sort
  • guaranteed to sort our data
  • requires up to n passes through the array

43
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) Step 1
Repeat while items in list are out of order
Step 1.1 Check each pair of adjacent elements
Step 1.1.1 If a pair of adjacent elements are
found to be out of order, swap them
44
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) Step 1
Repeat while items in list are out of order
Step 1.1 Check each pair of adjacent elements
Step 1.1.1 If a pair of adjacent elements are
found to be out of order, swap them
45
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) Step 1.1 Check
each pair of adjacent elements Step 1.1.1
If a pair of adjacent elements are found to be
out of order, swap them
46
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) Step 1.1 Check
each pair of adjacent elements Step 1.1.1
If a pair of adjacent elements are found to be
out of order, swap them
47
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) Step 1.1 for
each pair of adjacent elements Step 1.1.1
If these items out of order, swap

48
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0
j) Step 1.1.1 If these items
out of order, swap

49
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0
j) Step 1.1.1 If these items
out of order, swap

50
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0
j) Step 1.1.1 If these items
out of order Step 1.1.1.1 swap the two items

51
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0
j) Step 1.1.1 If these items
out of order Step 1.1.1.1 swap the two items

52
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0
j) if (aj gt aj1)
Step 1.1.1.1 swap the two items

53
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0
j) if (aj gt aj1)
Step 1.1.1.1 swap the two items

54
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0
j) if (aj gt aj1)
swap(aj, aj1)

55
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0
j) if (aj gt aj1)
swap(aj, aj1)

What is condition here?
56
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0 j
lt sz j) if (aj gt aj1)
swap(aj, aj1)

Will this work?
57
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0 j
lt sz - 1 j) if (aj gt aj1)
swap(aj, aj1)

58
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while (items
in list are out of order) for (int j 0 j
lt sz - 1 j) if (aj gt aj1)
swap(aj, aj1)

59
  • How do we know if the list is out of order?
  • Question What happens in our "check" loop when
    all of the items are in order?

60
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
61
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
62
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
63
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
64
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
65
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
66
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

63
31
45
85
96
50
17
24
In order, no swap
67
  • What did you notice?
  • no swaps took place
  • our list is sorted if during our check loop, no
    swaps took place
  • during our check loop, if a swap occurs, then
    remember this
  • use a boolean variable
  • initially false
  • true if a swap occurs

68
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) while
(items in list are out of order) for (int j
0 j lt sz - 1 j) if (aj gt aj1)
swap(aj, aj1)

69
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) bool
swapped true while (swapped)
swapped false for (int j 0 j lt sz - 1
j) if (aj gt aj1) swap(aj,
aj1) swapped true

70
  • Why do I initially set swapped to true
  • to guarantee that the while loop executes at
    least once
  • what is another way to guarantee this?
  • make it a do-while loop

71
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) bool
swapped do swapped false for
(int j 0 j lt sz - 1 j) if (aj gt
aj1) swap(aj, aj1)
swapped true
while (swapped)
72
  • An Improvement to Bubble Sort
  • this initial version of bubble sort works fine
  • how many comparisons does it make?
  • each time through the "check" loop n-1
  • how many times does it go through the check loop
  • n (in the worst case)
  • therefore, it can make up to n(n-1) comparisons

73
  • Better Bubble Sort
  • what do you notice at the end of each "check" loop

74
Bubble Sort
  • After first time through loop

50
96
24
63
45
17
31
85
75
Bubble Sort
  • After second time through loop

50
85
24
45
17
31
63
96
76
Bubble Sort
  • After third time through loop

63
24
17
31
45
85
96
50
77
  • Each time through the loop, the ith largest value
    gets put in its final position
  • first time through the loop, largest item placed
    in last spot
  • second time through the loop, second largest item
    placed in second-last spot
  • ith time through the loop, ith largest item
    placed in ith largest spot

78
  • What does this mean?
  • on the second pass through the loop, do I need to
    compare the last item?
  • no it's the largest item, so belongs in the
    last spot
  • this means that each time through the loop, the
    number of items to sort gets smaller

79
Bubble Sort
  • After first time through loop

Only these elements need to be sorted
50
96
24
63
45
17
31
85
80
Bubble Sort
  • After second time through loop

Only these elements need to be sorted
50
85
24
45
17
31
63
96
81
Bubble Sort
  • After third time through loop

Only these elements need to be sorted
63
24
17
31
45
85
96
50
82
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) bool
swapped do swapped false for
(int j 0 j lt sz - 1 j) if (aj gt
aj1) swap(aj, aj1)
swapped true
while (swapped)
83
include ltalgorithmgt using namespace std void
bubble_sort(int a, int sz) bool
swapped do swapped false for
(int j 0 j lt sz - 1 j) if (aj gt
aj1) swap(aj, aj1)
swapped true
sz-- while (swapped)

84
  • How good is this simple fix?
  • how many comparisons
  • first time through the loop n 1
  • second time through the loop n-2
  • ith time through the loop n-i
  • Number of comparisons in the worst case

85
  • Hence
  • this very small extension to the bubble sort
    algorithm improves the of comparisons by 50

86
  • Bubble Sort Problems
  • suppose you are sorting an array of strings
  • each time you call swap, the following must
    occur
  • copy first string to temporary storage
  • copy second string value to first string
  • copy temporary storage value to second string
  • if strings are big, this can be costly for time
  • ie. swap can be expensive

87
  • Bubble Sort
  • what makes bubble sort so "expensive" in terms of
    computation is the number of swaps that it does
  • number of comparisons n(n-1)/2 in worst case
  • number of swaps n(n-1)/2 in worst
    case

88
  • Selection Sort
  • suppose my items are arranged like this
  • what will the first "check" pass of bubble sort
    look like?

96
24
63
45
17
31
85
50
89
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
24
63
45
17
31
85
50
90
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
24
63
45
17
31
85
50
Out of order, so we swap
91
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
63
24
45
17
31
85
50
Out of order, so we swap
92
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
45
24
63
17
31
85
50
Out of order, so we swap
93
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
17
24
63
45
31
85
50
Out of order, so we swap
94
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
31
24
63
45
17
85
50
Out of order, so we swap
95
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
85
24
63
45
17
31
50
In order, so we don't swap
96
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

96
50
24
63
45
17
31
85
Out of order, so we swap.
97
Bubble Sort
  • Step 1 Repeat while items in list are out of
    order
  • Step 1.1 Check each pair of adjacent elements
  • Step 1.1.1 If a pair of adjacent elements are
    found to be out of order, swap them

50
96
24
63
45
17
31
85
98
  • What did you notice?
  • the biggest number (96) got placed in the last
    spot (discussed last day)
  • however, the number gets "dragged" along by a
    number of swaps
  • what if, instead of dragging it to its final
    location, I swapped it directly there?

99
  • Selection Sort
  • Algorithm
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array
  • in other words
  • find the smallest element, swap it into the first
    spot
  • find the 2nd smallest element, swap it into the
    2nd spot
  • find the 3rd smallest element, swap it into the
    3rd lspot

100
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

96
24
63
45
17
31
85
50
101
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

96
24
63
45
17
31
85
50
smallest
102
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

96
24
63
45
85
31
17
50
2nd smallest
103
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

96
24
63
45
85
31
17
50
3rd smallest
104
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

96
24
31
45
85
63
17
50
4th smallest
105
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

96
24
31
45
85
63
17
50
5th smallest
106
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

96
24
31
45
50
63
17
85
6th smallest
107
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

96
24
31
45
50
63
17
85
7th smallest
108
Selection Sort
  • Step 1 for each i between 1 and n
  • Step 1.1 find the i-th smallest number in the
    array
  • Step 1.2 swap that number in the i-th spot in
    the array

85
24
31
45
50
63
17
96
8th smallest
109
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) Step 1
for each i between 1 and n Step 1.1 find the
i-th smallest number in the array Step 1.2 swap
that number in the i-th spot in the
array
110
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) Step 1
for each i between 1 and n Step 1.1 find the
i-th smallest number in the array Step 1.2 swap
that number in the i-th spot in the
array
111
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 1 i lt n i) Step 1.1 find the i-th
smallest number in the array Step 1.2 swap that
number in the i-th spot in the array

Will this work?
112
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) Step 1.1 find the i-th
smallest number in the array Step 1.2 swap that
number in the i-th spot in the array

Remember arrays are ordered 0 to n-1, not 1 to n
113
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) Step 1.1 find the i-th
smallest number in the array Step 1.2 swap that
number in the i-th spot in the array

114
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith (i-th
smallest number in the array) Step 1.2 swap
that number in the i-th spot in the array

115
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith (i-th
smallest number in the array) Step 1.2 swap
that number in the i-th spot in the array

116
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith (i-th
smallest number in the array) swap(aith,
ai)
117
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith (i-th
smallest number in the array) swap(aith,
ai)
How do we find the ith smallest?
118
  • Let's simplify this
  • how do we set ith to point to the smallest
    number?
  • One solution
  • assume smallest is at location 0
  • loop through the other elements
  • if you find a smaller number at another location,
    remember that location

119
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith (i-th
smallest number in the array) swap(aith,
ai)
  • assume smallest is at location 0
  • loop through the other elements
  • if you find a smaller number at another
    location, remember that location

120
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith 0
swap(aith, ai)
  • assume smallest is at location 0
  • loop through the other elements
  • if you find a smaller number at another
    location, remember that location

121
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith 0 for
(int j 1 j lt n j)
swap(aith, ai)
  • assume smallest is at location 0
  • loop through the other elements
  • if you find a smaller number at another
    location, remember that location

122
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith 0 for
(int j 1 j lt n j) if (aj lt
aith) swap(aith,
ai)
  • assume smallest is at location 0
  • loop through the other elements
  • if you find a bigger number at another
    location, remember that location

123
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith 0 for
(int j 1 j lt n j) if (aj lt
aith) ith j
swap(aith, ai)
  • assume smallest is at location 0
  • loop through the other elements
  • if you find a bigger number at another
    location, remember that location

124
  • This works fine for finding the smallest
  • but what about finding the 2nd smallest, or 3rd
    smallest, etc?
  • let's consider what the array looks like after
    the ith smallest element is found

125
Bubble Sort
  • Our array, initially

96
24
63
45
17
31
85
50
126
Bubble Sort
  • After smallest is found

96
24
63
45
85
31
17
50
127
Bubble Sort
  • After second smallest is found

96
24
63
45
85
31
17
50
128
Bubble Sort
  • After third smallest is found

96
24
31
45
85
63
17
50
129
Bubble Sort
  • After third smallest is found

96
24
31
45
85
63
17
50
130
  • What do you notice?
  • after the smallest is found, it is placed in
    location 0
  • this means that the 2nd smallest item is the
    smallest item between location 1 and location n-1
  • after the 2nd smallest is found, it is placed in
    location 1
  • this means that the 3rd smallest item is the
    smallest item between location 2 and location n-1

131
Bubble Sort
  • Our array, initially

Find smallest in this set
96
24
63
45
17
31
85
50
132
Bubble Sort
  • After smallest is found

Find smallest in this set
96
24
63
45
85
31
17
50
133
Bubble Sort
  • After second smallest is found

Find smallest in this set
96
24
63
45
85
31
17
50
134
Bubble Sort
  • After third smallest is found

Find smallest in this set
96
24
31
45
85
63
17
50
135
  • In other words
  • we only need to search the elements beginning at i

136
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith 0 for
(int j 1 j lt n j) if (aj lt
aith) ith j
swap(aith, ai)
137
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n i) int ith i for
(int j i1 j lt n j) if (aj lt
aith) ith j
swap(aith, ai)
138
  • A Couple of Minor Improvements
  • 1) We do not need to do the last pass through the
    outer loop
  • 2) We do not need to swap if i ith

139
include ltalgorithmgt using namespace std void
selection_sort(int a, int n) for (int
i 0 i lt n-1 i) int ith i
for (int j i1 j lt n j) if (aj
lt aith) ith j
if (i ! ith) swap(aith,
ai)
140
  • Selection Sort
  • how many comparisons does selection sort make?
  • first pass through loop n-1
  • second pass through loop n-2
  • third pass through loop n-3
  • .
  • n-1 pass through loop 1

141
  • Selection Sort
  • just as many compares as bubble sort
  • how many swaps (worst case)?
  • N -1
  • this is a big improvement over bubble sort, which
    required n(n-1)/2 swaps

142
Insertion Sort
  • nested loop, just like selection sort
  • outer loop moves one at a time through each
    element of the array
  • at each element
  • "picks up" element out of the array
  • places the element in its corresponding spot
    behind this index
  • result
  • when outer loop is at index i, everything behind
    index i is sorted

143
Insertion Sort
  • Example

85
24
63
45
17
31
96
50
144
Insertion Sort Outer Loop
  • loop through all elements one at a time
  • starting at 2nd element

current
85
24
63
45
17
31
96
50
145
Insertion Sort Outer Loop
  • Step 1 take current element out of the array

current
24
85
63
45
17
31
96
50
146
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • or nothing left to shift

current
24
85
63
45
17
31
96
50
147
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 85 gt 24, so it's shifted

current
24
85
63
45
17
31
96
50
148
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • nothing left to shift, so stop

current
24
63
45
17
31
96
50
85
149
Insertion Sort Outer Loop
  • Step 3 place the current element in the empty
    spot created by shifting

current
24
63
45
17
31
96
50
85
150
Insertion Sort Outer Loop
  • Step 3 place the current element in the empty
    spot created by shifting

current
24
63
45
17
31
96
50
85
151
Insertion Sort Outer Loop
  • Step 4 move to next element in list, and being
    process again

current
24
63
45
17
31
96
50
85
152
Insertion Sort Outer Loop
  • Step 1 take current element out of the array

current
63
24
45
17
31
96
50
85
153
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 85 gt 63, so shift

current
63
24
45
17
31
96
50
85
154
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 24 lt 63, so stop!

current
63
24
45
17
31
96
50
85
155
Insertion Sort Outer Loop
  • Step 3 place the current element in the empty
    spot created by shifting

current
24
45
17
31
96
50
85
63
156
Insertion Sort Outer Loop
  • Step 4 move to next element in list, and being
    process again

current
24
45
17
31
96
50
85
63
157
Insertion Sort Outer Loop
  • Step 1 take current element out of the array

current
45
24
17
31
96
50
85
63
158
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 85 gt 45, so shift

current
45
24
17
31
96
50
85
63
159
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 63 gt 45, so shift

current
45
24
17
31
96
50
85
63
160
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 24 lt 45, so stop!

current
45
24
17
31
96
50
85
63
161
Insertion Sort Outer Loop
  • Step 3 place the current element in the empty
    spot created by shifting

current
24
45
17
31
96
50
85
63
162
Insertion Sort Outer Loop
  • Step 4 move to next element in list, and being
    process again

current
24
45
17
31
96
50
85
63
163
Insertion Sort Outer Loop
  • Step 1 take current element out of the array

current
17
24
45
31
96
50
85
63
164
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 85 gt 17, so shift

current
17
24
45
31
96
50
85
63
165
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 63 gt 17, so shift

current
17
24
45
31
96
50
85
63
166
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 45 gt 17, so shift

current
17
24
45
31
96
50
85
63
167
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • 24 gt 17, so shift

current
17
24
45
31
96
50
85
63
168
Insertion Sort Outer Loop
  • Step 2 shift elements behind this element to
    the right
  • stop when element to be shifted is less than
    current element
  • nothing left to shift, so stop!

current
17
24
45
31
96
50
85
63
169
Insertion Sort Outer Loop
  • Step 3 place the current element in the empty
    spot created by shifting

current
24
45
17
31
96
50
85
63
170
Insertion Sort Outer Loop
  • Step 4 move to next element in list, and being
    process again

current
24
45
17
31
96
50
85
63
171
Insertion Sort Outer Loop
  • And so on
  • notice that the list to the left of current is
    always sorted

current
24
45
17
31
96
50
85
63
172
  • How do we code this?
  • tricky
  • Algorithm
  • for each element (starting at 2nd)
  • take element out of array
  • Shift elements behind this element to the right,
    stopping when element to be shifted is less than
    current element, or nothing left to shift
  • place the current element in the empty spot
    created by shifting

173
include ltalgorithmgt using namespace std void
insertion_sort(int a, int n) Step 1
for each element (starting at 2nd) Step 1.1
Take element out of array Step 1.2 Shift
elements behind this element to the right,
stopping when element to be shifted is less than
current element, or nothing left to shift
Write a Comment
User Comments (0)
About PowerShow.com