Monday Week 11 11/10/2014 12:24 pm Monday Week 11 11/10/2014 12:24 pm Measure performance of selection sort using the UNIX time command Monday Week 11 1,000 numbers 10,000 numbers 100,000 numbers 1/15 Sorting Algorithms randomly generated We will discuss three sorting algorithms already ordered reverse ordered (numbers in descending order) Selection Sort Insertion Sort Quick Sort 5/15 ... Exercise: Performance of Selection Sort Selection sort is typical of a class of quadratic algorithms 2/15 Selection Sort these have quadratic performance quadratic means to the power of 2 This is the sorting method we saw last week. execution time is proprtional to the square of the input N Basic algorithm (based on arrays): double N, quadruple the time triple N, increase the time by factor 9 Starting with the first element in a list increase N by a factor of 10, it takes 10 2 = 100 times as long to execute if this is the last element in the list, you have finished increase N by a factor of 100, it takes 100 2 = 10000 times as long to execute find the minimum in the list, and swap with the first element etc redefine the list as starting with the next element do next iteration This is slower than linear algorithms (e.g. random number generation) Example animation 6/15 Selection Sort on Linked Lists ... Selection Sort 3/15 Sort algorithms are usually implemented using arrays: allow random access void selSort(int array[], int n) { // an 'in place' sort so ... every element can be accessed immediately int i; // ... the array is 'overwritten' are implemented efficiently/concisley int min; int marker; for (marker=0; marker < n; marker++) { Sort algorithms can beimplemented on other data structures min = marker; // assume element 'marker' is min for (i = marker+1; i < n; i++) { linked lists if (array[i] < array[min]) { even stacks (really!?) min = i; // found better min at 'i' } It depends on the sort algorithm whether this is very inefficient or not } int tmp; tmp = array[marker]; array[marker] = array[min]; // swap elements 'min' ... array[min] = tmp; // ... and 'marker' ... Selection Sort on Linked Lists 7/15 } } In Selection Sort 4/15 Exercise: Performance of Selection Sort outside loop: marker starts off at head of list http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html Page 1 of 5 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html Page 2 of 5
Monday Week 11 11/10/2014 12:24 pm Monday Week 11 11/10/2014 12:24 pm and steps through until the end ... Exercise: Performance of Insertion Sort 11/15 inner loop: starts off at marker->next If array[i] >= array[i-1] and goes to the end of the list to swap elements: re-direct pointers then the inner loop does nothing and no element is moved 8/15 Hence, for ordered input Insertion Sort to know an element is in the right position Often used by card players. needs 1 comparison each time total number of comparisons = (n-1) Basic algorithm: total number of moves = 0 best case: linear performance iteratively remove an element from the input data insert that element at the correct position in the already sorted list until no elements are left in the input list ... Exercise: Performance of Insertion Sort 12/15 note: after k iterations, a sorted list of length k has been produced If array[i] < array[0] Example animation then the inner loop goes all the way down to 0 and moves up every element below i 9/15 ... Insertion Sort Hence, for reversed input total number of moves in worst case = void insertionSort(int array[], int n) { (n-1) moves for element n int i; for (i = 1; i < n; i++) { + (n-2) moves for element n-1 int marker = array[i]; // for this element ... + (n-3) moves for element n-2 int j; // ... work down the ordered list + ... for (j=i; j > 0 && marker < array[j-1]; j--) { + 2 moves for element 3 // ... and move ordered elements up + 1 move for element 2 array[j] = array[j-1]; } + 0 moves for element 1 array[j] = marker; // insert element in correct position } General formula: 1 + 2 + ... + (n-1) + n = n*(n+1)/2 = (n 2 -n)/2 } Total number of comparisons is the same. Insertion sort is quadratic 10/15 Exercise: Performance of Insertion Sort 13/15 Sorting with Stacks Measure performance of insertion sort using the UNIX time command The quadratic sorts can also be implemented using stacks 1,000 numbers 10,000 numbers not random-access; restricted to push ing and pop ping 100,000 numbers this restriction makes implementing more difficult randomly generated already ordered ... Sorting with Stacks 14/15 reverse ordered (numbers in descending order) Assume we have http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html Page 3 of 5 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html Page 4 of 5
Monday Week 11 11/10/2014 12:24 pm two stacks, A and B n unsorted numbers in stack A Selection sort: 1. pop the n numbers from A and push to B but not the maximum number 2. push the maximum number onto A 3. pop the n-1 numbers from B and push them onto A 4. let n = n-1 and repeat steps 1, 2 and 3 until n = 1 5. A is now sorted (and B is of course empty) 15/15 ... Sorting with Stacks Produced: 11 Oct 2014 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html Page 5 of 5
Recommend
More recommend