Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting - Selection Sort Cmput 115 - Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.

Similar presentations


Presentation on theme: "Sorting - Selection Sort Cmput 115 - Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is."— Presentation transcript:

1 Sorting - Selection Sort Cmput 115 - 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

2 ©Duane Szafron 1999 2 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.

3 ©Duane Szafron 1999 3 Outline Selection Sort Algorithm Selection Sort - Arrays Time and Space Complexity of Selection Sort Selection Sort - Vectors

4 ©Duane Szafron 1999 4 The Sort Problem Given a collection, with elements that can be compared, put the elements in increasing or decreasing order. 603010204090708050 012345678 102030405060708090 012345678

5 ©Duane Szafron 1999 5 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. 603010204090708050 012345678 603010204050708090 012345678 largest last

6 ©Duane Szafron 1999 6 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. 603010204050708090 012345678 603010204050708090 012345678 No exchanges necessary

7 ©Duane Szafron 1999 7 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. 603010204050708090 012345678 603010204050708090 012345678 No exchanges necessary

8 ©Duane Szafron 1999 8 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. 503010204060708090 012345678 603010204050708090 012345678

9 ©Duane Szafron 1999 9 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. 403010205060708090 012345678 503010204060708090 012345678

10 ©Duane Szafron 1999 10 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. 203010405060708090 012345678 403010205060708090 012345678

11 ©Duane Szafron 1999 11 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. 201030405060708090 012345678 203010405060708090 012345678

12 ©Duane Szafron 1999 12 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. 102030405060708090 012345678 201030405060708090 012345678

13 ©Duane Szafron 1999 13 Selection Sort Algorithm 9 Stop. 102030405060708090 012345678

14 ©Duane Szafron 1999 14 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?

15 ©Duane Szafron 1999 15 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

16 ©Duane Szafron 1999 16 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

17 ©Duane Szafron 1999 17 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= (1 + 2 + … n) - 1 = n(n + 1) - 1 = O(n 2 ). 2

18 ©Duane Szafron 1999 18 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).

19 ©Duane Szafron 1999 19 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 ).

20 ©Duane Szafron 1999 20 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).

21 ©Duane Szafron 1999 21 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.

22 ©Duane Szafron 1999 22 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); }

23 ©Duane Szafron 1999 23 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; } }


Download ppt "Sorting - Selection Sort Cmput 115 - Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is."

Similar presentations


Ads by Google