sorting algorithms v01
DESCRIPTION
MCAST example . Sorting algorithmsTRANSCRIPT
Sorting Algorithms
2
Instructions
• For the embedded lessons look for the Icon
Click on it to present embedded lessons
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.
What is sorting?
An operation that puts (organizes) elements of a list (a collection of data) into certain order (ascending or descending order).
Sorting = ordering.Sorted = ordered based on a particular
way. 3 1 6 2 1 3 4 5 9 0
0 1 1 2 3 3 4 5 6 9
9 6 5 4 3 3 2 1 1 0
Ascending or descending order
Sorting – formal definition
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
Permutation
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.
What is a Sorting Algorithm?An algorithm for sorting a set of elements.
3 1 6 2 1 3 4 5 9 0
0 1 1 2 3 3 4 5 6 9
9 6 5 4 3 3 2 1 1 0
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
Selection Sort
One of the simplest sorting algorithms works as follows:
– 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.
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.
Meaning of words
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 6 2
Comparison
Data Movement
Sorted
Largest
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Selection Sort
5 1 3 4 2 6
Comparison
Data Movement
Sorted
Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Largest
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
2 1 3 4 5 6
Comparison
Data Movement
Sorted
Largest
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
Selection Sort
1 2 3 4 5 6
Comparison
Data Movement
Sorted
DONE!
Selection Sort
46
start
i:=1,n-1
min:=a(i)ind:=i
for j:=i+1,n
a(j) < min
min:=a(j)ind:=j
a(ind):=a(i)a(i):=min
end
false
Selection sort
Code of selection sortpublic static void selectionSort(int[] a) {
int outer, inner, min; for (outer = 0; outer < a.length - 1; outer++)
{ min = outer;
for (inner = outer + 1; inner < a.length; inner++) {
if (a[inner] < a[min]) min = 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] }
}
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. –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.
Insertion sort
This method is often used for playing cards (used by people to sort bridge hands).
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.
Insertion sort
50
6 1024
12
36
Insertion sort
1. To insert 12, we need to make room for it.
51
6 1024 36
12
Insertion sort
2. Moving 36.
52
6 10 24 36
12
Insertion sort
3. Moving 24.
53
6 10 243612
Insertion sort
4. Place 12 into right position.
for(i=1; i<n; i++)for(j=i; (j>0) && (a[j]>a[j-1]); j--){
pom=a[j];a[j]=a[j-1];a[j-1]=pom;
}
Code for insertion sort
Insertion sortwith Romanian folk dance
• 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.
Bubble sort
9, 6, 2, 12, 11, 9, 3, 7
6, 9, 2, 12, 11, 9, 3, 7
6, 2, 9, 12, 11, 9, 3, 7In 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 the list.
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 the list.
The next pair of numbers are compared. 9 is the larger and this pair is also exchanged.The next pair of numbers are compared. 9 is the larger and this pair is also exchanged.
Compares the numbers in pairs from left to right exchanging when necessary. The first number is compared to the second and as it is larger they are exchanged.
Compares the numbers in pairs from left to right exchanging when necessary. The first number is compared to the second and as it is larger they are exchanged.
Bubble sort
6, 2, 9, 12, 11, 9, 3, 7
6, 2, 9, 11, 12, 9, 3, 7
6, 2, 9, 11, 9, 12, 3, 7The 12 is greater than the 3 so they are exchanged.The 12 is greater than the 3 so they are exchanged.
The 12 is greater than the 9 so they are exchangedThe 12 is greater than the 9 so they are exchanged
The 12 is larger than the 11 so they are exchanged.The 12 is larger than the 11 so they are exchanged.
Bubble sort
6, 2, 9, 11, 9, 3, 12, 7
6, 2, 9, 11, 9, 3, 7, 12
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 at the end of the list must be largest number in the list and so is now in the correct position. We now start a new pass from left to right.
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 position. We now start a new pass from left to right.
Bubble sort
6, 2, 9, 11, 9, 3, 7, 122, 6, 9, 11, 9, 3, 7, 122, 6, 9, 9, 11, 3, 7, 122, 6, 9, 9, 3, 11, 7, 122, 6, 9, 9, 3, 7, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
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.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.
First Pass
Second Pass
Bubble sort
2, 6, 9, 9, 3, 7, 11, 122, 6, 9, 3, 9, 7, 11, 122, 6, 9, 3, 7, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12Second Pass
First Pass
Third Pass
This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.This time the 11 and 12 are in position. This pass therefore only requires 5 comparisons.
Bubble sort
2, 6, 9, 3, 7, 9, 11, 122, 6, 3, 9, 7, 9, 11, 122, 6, 3, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12Second Pass
First Pass
Third Pass
Each pass requires fewer comparisons. This time only 4 are needed.Each pass requires fewer comparisons. This time only 4 are needed.
2, 6, 9, 3, 7, 9, 11, 12Fourth Pass
Bubble sort
2, 6, 3, 7, 9, 9, 11, 122, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12Second Pass
First Pass
Third Pass
The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.The list is now sorted but the algorithm does not know this until it completes a pass with no exchanges.
2, 6, 9, 3, 7, 9, 11, 12Fourth Pass2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
Bubble sort
2, 3, 6, 7, 9, 9, 11, 12
6, 2, 9, 11, 9, 3, 7, 12
2, 6, 9, 9, 3, 7, 11, 12Second Pass
First Pass
Third Pass2, 6, 9, 3, 7, 9, 11, 12Fourth Pass2, 6, 3, 7, 9, 9, 11, 12
Fifth Pass
Sixth Pass
2, 3, 6, 7, 9, 9, 11, 12This 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 lists this check could save much more work.
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 lists this check could save much more work.
Bubble sort
65
start
i=2,n
j=n,i,-1
a(j-1) > a(j)
temp=a(j-1)a(j-1)=a(j)a(j)=temp
end
false
Bubble sort
Code for bubble sort
public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--)
{ // counting down for (inner = 0; inner < outer; inner++)
{ // bubbling up if (a[inner] > a[inner + 1])
{ // if out of order... int temp = a[inner];
// ...then swap a[inner] = a[inner + 1]; a[inner + 1] = temp; } } }}
67
Bubble sort with Hungarian ("Csángó") folk dance
• Divide array into two halves.• Recursively sort each half.• Merge two halves to make sorted whole.
merge
sort
A L G O R I T H M S
divideA L G O R I T H M S
A G L O R H I M S T
A G H I L M O R S T
Merge sort
auxiliary array
smallest smallest
A G L O R H I M S T
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
A
Merge sort
auxiliary array
smallest smallest
A G L O R H I M S T
A G
Merge sort
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
auxiliary array
smallest smallest
A G L O R H I M S T
A G H
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
Merge sort
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
Merge sort
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
Merge sort
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
Merge sort
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M O
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
Merge sort
auxiliary array
smallest smallest
A G L O R H I M S T
A G H I L M O R
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
Merge sort
auxiliary array
first halfexhausted smallest
A G L O R H I M S T
A G H I L M O R S
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
Merge sort
auxiliary array
first halfexhausted smallest
A G L O R H I M S T
A G H I L M O R S T
• Keep track of smallest element in each sorted half.• Insert smallest of two elements into auxiliary array.• Repeat until done.
Merge sort
Merge sort
We learnt!
• What is sorting. • Sorting algorithms.• Types of sorting algorithms. • Bubble Sort.• Insertion Sort.• Selection Sort.• Merge Sort.
Quiz
A
B
C
D
1. One of the simplest sorting algorithms is called bubble sort. Do you know why?
It encases each element in a 'bubble' before sorting them.
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 nameSmaller elements 'bubble' to the top
Teacher Guide - Quiz Question
Teacher Guide - Quiz Solution
Quiz
A
B
C
D
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)?
If the array was sorted in reverse order.
If the array was already sorted.
If the array was in a random order.
These will all perform equally well.
Sort the following array using each of the four sorting algorithms.
26 48 12 92 28 6 33
Homework
a) Bubble Sortb) Selection Sortc) Insertion Sortd) Merge Sort