3. Lecture Objectives
•
•
•
•
•
•
•
Become aware of what is sorting.
Become aware of sorting algorithms.
Explore the types of sorting algorithms.
Learn about Bubble Sort.
Learn about Insertion Sort.
Learn about Selection Sort.
Learn about Merge Sort.
4. What is sorting?
Ascending or
descending order
Sorting = ordering.
Sorted = ordered based on a particular
way.
An operation that puts
(organizes) elements of a list
(a collection of data) into
certain order (ascending or
descending order).
3162134590
0112334569
9654332110
5. Sorting – formal definition
Permutation
Input:
A sequence of n numbers:
a1, a2, ..., an
Output:
A permutation (reordering):
ak1, ak2, ..., akn
of the input sequence such that
f(ak1) ≤ f(ak2) ≤... ≤ f(akn)
where f is an ordering function
6. Examples of Sorting
– Words in a dictionary are sorted.
– Files in a directory are often listed in sorted order.
– In a newspaper, the calendar of events in a schedule is generally
sorted by date.
– In a record store musical compact disks are generally sorted by
recording artist.
7. What is a Sorting Algorithm?
An algorithm for sorting a set of elements.
3162134590
0112334569
9654332110
8. Types of Sorting Algorithms
There are many, many different types of sorting algorithms,
but the primary ones are:
•
•
•
•
•
•
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
Quick Sort
Shell Sort
9. Selection Sort
One of the simplest
algorithms works as follows:
Name “selection sort” because it works by
repeatedly “selecting“ the smallest remaining
element and exchanging it with the i-th
element in i-th iteration.
sorting
– Find the smallest element and
exchange it with the element
in the first position
– Find the second smallest
element and exchange it with
the element in the second
position
– Continuing in this way until
the entire array is sorted.
Meaning of words
47. Code of selection sort
public static void selectionSort(int[]
{
int outer, inner, min;
for (outer = 0; outer < a.length
{
min = outer;
for (inner = outer + 1; inner <
{
if (a[inner] < a[min]) min =
a)
- 1; outer++)
a.length; inner++)
inner;}
// Invariant: for all i, if outer <= i <= inner, then
a[min] <= a[i]
// a[min] is least among a[outer]..a[a.length - 1]
int temp = a[outer];
a[outer] = a[min];
a[min] = temp;
// Invariant: for all i <= outer, if i < j then a[i] <=
a[j]
}
}
48. Insertion sort
1. We have two group of items:
–sorted group, and
–unsorted group
2. Initially, all items in the
unsorted group and the sorted
group is empty.
This method is often
used for playing cards (used by
people to sort bridge hands).
–We assume that items in the
unsorted group unsorted.
–We have to keep items in the
sorted group sorted.
3. Pick any item from, then insert
the item at the right position in
the sorted group to maintain
sorted property.
4. Repeat the process until the
unsorted group becomes empty.
49. Insertion sort
Like sorting a hand of playing cards:
–Start with an empty left hand and the
cards facing down on the table.
–Remove one card at a time from the
table, and insert it into the correct
position in the left hand
Compare it with each of the cards
already in the hand, from right to left.
–The cards held in the left hand are
sorted
These cards were originally the top cards
of the pile on the table.
56. Bubble sort
•
•
•
Compare each element (execept the last one) with its neighbor to the
right.
– If they are out of order, swap them.
– This puts the largest element at the very end.
– The last element is now in the correct and final place.
Compare each element (execept the last two) with its neighbor to the
right.
– If they are out of order, swap them.
– This puts the largest element at the very end.
– The last two elements is now in the correct and final place.
Compare each element (execept the last three) with its neighbor to the
right.
– Continue as above until you have no unsorted elements on the left.
57. Bubble sort
9, 6, 2, 12, 11, 9, 3, 7
Compares the numbers in pairs from left to right exchanging when
Compares the numbers in pairs from left to right exchanging when
necessary. The first number is compared to the second and as it is
necessary. The first number is compared to the second and as it is
larger they are exchanged.
larger they are exchanged.
6, 9, 2, 12, 11, 9, 3, 7
The next pair of numbers are compared. 9 is the larger and this pair is
The next pair of numbers are compared. 9 is the larger and this pair is
also exchanged.
also exchanged.
6, 2, 9, 12, 11, 9, 3, 7
In the third comparison, the 9 is not larger than the 12 so no exchange
In the third comparison, the 9 is not larger than the 12 so no exchange
is made. We move on to compare the next pair without any change to
is made. We move on to compare the next pair without any change to
the list.
the list.
58. Bubble sort
6, 2, 9, 12, 11, 9, 3, 7
The 12 is larger than the 11 so they are exchanged.
The 12 is larger than the 11 so they are exchanged.
6, 2, 9, 11, 12, 9, 3, 7
The 12 is greater than the 9 so they are exchanged
The 12 is greater than the 9 so they are exchanged
6, 2, 9, 11, 9, 12, 3, 7
The 12 is greater than the 3 so they are exchanged.
The 12 is greater than the 3 so they are exchanged.
59. Bubble sort
6, 2, 9, 11, 9, 3, 12, 7
The 12 is greater than the 7 so they are exchanged.
The 12 is greater than the 7 so they are exchanged.
The end of the list has been reached so this is the end of the first pass. The twelve
The end of the list has been reached so this is the end of the first pass. The twelve
at the end of the list must be largest number in the list and so is now in the correct
at the end of the list must be largest number in the list and so is now in the correct
position. We now start aanew pass from left to right.
position. We now start new pass from left to right.
6, 2, 9, 11, 9, 3, 7, 12
60. Bubble sort
First Pass
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2, 6,
6, 2, 9, 11,11,11,11, 12
9, 3, 7, 7,
9, 3,
This time we do not have to compare the last two numbers as we know
This time we do not have to compare the last two numbers as we know
the 12 is in position. This pass therefore only requires 6 comparisons.
the 12 is in position. This pass therefore only requires 6 comparisons.
61. Bubble sort
6, 2, 9, 11, 9, 3, 7, 12
Second Pass
2, 6, 9, 9, 3, 7, 11, 12
Third Pass
2, 6, 9, 9, 3, 7, 11, 12
3, 9, 9,
7,
First Pass
This time the 11 and 12 are in position. This pass therefore only
This time the 11 and 12 are in position. This pass therefore only
requires 5 comparisons.
requires 5 comparisons.
62. Bubble sort
6,
Second Pass
2,
Third Pass2,
Fourth Pass
2,
First Pass
2,
6,
6,
6,
9,
9,
9,
3,
9,
11, 9, 3, 7,
9, 3, 7, 11,
3, 7, 9, 11,
7,
9, 9,
3, 7, 9, 11,
12
12
12
12
Each pass requires fewer comparisons. This time only 4 are needed.
Each pass requires fewer comparisons. This time only 4 are needed.
63. Bubble sort
6,
Second Pass
2,
Third Pass2,
Fourth Pass
2,
Fifth Pass
2,
First Pass
2,
6,
6,
6,
3,
6,
9,
9,
9,
3,
6,
3,
11, 9, 3, 7,
9, 3, 7, 11,
3, 7, 9, 11,
7, 9, 9, 11,
7, 9, 9, 11,
12
12
12
12
12
The list is now sorted but the algorithm does not know this until it
The list is now sorted but the algorithm does not know this until it
completes aapass with no exchanges.
completes pass with no exchanges.
64. Bubble sort
6,
Second Pass
2,
Third Pass2,
Fourth Pass
2,
Fifth Pass
2,
Sixth Pass 2,
First Pass
2,
6,
6,
6,
3,
3,
9,
9,
9,
3,
6,
6,
11, 9, 3, 7,
9, 3, 7, 11,
3, 7, 9, 11,
7, 9, 9, 11,
7, 9, 9, 11,
7, 9, 9, 11,
12
12
12
12
12
12
This pass no exchanges are made so the algorithm knows the list is
This pass no exchanges are made so the algorithm knows the list is
sorted. It can therefore save time by not doing the final pass. With other
sorted. It can therefore save time by not doing the final pass. With other
lists this check could save much more work.
lists this check could save much more work.
68. Merge sort
• Divide array into two halves.
• Recursively sort each half.
• Merge two halves to make sorted whole.
A
L
G
O
R
I
T
H
M
S
A
L
G
O
R
I
T
H
M
S
divide
A
G
L
O
R
H
I
M
S
T
sort
A
G
H
I
L
M
O
R
S
T
merge
69. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest
A
G
A
smallest
L
O
R
H
I
M
S
T
auxiliary array
70. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest
A
G
A
smallest
L
G
O
R
H
I
M
S
T
auxiliary array
71. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest
A
G
A
L
G
smallest
O
H
R
H
I
M
S
T
auxiliary array
72. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest
A
G
A
L
G
smallest
O
H
R
I
H
I
M
S
T
auxiliary array
73. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest
A
G
A
L
G
smallest
O
H
R
I
H
L
I
M
S
T
auxiliary
array
74. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest
A
G
A
L
G
O
H
smallest
R
I
H
L
M
I
M
S
T
auxiliary array
75. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest
A
G
A
L
G
O
H
smallest
R
I
H
L
M
I
O
M
S
T
auxiliary array
76. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
smallest
A
G
A
L
G
O
H
smallest
R
I
H
L
M
I
O
M
R
S
T
auxiliary array
77. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
first half
exhausted
A
G
A
L
G
O
H
R
I
smallest
H
L
M
I
O
M
R
S
S
T
auxiliary array
78. Merge sort
• Keep track of smallest element in each sorted half.
• Insert smallest of two elements into auxiliary array.
• Repeat until done.
first half
exhausted
A
G
A
L
G
O
H
R
I
smallest
H
L
M
I
O
M
R
S
S
T
T
auxiliary array
80. We learnt!
•
•
•
•
•
•
•
What is sorting.
Sorting algorithms.
Types of sorting algorithms.
Bubble Sort.
Insertion Sort.
Selection Sort.
Merge Sort.
81. Quiz
1.
One of the simplest sorting algorithms is called bubble
sort. Do you know why?
A
It encases each element in a 'bubble' before sorting
them.
B
It's a mystery. Why is anything called what it is,
really?
The designer hoped more people would use his sort if
it had a cute name
C
D
Teacher Guide Quiz Question
Smaller elements 'bubble' to the top
Teacher Guide Quiz Solution
82. Quiz
2.
In which case would an insertion sort perform best,
assuming it was reading the array to be sorted from
beginning to end (as opposed to randomly)?
A
If the array was sorted in reverse order.
B
If the array was already sorted.
C
If the array was in a random order.
D
These will all perform equally well.
83. Homework
Sort the following array using each of the four sorting
algorithms.
26
a)
b)
c)
d)
48
12
Bubble Sort
Selection Sort
Insertion Sort
Merge Sort
92
28
6
33
Notes de l'éditeur
{"82":"Note: For solving this quiz it is necessary to use Mouse Mischief (A fun way to keep your students' attention and increase participation in the classroom. http://www.microsoft.com/multipoint/mouse-mischief/en-us/default.aspx) or Interactive Classroom (Use classroom polls to improve student learning, http://www.pil-network.com/Resources/Tools/Details/da5d09fd-9362-43b6-9e96-cf9aadc66e6c).\n","50":"Note:Insertion sort is the strategy used by the card player who picks up the card he or she is dealt one at a time and inserts it into his or her existing hand\n","7":"Note: The teacher reminds students what the algorithm is? \n","57":"Note: The Bubble Sort algorithm looks at pairs of entries in the array, and swaps their order if needed.\nIf we compare pairs of adjacent elements and none are out of order, the list is sorted.\nIf any are out of order, we must have to swap them to get an ordered list.\nBubble sort will make passes though the list swapping any adjacent elements that are out of order.\nAfter the first pass, we know that the largest element must be in the correct place.\nAfter the second pass, we know that the second largest element must be in the correct place.\nBecause of this, we can shorten each successive pass of the comparison loop.\n","8":"Teacher: Sorting was intensely studied in the early days (1950's and 1960's). At one time it was estimated that a third to a half of all computation time world wide spent on sorting. Now it is a smaller fraction, but still sorting is a widely used operation on data. We will concentrate on 4 sorting algorithms: merge, insertion, quick, and heap. Each of them, besides being important for sorting, has a side benefit in illustrating design and analysis principles\n","81":"Note: For solving this quiz it is necessary to use Mouse Mischief (A fun way to keep your students' attention and increase participation in the classroom. http://www.microsoft.com/multipoint/mouse-mischief/en-us/default.aspx) or Interactive Classroom (Use classroom polls to improve student learning, http://www.pil-network.com/Resources/Tools/Details/da5d09fd-9362-43b6-9e96-cf9aadc66e6c).\n","4":"Teacher: Sorting was intensely studied in the early days (1950's and 1960's). At one time it was estimated that a third to a half of all computation time world wide spent on sorting. Now it is a smaller fraction, but still sorting is a widely used operation on data.\n","10":"Teacher: The selection sorting algorithm works by finding the smallest element in current unsorted array and swapping it with the element that it need to be sorted\n"}