3.4 selection sort

44
Selection Sort

Transcript of 3.4 selection sort

Selection Sort

Selection Sort: Idea

1. We have two group of items: sorted group, and unsorted group

2. Initially, all items are in the unsorted group. The sorted group is empty. We assume that items in the unsorted

group unsorted. We have to keep items in the sorted group

sorted.

Selection Sort: Cont’d

1. Select the “best” (eg. smallest) item from the unsorted group, then put the “best” item at the end of the sorted group.

2. Repeat the process until the unsorted group becomes empty.

Selection Sort Example

35 65 30 60 20 scan 0-4, smallest 20

swap 35 and 2020 65 30 60 35 scan 1-4,

smallest 30swap 65 and 30

20 30 65 60 35 scan 2-4, smallest 35

swap 65 and 3520 30 35 60 65 scan 3-4,

smallest 60swap 60 and 60

20 30 35 60 65 done

Chapter 10: Sorting

4

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 6 2

Comparison

Data Movement

Sorted

Largest

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Selection Sort

5 1 3 4 2 6

Comparison

Data Movement

Sorted

Largest

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

2 1 3 4 5 6

Comparison

Data Movement

Sorted

Largest

Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted

Selection Sort

1 2 3 4 5 6

Comparison

Data Movement

Sorted

DONE!

4240 2 1 3 3 4 0 -1 655843

40 2 1 43 3 4 0 -1 42 65583

40 2 1 43 3 4 0 -1 58 3 6542

40 2 1 43 3 65 0 -1 58 3 42 4

Selection Sort: Example

4240 2 1 3 3 4 0 655843-1

42-1 2 1 3 3 4 0 65584340

42-1 2 1 3 3 4 655843400

42-1 2 1 0 3 4 655843403

Selection Sort: Example

1

42-1 2 1 3 4 6558434030

42-1 0 3 4 6558434032

1 42-1 0 3 4 6558434032

1 420 3 4 6558434032-1

1 420 3 4 6558434032-1

Selection Sort: Example

Selection Sort: Analysis

Running time: Worst case: O(N2) Best case: O(N2)