Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
ALG0183 Algorithms & Data Structures
Lecture 14
Selection Sort
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
comparison sort
worse-case, average case O(N2)
best-case O(N2)
in-place algorithm
(input overwritten by output)
unstable sort
1
Definition
http://www.itl.nist.gov/div897/sqg/dads/HTML/selectionSort.html
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).
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.
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.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
2
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
Step-by-step example
http://www.algolist.net/Algorithms/Sorting/Selection_sort
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
4
Pseudocode implementation
From: “An analysis of selection sort using recurrence relations” Ferri & Albert
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
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
5
Gaddis code © Addison Wesley
selection sort on an array of integers
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.
}
8/25/2009
}
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
6
Gaddis code © Addison Wesley
selection sort on an array of objects
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/25/2009
}
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
7
code @ http://www.algolist.net/Algorithms/Sorting/Selection_sort
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.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
8
Lewis code © Addison Wesley
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);
}
}
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
9
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.
bx
Pass 1 a
Pass 2 a
Pass 3 a
8/25/2009
by
by
by
by
c
c
c
bx
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
a
bx
bx
c
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.
c
Pass 1 a
Pass 2 a
Pass 3 a
8/25/2009
a
c
bx
bx
bx
bx
c
by
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
by
by
by
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.
bx
Pass 1 a
Pass 2 a
Pass 3 a
8/25/2009
c
c
by
by
by
by
c
bx
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
a
bx
bx
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.
c
Pass 1 a
Pass 2 a
Pass 3 a
bx
bx
bx
bx
a
c
c
by
by
by
by
c
There are other combinations to consider: “an exercise for the reader”.
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
13
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
n(n 1)
x
2
x 1
n
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(N2).
• In the best-case, where the input is in order,
selection sort still takes just as long.
“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
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
15
“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
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
16
Static visualisation of Selection Sort
http://www.hatfulofhollow.com//posts/code/visualisingsorting/
• “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
Is this step-by-step trace of Selection Sort correct?
8/25/2009
ALG0183 Algorithms & Data Structures by
Dr Andy Brooks
18