View
218
Download
1
Tags:
Embed Size (px)
Citation preview
Sorting - Selection SortSorting - Selection Sort
Cmput 115 - Lecture 10Department of Computing Science
University of Alberta©Duane Szafron 2000
Some code in this lecture is based on code from the book:Java Structures by Duane A. Bailey or the companion structure package
Revised 1/26/00
©Duane Szafron 1999
2
About This LectureAbout This Lecture
In this lecture we will learn about a sorting algorithm called the Selection Sort.
We will study its implementation and its time and space complexity.
©Duane Szafron 1999
3
OutlineOutline
Selection Sort Algorithm
Selection Sort - Arrays
Time and Space Complexity of Selection Sort
Selection Sort - Vectors
©Duane Szafron 1999
4
The Sort ProblemThe Sort Problem
Given a collection, with elements that can be compared, put the elements in increasing or decreasing order.
60 30 10 20 40 90 70 80 500 1 2 3 4 5 6 7 8
10 20 30 40 50 60 70 80 900 1 2 3 4 5 6 7 8
©Duane Szafron 1999
5
Selection Sort Algorithm 1Selection Sort Algorithm 1
Find the index of the largest element and exchange references with the element at the last index.
Decrement the last index.
60 30 10 20 40 90 70 80 500 1 2 3 4 5 6 7 8
60 30 10 20 40 50 70 80 900 1 2 3 4 5 6 7 8
largest last
©Duane Szafron 1999
6
Selection Sort Algorithm 2Selection Sort Algorithm 2
Find the index of the largest element and exchange references with the element at the last index.
Decrement the last index.
60 30 10 20 40 50 70 80 900 1 2 3 4 5 6 7 8
60 30 10 20 40 50 70 80 900 1 2 3 4 5 6 7 8
No exchangesnecessary
©Duane Szafron 1999
7
Selection Sort Algorithm 3Selection Sort Algorithm 3
Find the index of the largest element and exchange references with the element at the last index.
Decrement the last index.
60 30 10 20 40 50 70 80 900 1 2 3 4 5 6 7 8
60 30 10 20 40 50 70 80 900 1 2 3 4 5 6 7 8
No exchangesnecessary
©Duane Szafron 1999
8
Selection Sort Algorithm 4Selection Sort Algorithm 4
Find the index of the largest element and exchange references with the element at the last index.
Decrement the last index.
50 30 10 20 40 60 70 80 900 1 2 3 4 5 6 7 8
60 30 10 20 40 50 70 80 900 1 2 3 4 5 6 7 8
©Duane Szafron 1999
9
Selection Sort Algorithm 5Selection Sort Algorithm 5
Find the index of the largest element and exchange references with the element at the last index.
Decrement the last index.
40 30 10 20 50 60 70 80 900 1 2 3 4 5 6 7 8
50 30 10 20 40 60 70 80 900 1 2 3 4 5 6 7 8
©Duane Szafron 1999
10
Selection Sort Algorithm 6Selection Sort Algorithm 6
Find the index of the largest element and exchange references with the element at the last index.
Decrement the last index.
20 30 10 40 50 60 70 80 900 1 2 3 4 5 6 7 8
40 30 10 20 50 60 70 80 900 1 2 3 4 5 6 7 8
©Duane Szafron 1999
11
Selection Sort Algorithm 7Selection Sort Algorithm 7
Find the index of the largest element and exchange references with the element at the last index.
Decrement the last index.
20 10 30 40 50 60 70 80 900 1 2 3 4 5 6 7 8
20 30 10 40 50 60 70 80 900 1 2 3 4 5 6 7 8
©Duane Szafron 1999
12
Selection Sort Algorithm 8Selection Sort Algorithm 8
Find the index of the largest element and exchange references with the element at the last index.
Decrement the last index.
10 20 30 40 50 60 70 80 900 1 2 3 4 5 6 7 8
20 10 30 40 50 60 70 80 900 1 2 3 4 5 6 7 8
©Duane Szafron 1999
13
Selection Sort Algorithm 9Selection Sort Algorithm 9
Stop.
10 20 30 40 50 60 70 80 900 1 2 3 4 5 6 7 8
©Duane Szafron 1999
14
Sorting Using ArraysSorting Using Arrays
Often when we are sorting elements in an Array, the physical size of the array is larger than its logical size (the number of “real” elements it contains).
To accommodate this, we add the logical size of the array as an extra parameter to our sorting methods.
If we know the logical size and physical size are the same, we can omit this extra parameter. Why wouldn’t we expect them to always be the same?
©Duane Szafron 1999
15
Selection Sort Code - Selection Sort Code - ArraysArrays
public static void selectionSort(Comparable anArray[ ], int size) {
// pre: 0 <= size <= anArray.length// post: values in anArray are in ascending order
int maxIndex; //index of largest objectint index; //index of last unsorted element
for (index = size - 1; index > 0; index--) {maxIndex = this.getMaxIndex(anArray, index);this.swap(anArray, index, maxIndex);
}}
code based on Bailey pg. 82
©Duane Szafron 1999
16
Method - getMaxIndex() - Method - getMaxIndex() - ArraysArrayspublic static int getMaxIndex(Comparable anArray[], int last)
{// pre: 0 <= last < anArray.length// post: return the index of the max value in the // given Array up to the last index
int maxIndex; //index of largest objectint index; //index of inspected element
maxIndex = last;for (index = last - 1; index >= 0; index--) {
if (anArray[index].compareTo(anArray[maxIndex]) > 0)
maxIndex = index;} }
code based on Bailey pg. 82
©Duane Szafron 1999
17
Counting ComparisonsCounting Comparisons
How many comparison operations are required for a How many comparison operations are required for a selection sort of an n-element collection?selection sort of an n-element collection?
The sort method executes getLargestgetLargest in a loop for the indexes: k = n-1, n-2, … 1.
Each time getLargest is executed for an index, k, it does a comparison in a loop for the indexes: k-1, k-2, … 0. This is k comparisonsk comparisons.
The total number of comparisons is:
(n-1) + (n-2) + … + 1= (1 + 2 + … n) - 1 =(n-1) + (n-2) + … + 1= (1 + 2 + … n) - 1 =
n(n + 1)n(n + 1) - 1 = O(n - 1 = O(n22).). 22
©Duane Szafron 1999
18
Counting AssignmentsCounting Assignments
How many assignment operations are required for a How many assignment operations are required for a selection sort of an n-element collection?selection sort of an n-element collection?
The only time we do an assignment is in a reference exchange which takes three assignments.
The sort method executes swap() in a loop for the indexes: k = n-1, n-2, … 1.
The total number of assignments is:
3*(n-1) = O(n).
©Duane Szafron 1999
19
Time Complexity of Selection SortTime Complexity of Selection Sort
The number of comparisons and assignments is independent of the dataindependent of the data (the initial order of elements doesn’t matter).
Therefore, the bestbest, averageaverage and worstworst case time complexities of the Selection Sort are all O(n2).
©Duane Szafron 1999
20
Space Complexity of Selection Space Complexity of Selection SortSort
Besides the collection itself, the only extra storage for this sort is the single temp reference used in the swap method.
Therefore, the space complexity of Selection Sort is O(n).
©Duane Szafron 1999
21
Sorting Using VectorsSorting Using Vectors
When we are sorting elements in a Vector, the Vector knows both its logical and physical size so we don’t need any extra parameters in the sort method.
However, we must access the elements in a Vector using the message elementAt() and we must cast the elements as we access them.
We must modify elements in a Vector using the message setElementAt().
Using access methods adds execution time.
©Duane Szafron 1999
22
Selection Sort Code - Selection Sort Code - VectorsVectors
public static void selectionSort(Vector aVector) {// post: values in aVector are in ascending order
int maxIndex; //index of largest objectint index; //index of last unsorted element
for (index = aVector.size()-1; index > 0; index--) {maxIndex = this.getMaxIndex(aVector,
index);this.swap(aVector, index, maxIndex);
}}
©Duane Szafron 1999
23
Method - getMaxIndex() - VectorsMethod - getMaxIndex() - Vectors
public static int getMaxIndex(Vector aVector, int last) {// pre: 0 <= last < aVector.size// pre: 0 <= last < aVector.size// post: return the index of the max value in the // post: return the index of the max value in the // given Vector up to the last index// given Vector up to the last index
int maxIndex; //index of largest object//index of largest objectint index; //index of inspected element//index of inspected element
maxIndex = last;for (index = last - 1; index >= 0; index--) {
if (((Comparable) aVector.elementAt(index)).compareTo( (Comparable) aVector.elementAt(maxIndex)) > 0)
maxIndex = index; } }