Monday Week 11 1,000 numbers 10,000 numbers 100,000 numbers 1/15 - - PowerPoint PPT Presentation

monday week 11
SMART_READER_LITE
LIVE PREVIEW

Monday Week 11 1,000 numbers 10,000 numbers 100,000 numbers 1/15 - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

11/10/2014 12:24 pm Monday Week 11 Page 1 of 5 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html

Monday Week 11 Sorting Algorithms

1/15

We will discuss three sorting algorithms Selection Sort Insertion Sort Quick Sort

Selection Sort

2/15

This is the sorting method we saw last week. Basic algorithm (based on arrays): Starting with the first element in a list if this is the last element in the list, you have finished find the minimum in the list, and swap with the first element redefine the list as starting with the next element do next iteration Example animation

... Selection Sort

3/15 void selSort(int array[], int n) { // an 'in place' sort so ... int i; // ... the array is 'overwritten' int min; int marker; for (marker=0; marker < n; marker++) { min = marker; // assume element 'marker' is min for (i = marker+1; i < n; i++) { if (array[i] < array[min]) { min = i; // found better min at 'i' } } int tmp; tmp = array[marker]; array[marker] = array[min]; // swap elements 'min' ... array[min] = tmp; // ... and 'marker' } }

Exercise: Performance of Selection Sort

4/15

11/10/2014 12:24 pm Monday Week 11 Page 2 of 5 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html

Measure performance of selection sort using the UNIX time command 1,000 numbers 10,000 numbers 100,000 numbers

randomly generated already ordered reverse ordered (numbers in descending order)

... Exercise: Performance of Selection Sort

5/15

Selection sort is typical of a class of quadratic algorithms these have quadratic performance quadratic means to the power of 2 execution time is proprtional to the square of the input N

double N, quadruple the time triple N, increase the time by factor 9 increase N by a factor of 10, it takes 102 = 100 times as long to execute increase N by a factor of 100, it takes 1002 = 10000 times as long to execute etc

This is slower than linear algorithms (e.g. random number generation)

Selection Sort on Linked Lists

6/15

Sort algorithms are usually implemented using arrays: allow random access

every element can be accessed immediately

are implemented efficiently/concisley Sort algorithms can beimplemented on other data structures linked lists even stacks (really!?) It depends on the sort algorithm whether this is very inefficient or not

... Selection Sort on Linked Lists

7/15

In Selection Sort

  • utside loop:

marker starts off at head of list

slide-2
SLIDE 2

11/10/2014 12:24 pm Monday Week 11 Page 3 of 5 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html

and steps through until the end inner loop: starts off at marker->next and goes to the end of the list to swap elements: re-direct pointers

Insertion Sort

8/15

Often used by card players. Basic algorithm: 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 note: after k iterations, a sorted list of length k has been produced Example animation

... Insertion Sort

9/15 void insertionSort(int array[], int n) { int i; for (i = 1; i < n; i++) { int marker = array[i]; // for this element ... int j; // ... work down the ordered list for (j=i; j > 0 && marker < array[j-1]; j--) { // ... and move ordered elements up array[j] = array[j-1]; } array[j] = marker; // insert element in correct position } }

Exercise: Performance of Insertion Sort

10/15

Measure performance of insertion sort using the UNIX time command 1,000 numbers 10,000 numbers 100,000 numbers

randomly generated already ordered reverse ordered (numbers in descending order)

11/10/2014 12:24 pm Monday Week 11 Page 4 of 5 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html

... Exercise: Performance of Insertion Sort

11/15

If array[i] >= array[i-1] then the inner loop does nothing and no element is moved Hence, for ordered input to know an element is in the right position needs 1 comparison each time total number of comparisons = (n-1) total number of moves = 0 best case: linear performance

... Exercise: Performance of Insertion Sort

12/15

If array[i] < array[0] then the inner loop goes all the way down to 0 and moves up every element below i Hence, for reversed input total number of moves in worst case =

(n-1) moves for element n + (n-2) moves for element n-1 + (n-3) moves for element n-2 + ... + 2 moves for element 3 + 1 move for element 2 + 0 moves for element 1

General formula: 1 + 2 + ... + (n-1) + n = n*(n+1)/2 = (n2-n)/2 Total number of comparisons is the same. Insertion sort is quadratic

Sorting with Stacks

13/15

The quadratic sorts can also be implemented using stacks not random-access; restricted to pushing and popping this restriction makes implementing more difficult

... Sorting with Stacks

14/15

Assume we have

slide-3
SLIDE 3

11/10/2014 12:24 pm Monday Week 11 Page 5 of 5 http://www.cse.unsw.edu.au/~cs1921/14s2/slides/week11a/notes.html

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)

... Sorting with Stacks

15/15

Produced: 11 Oct 2014