1 cse1301 computer programming: lecture 28 list sorting

26
1 CSE1301 Computer Programming: Lecture 28 List Sorting

Post on 19-Dec-2015

232 views

Category:

Documents


2 download

TRANSCRIPT

1

CSE1301 Computer Programming:

Lecture 28List Sorting

2

Topics

• Sorting lists

• Selection sort

• Insertion sort

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

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

1 2 30

7 195newA:

1 2 30

5newA:

Selection Sort -- Example

1 2 30

5 7 1219a:a:1 2 30

5

1 2 30

5 7 1219a:a:1 2 30

...etc...

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

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

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

1 2 30

7 12 195a: 19

1 2 30

7 19 125a: 12

1 2 30

19 7 125a: 7

Selection Sort -- Example

1 2 30

7 12 195a:

1 2 30

5 7 1219a:a:1 2 30

5

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

1 2 30

7 12 195a: 19

1 2 30

7 12 195a: 12

1 2 30

12 7 195a: 7

1 2 30

12 7 519a: 5

Selection Sort -- Examplecount posMin 0 3

1 2

2 2

3 3

1 2 30

7 12 195a: 4 3

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

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

Insertion Sort -- Example

1 2 30

12 5 719a:

1 2 30

19newA:

1 2 30

1912newA:

1 2 30

12 195newA: ...etc...

15

Insertion -- Example

1 2 30

5 64

If you want to insert 3, you have to move all the others along one

1 2 30

5 64

1 2 30

5 64

1 2 30

4 5 6 So that now you have somewhere to put the 3

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

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

Insertion Sort -- Example

1 2 30

12 5 719a:

1 2 30

19 5 712a:

1 2 30

12 19 75a:

1 2 30

7 12 195a:

19

Insertion Sort: AlgorithminsertionSort( array )

{ Take the next unsorted element in the

arrayInsert it into the correct position in the

sorted part of the array}

20

Insertion Sort: AlgorithminsertionSort( 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

Insertion Sort -- Example

1 2 30

12 5 719a:

1 2 30

19 5 719a:

count val pos

1 12 0

1 12 -11 2 30

5 719a: 1219

1 2 30

19 5 712a:

12

22

Insertion Sort -- Example (cont)

1 2 30

12 19 75a:

count val pos

2 5 1

2 5 -1

1 2 30

19 5 712a:

1 2 30

5 712a: 19 19

1 2 30

19 712a: 1912

2 5 0

1 2 30

12 19 712a: 5

23

Insertion Sort -- Example (cont)

1 2 30

12 19 75a:

count val pos

3 7 2

3 7 0

3 7 11 2 30

12 19 75a: 19

1 2 30

12 19 195a: 12

1 2 30

12 12 195a: 7

1 2 30

7 12 195a:

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

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

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