Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE1301 Computer Programming: Lecture 28 List Sorting.

Similar presentations


Presentation on theme: "1 CSE1301 Computer Programming: Lecture 28 List Sorting."— Presentation transcript:

1 1 CSE1301 Computer Programming: Lecture 28 List Sorting

2 2 Topics Sorting lists Selection sort Insertion sort

3 3 Sorting Aim: –start with an unsorted array –end with a sorted array How to sort student records? –depends on purpose –by name, ID number, marks Exercise: how to sort words?

4 4 Selection Sort with two arrays Basic idea: –find the smallest element –put it at the start of the new array –find the next smallest element –put it in the second position of the new array –find the next smallest element –put it in the third element of the array –...etc

5 5 1230 7195 newA: 1230 5 Selection Sort -- Example 1230 571219 a: 1230 5 1230 571219 a: 1230...etc...

6 6 Selection Sort with only one array Basic idea: –You can do selection sort "in place" –So that you don't need to duplicate the whole array –Instead you need to rearrange some of the elements a bit –So there are more operations to do, but it takes up less space

7 7 Selection Sort with only one array Basic idea: –find the smallest element –swap it with the first element of the array –find the next smallest element –swap it with the second element of the array –find the next smallest element –swap it with the third element of the array –...etc

8 8 Selection Sort generalise: –find the smallest element –swap it with the first unsorted element of the array –repeat with the part of the array which is still unsorted

9 9 1230 712195 a: 19 1230 7 125 a: 12 1230 197125 a: 7 Selection Sort -- Example 1230 712195 a: 1230 571219 a: 1230 5

10 10 Selection Sort: Algorithm and Code selectionSort(array, N) { set count to 0 while ( count < N ) { Find the smallest element between count and N-1 set posMin to be the index of that element swap item at position posMin with item at position count add 1 to count } }

11 11 1230 712195 a: 19 1230 712195 a: 12 1230 7195 a: 7 1230 127519 a: 5 Selection Sort -- Example count posMin 0 3 1 2 2 2 3 3 1230 712195 a: 4 3

12 12 Selection Sort Analysis What is the time complexity of this algorithm? Worst case == Best case == Average case Each iteration performs a linear search on the rest of the array For each position in the array, you look for the next smallest element in the rest of the array So it's quite expensive.

13 13 Insertion Sort with one array Basic idea (sorting cards): 1.Take the first item in the array 2.Insert the item at the start of the new array 3.Take the next item in the array 4.Insert that in the correct place in the new array 5.Repeat steps 3 & 4 until you've finished the first array

14 14 Insertion Sort -- Example 1230 125719 a: 1230 19 newA: 1230 1912 newA: 1230 12195 newA:...etc...

15 15 Insertion -- Example 1230 564 If you want to insert 3, you have to move all the others along one 1230 564 1230 564 1230 456 So that now you have somewhere to put the 3

16 16 Insertion Sort with only one array Insertion Sort can also be done "in place" Again you save space Think about how many extra operations there might be...

17 17 Insertion Sort with only one array Basic idea (sorting cards): –Take the first unsorted item (assume that the portion of the array in front of this item is sorted) –Insert the item in the correct position in the sorted part of the array –When you insert something in an array, you have to move every subsequent element up by one position

18 18 Insertion Sort -- Example 1230 125719 a: 1230 195712 a: 1230 121975 a: 1230 712195 a:

19 19 Insertion Sort: Algorithm insertionSort( array ) { Take the next unsorted element in the array Insert it into the correct position in the sorted part of the array }

20 20 Insertion Sort: Algorithm insertionSort( array ) { set count to 1 while ( count < N ) { set val to array[count] set pos to count-1 while (pos is in the array and val<item in pos) { shuffle item in pos one place to right decrement pos by 1 } put val in pos+1 add 1 to count } }

21 21 Insertion Sort -- Example 1230 125719 a: 1230 1957 a: count val pos 1 12 0 1 12 -1 1230 5719 a: 12 19 1230 5712 a: 12

22 22 Insertion Sort -- Example (cont) 1230 121975 a: count val pos 2 5 1 2 5 -1 1230 195712 a: 1230 5712 a: 19 1230 712 a: 19 12 2 5 0 1230 1219712 a: 5

23 23 Insertion Sort -- Example (cont) 1230 121975 a: count val pos 3 7 2 3 7 0 3 7 1 1230 121975 a: 19 1230 1219 5 a: 12 1230 195 a: 7 1230 712195 a:

24 24 Insertion Sort Analysis What is the time complexity of this algorithm? Worst case > Average case > Best case Each iteration inserts an element at the start of the array, shifting all sorted elements along

25 25 Summary Insertion and Selection sort Both quite expensive in the worst case Selection sort is expensive in the best case too But Insertion sort is good in the best case

26 26 Reading Forouzan and Gilberg, Section 8.5 Selection sort -- –Knuth, Sorting and Searching, pages 139-142 (ed 1), pages 138-141 (ed 2) Insertion sort -- –Brookshear, pages 154-157 –Knuth, Sorting and Searching, pages 80-82


Download ppt "1 CSE1301 Computer Programming: Lecture 28 List Sorting."

Similar presentations


Ads by Google