Download presentation

Presentation is loading. Please wait.

Published byPeregrine Young Modified about 1 year ago

1
ALG0183 Algorithms & Data Structures Lecture 14 Selection Sort 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks comparison sort worse-case, average case O(N 2 ) best-case O(N 2 ) in-place algorithm (input overwritten by output) unstable sort

2
Definition 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2 A sort algorithm that repeatedly looks through remaining items to find the least one and moves it to its final location. The run time is Θ(n²), where n is the number of elements. The number of swaps is O(n).sortΘ(n²)O(n) Note: Sorting can be done in place by swapping the least remaining item with the item in the next position to be filled. However, this implementation of the algorithm is not stable.in placestable If the (first) least remaining item is inserted, that is, all intervening items moved down (instead of swapping), this algorithm is stable. However, if the items are in an array, rather than, say a linked list, the number of moves is O(n²). The algorithm is then like bubble sort with a more complicated control structure.linked listO(n²)bubble sort

3
Linked lists. Data is not stored contiguously in memory. Each record has the address of the next record. Inserting a record involves manipulating these addresses. There is no moving or swapping. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3

4
8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4 Step-by-step example

5
Pseudocode implementation From: “An analysis of selection sort using recurrence relations” Ferri & Albert 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5 The inner loop finds the minimum in the unsorted part of the list. x is the value of the minimum k is the index of the minimum

6
Gaddis code © Addison Wesley selection sort on an array of integers 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6 public static void selectionSort(int[] array) { int startScan; // Starting position of the scan int index; // To hold a subscript value int minIndex; // Element with smallest value in the scan int minValue; // The smallest value found in the scan for (startScan = 0; startScan < (array.length-1); startScan++) { minIndex = startScan; //Assume the first element in the scannable area minValue = array[startScan]; // is the smallest value. // Scan the array, starting at the 2nd element in the scannable area. for(index = startScan + 1; index < array.length; index++) { if (array[index] < minValue) { minValue = array[index]; minIndex = index; } array[minIndex] = array[startScan]; // Swap the element with the smallest value array[startScan] = minValue; // with the first element in the scannable area. }

7
Gaddis code © Addison Wesley selection sort on an array of objects 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7 public static void selectionSort(Comparable[] array) { int startScan; // Starting position of the scan int index; // To hold a subscript value int minIndex; // Element with smallest value in the scan Comparable minValue; // The smallest value found in the scan for (startScan = 0; startScan < (array.length-1); startScan++) { minIndex = startScan; //Assume the first element in the scannable area minValue = array[startScan]; // is the smallest value. // Scan the array, starting at the 2nd element in the scannable area. for(index = startScan + 1; index < array.length; index++) { if (array[index].compareTo(minValue) < 0) { minValue = array[index]; minIndex = index; } array[minIndex] = array[startScan]; // Swap the element with the smallest value array[startScan] = minValue; // with the first element in the scannable area. }

8
8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8 public void selectionSort(int[] arr) { int i, j, minIndex, tmp; int n = arr.length; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minIndex]) minIndex = j; if (minIndex != i) { tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; } If you knew you the data was a permutation, the test would be unnecessary.

9
Lewis code © Addison Wesley 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9 public static void selectionSort (Comparable[] data) { int min; for (int index = 0; index < data.length-1; index++) { min = index; for (int scan = index+1; scan < data.length; scan++) if (data[scan].compareTo(data[min]) < 0) min = scan; swap (data, min, index); }

10
Stable or unstable? Record keys are a,b, and c. There are 4 records. Two records have the same key b. Let x and y subscripts be used to distinguish records with key b. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10 bxbx byby ca Pass 1abyby cbxbx Pass 2abyby cbxbx Pass 3abyby bxbx c

11
Stable or unstable? Record keys are a,b, and c. There are 4 records. Two records have the same key b. Let x and y subscripts be used to distinguish records with key b. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 cabxbx byby Pass 1acbxbx byby Pass 2abxbx cbyby Pass 3abxbx byby c

12
Stable or unstable? Record keys are a,b, and c. There are 4 records. Two records have the same key b. Let x and y subscripts be used to distinguish records with key b. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 bxbx cbyby a Pass 1acbyby bxbx Pass 2abyby cbxbx Pass 3abyby bxbx c

13
Stable or unstable? Record keys are a,b, and c. There are 4 records. Two records have the same key b. Let x and y subscripts be used to distinguish records with key b. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 cbxbx abyby Pass 1abxbx cbyby Pass 2abxbx cbyby Pass 3abxbx byby c There are other combinations to consider: “an exercise for the reader”.

14
Figure 1: Sorting Strings in Java ©ACM data by Owen Astrachan Selection sort´s performance on random data is better than bubble sort but worse than insertion sort. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14

15
Some Big-Oh The number of comparisons is: (N-1)+(N-2)+...+1=N(N-1)/2. The number of swaps (exchanges) is: N-1. Overall, Big-Oh is O(N 2 ). In the best-case, where the input is in order, selection sort still takes just as long. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 “Every algorithm for finding the maximum of n elements, based on comparing pairs of elements, must make at least n-1 comparisons.” Donald E. Knuth

16
8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 “Despite its simplicity and evident brute-force approach, selection sort outperforms more sophisticated methods in one important application: it is the method of choice when sorting files with huge items and small keys. For such applications, the cost of moving the data dominates the cost of making comparisons, and no algorithm can sort a file with substantially less data movement than selection sort.” Robert Sedgewick

17
Static visualisation of Selection Sort “The magnitude of a number is indicated by shading - higher numbers are darker, and lower numbers are lighter.” “We begin on the left hand side with the numbers in a random order, and the sorting progression plays out until we reach the right hand side with a sorted sequence. Time, in this particular case, is measured by the number of swaps performed.” This technique works only for in-place algorithms. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17

18
Is this step-by-step trace of Selection Sort correct? 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google