Title: Computer Science 1620
1Computer Science 1620
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
3Sorting
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
6Simple 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
9Bubble 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
10Bubble 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
11Bubble 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
12Bubble 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
13Bubble 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
14Bubble 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
15Bubble 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
16Bubble 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.
17Bubble 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
18Bubble 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
19Bubble 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
20Bubble 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
21Bubble 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
22Bubble 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
23Bubble 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.
24Bubble 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
25Bubble 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
26Bubble 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
27Bubble 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
28Bubble 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
29Bubble 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.
30Bubble 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
31Bubble 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
32Bubble 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
33Bubble 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
34Bubble 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
35Bubble 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
36Bubble 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
37Bubble 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
38Bubble 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
39Bubble 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
40Bubble 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
41Bubble 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
43include 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
44include 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
45include 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
46include 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
47include 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
48include 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
49include 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
50include 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
51include 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
52include 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
53include 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
54include 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)
55include 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?
56include 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?
57include 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)
58include 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?
60Bubble 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
61Bubble 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
62Bubble 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
63Bubble 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
64Bubble 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
65Bubble 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
66Bubble 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
68include 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)
69include 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
71include 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
74Bubble Sort
- After first time through loop
50
96
24
63
45
17
31
85
75Bubble Sort
- After second time through loop
50
85
24
45
17
31
63
96
76Bubble 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
79Bubble Sort
- After first time through loop
Only these elements need to be sorted
50
96
24
63
45
17
31
85
80Bubble Sort
- After second time through loop
Only these elements need to be sorted
50
85
24
45
17
31
63
96
81Bubble Sort
- After third time through loop
Only these elements need to be sorted
63
24
17
31
45
85
96
50
82include 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)
83include 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
89Bubble 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
90Bubble 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
91Bubble 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
92Bubble 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
93Bubble 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
94Bubble 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
95Bubble 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
96Bubble 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.
97Bubble 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
100Selection 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
101Selection 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
102Selection 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
103Selection 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
104Selection 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
105Selection 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
106Selection 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
107Selection 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
108Selection 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
109include 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
110include 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
111include 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?
112include 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
113include 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
114include 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
115include 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
116include 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)
117include 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
119include 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
120include 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
121include 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
122include 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
123include 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
125Bubble Sort
96
24
63
45
17
31
85
50
126Bubble Sort
96
24
63
45
85
31
17
50
127Bubble Sort
- After second smallest is found
96
24
63
45
85
31
17
50
128Bubble Sort
- After third smallest is found
96
24
31
45
85
63
17
50
129Bubble 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
131Bubble Sort
Find smallest in this set
96
24
63
45
17
31
85
50
132Bubble Sort
Find smallest in this set
96
24
63
45
85
31
17
50
133Bubble Sort
- After second smallest is found
Find smallest in this set
96
24
63
45
85
31
17
50
134Bubble 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
136include 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)
137include 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
139include 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
142Insertion 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
143Insertion Sort
85
24
63
45
17
31
96
50
144Insertion Sort Outer Loop
- loop through all elements one at a time
- starting at 2nd element
current
85
24
63
45
17
31
96
50
145Insertion Sort Outer Loop
- Step 1 take current element out of the array
current
24
85
63
45
17
31
96
50
146Insertion 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
147Insertion 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
148Insertion 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
149Insertion 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
150Insertion 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
151Insertion Sort Outer Loop
- Step 4 move to next element in list, and being
process again
current
24
63
45
17
31
96
50
85
152Insertion Sort Outer Loop
- Step 1 take current element out of the array
current
63
24
45
17
31
96
50
85
153Insertion 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
154Insertion 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
155Insertion 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
156Insertion Sort Outer Loop
- Step 4 move to next element in list, and being
process again
current
24
45
17
31
96
50
85
63
157Insertion Sort Outer Loop
- Step 1 take current element out of the array
current
45
24
17
31
96
50
85
63
158Insertion 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
159Insertion 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
160Insertion 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
161Insertion 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
162Insertion Sort Outer Loop
- Step 4 move to next element in list, and being
process again
current
24
45
17
31
96
50
85
63
163Insertion Sort Outer Loop
- Step 1 take current element out of the array
current
17
24
45
31
96
50
85
63
164Insertion 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
165Insertion 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
166Insertion 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
167Insertion 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
168Insertion 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
169Insertion 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
170Insertion Sort Outer Loop
- Step 4 move to next element in list, and being
process again
current
24
45
17
31
96
50
85
63
171Insertion 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
173include 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