Sorting Animation Chapter 10

24
1 Sorting Animation Chapter 10

description

Sorting Animation Chapter 10. Selection Sort. Another way of sorting is the selection sort The main idea is to keep finding the smallest (and next smallest) items in the array And move them into correct position (swap). Selection Sort. data. small_pos. smallest. n. 0. 45. 0. k. - PowerPoint PPT Presentation

Transcript of Sorting Animation Chapter 10

Page 1: Sorting Animation Chapter 10

1

Sorting AnimationChapter 10

Page 2: Sorting Animation Chapter 10

2

Selection Sort

Another way of sorting is the selection sort

The main idea is to keep finding the smallest (and next smallest) items in the array

And move them into correct position (swap)

Page 3: Sorting Animation Chapter 10

3

Selection Sort

45 < smallest? F

45

smallest

0

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

0

n

Page 4: Sorting Animation Chapter 10

4

Selection Sort

32 < smallest? T

45

smallest

0

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

1

n

Page 5: Sorting Animation Chapter 10

5

Selection Sort

32 < smallest? T

32

smallest

1

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

1

n

Page 6: Sorting Animation Chapter 10

6

Selection Sort

56 < smallest? F

32

smallest

1

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

2

n

Page 7: Sorting Animation Chapter 10

7

Selection Sort

9 < smallest? T

32

smallest

1

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

3

n

Page 8: Sorting Animation Chapter 10

8

Selection Sort

9 < smallest? T

9

smallest

3

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

3

n

Page 9: Sorting Animation Chapter 10

9

Selection Sort

21 < smallest? F

9

smallest

3

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

4

n

Page 10: Sorting Animation Chapter 10

10

Selection Sort

77 < smallest? F

9

smallest

3

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

5

n

Page 11: Sorting Animation Chapter 10

11

Selection Sort

17 < smallest? F

9

smallest

3

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

6

n

Page 12: Sorting Animation Chapter 10

12

Selection Sort—SWAP!

9

smallest

3

small_pos

0

k

data

0 1 2 3 4 5 6 7

45 32 56 9 21 77 18 17

6

n

Swap(data[k], data[small_pos]);

Page 13: Sorting Animation Chapter 10

13

Selection Sort

32 < smallest? F

32

smallest

1

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

1

n

Page 14: Sorting Animation Chapter 10

14

Selection Sort

32 < smallest? F

32

smallest

1

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

2

n

Page 15: Sorting Animation Chapter 10

15

Selection Sort

45 < smallest? F

32

smallest

1

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

3

n

Page 16: Sorting Animation Chapter 10

16

Selection Sort

21 < smallest? T

32

smallest

1

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

4

n

Page 17: Sorting Animation Chapter 10

17

Selection Sort

21 < smallest? T

21

smallest

4

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

4

n

Page 18: Sorting Animation Chapter 10

18

Selection Sort

77 < smallest? F

21

smallest

4

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

5

n

Page 19: Sorting Animation Chapter 10

19

Selection Sort

18 < smallest? T

21

smallest

4

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

6

n

Page 20: Sorting Animation Chapter 10

20

Selection Sort

18 < smallest? T

18

smallest

6

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

6

n

Page 21: Sorting Animation Chapter 10

21

Selection Sort

17 < smallest? T

18

smallest

6

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

7

n

Page 22: Sorting Animation Chapter 10

22

Selection Sort

17 < smallest? T

17

smallest

7

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

7

n

Page 23: Sorting Animation Chapter 10

23

Selection Sort—SWAP!

17

smallest

7

small_pos

1

k

data

0 1 2 3 4 5 6 7

9 32 56 45 21 77 18 17

7

n

Swap(data[k], data[small_pos]);

Page 24: Sorting Animation Chapter 10

24

Selection Sort—and so on

56 < smallest? F

56

smallest

2

small_pos

2

k

data

0 1 2 3 4 5 6 7

9 17 56 45 21 77 18 32

2

n