Top Banner
1 CSE1301 Computer Programming: Lecture 28 List Sorting
26

1 CSE1301 Computer Programming: Lecture 28 List Sorting.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

1

CSE1301 Computer Programming:

Lecture 28List Sorting

Page 2: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

2

Topics

• Sorting lists

• Selection sort

• Insertion sort

Page 3: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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?

Page 4: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 5: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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...

Page 6: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 7: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 8: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 9: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 10: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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 }}

Page 11: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 12: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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.

Page 13: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 14: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

14

Insertion Sort -- Example

1 2 30

12 5 719a:

1 2 30

19newA:

1 2 30

1912newA:

1 2 30

12 195newA: ...etc...

Page 15: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 16: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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...

Page 17: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 18: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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:

Page 19: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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}

Page 20: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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 }}

Page 21: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 22: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 23: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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:

Page 24: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 25: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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

Page 26: 1 CSE1301 Computer Programming: Lecture 28 List Sorting.

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