Sorting - Selection Sort Cmput Lecture 10 Department 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 About 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 Outline Selection Sort Algorithm Selection Sort - Arrays Time and Space Complexity of Selection Sort Selection Sort - Vectors
©Duane Szafron The Sort Problem Given a collection, with elements that can be compared, put the elements in increasing or decreasing order
©Duane Szafron Selection Sort Algorithm 1 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index largest last
©Duane Szafron Selection Sort Algorithm 2 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index No exchanges necessary
©Duane Szafron Selection Sort Algorithm 3 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index No exchanges necessary
©Duane Szafron Selection Sort Algorithm 4 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index
©Duane Szafron Selection Sort Algorithm 5 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index
©Duane Szafron Selection Sort Algorithm 6 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index
©Duane Szafron Selection Sort Algorithm 7 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index
©Duane Szafron Selection Sort Algorithm 8 Find the index of the largest element and exchange references with the element at the last index. Decrement the last index
©Duane Szafron Selection Sort Algorithm 9 Stop
©Duane Szafron Sorting 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 Selection Sort Code - Arrays public static void selectionSort(Comparable anArray[ ], int size) { // pre: 0 <= size <= anArray.length // post: values in anArray are in ascending order intmaxIndex; //index of largest object int 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 Method - getMaxIndex() - Arrays public 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 intmaxIndex; //index of largest object int 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 Counting Comparisons How many comparison operations are required for a selection sort of an n-element collection? How many comparison operations are required for a selection sort of an n-element collection? getLargest The sort method executes getLargest in a loop for the indexes: k = n-1, n-2, … 1. k comparisons 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 comparisons. The total number of comparisons is: (n-1) + (n-2) + … + 1= ( … n) - 1 = n(n + 1) - 1 = O(n 2 ). 2
©Duane Szafron Counting Assignments How many assignment operations are required for a selection sort of an n-element collection? How many assignment operations are required for a 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 Time Complexity of Selection Sort independent of the data The number of comparisons and assignments is independent of the data (the initial order of elements doesn’t matter). bestaverageworst Therefore, the best, average and worst case time complexities of the Selection Sort are all O(n 2 ).
©Duane Szafron Space Complexity of Selection Sort 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 Sorting 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 Selection Sort Code - Vectors public static void selectionSort(Vector aVector) { // post: values in aVector are in ascending order intmaxIndex; //index of largest object int 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 Method - getMaxIndex() - Vectors public static int getMaxIndex(Vector aVector, int last) { // pre: 0 <= last < aVector.size // post: return the index of the max value in the // given Vector up to the last index //index of largest object intmaxIndex; //index of largest object //index of inspected element int index; //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; } }