33
Sorting: Selection Sort Damian Gordon

Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Embed Size (px)

Citation preview

Page 1: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting:Selection Sort

Damian Gordon

Page 2: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

• OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways that’s more natural for a person to understand.

• It’s called SELECTION SORT.

Page 3: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

• It works as follows:– Find the smallest number, swap it with the value in the first location

of the array– Find the second smallest number, swap it with the value in the

second location of the array– Find the third smallest number, swap it with the value in the third

location of the array– Etc.

Page 4: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 5: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 6: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

44 23 42 33 16 54 34 180 1 2 3 4 5 6 7Age

Page 7: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 23 42 33 44 54 34 180 1 2 3 4 5 6 7Age

Page 8: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 23 42 33 44 54 34 180 1 2 3 4 5 6 7Age

Page 9: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 23 42 33 44 54 34 180 1 2 3 4 5 6 7Age

Page 10: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 42 33 44 54 34 230 1 2 3 4 5 6 7Age

Page 11: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 42 33 44 54 34 230 1 2 3 4 5 6 7Age

Page 12: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 42 33 44 54 34 230 1 2 3 4 5 6 7Age

Page 13: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 44 54 34 420 1 2 3 4 5 6 7Age

Page 14: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 44 54 34 420 1 2 3 4 5 6 7Age

Page 15: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 44 54 34 420 1 2 3 4 5 6 7Age

Page 16: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 44 54 34 420 1 2 3 4 5 6 7Age

Page 17: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 44 54 34 420 1 2 3 4 5 6 7Age

Page 18: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 34 54 44 420 1 2 3 4 5 6 7Age

Page 19: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 34 54 44 420 1 2 3 4 5 6 7Age

Page 20: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 34 54 44 420 1 2 3 4 5 6 7Age

Page 21: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age

Page 22: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age

Page 23: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age

Page 24: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age

Page 25: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

16 18 23 33 34 42 44 540 1 2 3 4 5 6 7Age

Page 26: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

• So we know we only have to swap if the value is not already in the correct location.

Page 27: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

• So we know we only have to swap if the value is not already in the correct location.

• This will look a little something like this:

Page 28: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

• So we know we only have to swap if the value is not already in the correct location.

• This will look a little something like this:

IF (MinValueLocation != CurrentLocation) THEN Swap(Age[CurrentLocation], Age[MinValueLocation];ENDIF;

Page 29: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

• We will find the minimum value in the list using something like the following:

MinValueLocation <- 0; FOR Index IN IncreasingCount TO N-1 DO IF (Age[Index] < Age[MinValueLocation]) THEN MinValueLocation <- Index; ENDIF; ENDFOR;

Sorting: Selection Sort

Page 30: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

• So we can put these two bits together,

Page 31: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

PROGRAM SelectionSort: Integer Age[8] <- {44,23,42,33,16,54,34,18};

FOR Outer-Index IN 0 TO N-1 MinValueLocation <- Outer-Index; FOR Index IN Outer-Index+1 TO N-1 DO IF (Age[Index] < Age[MinValueLocation]) THEN MinValueLocation <- Index; ENDIF; ENDFOR; IF (MinValueLocation != Outer-Index) THEN Swap(Age[Outer-Index], Age[MinValueLocation]); ENDIF;

ENDFOR;END.

Sorting: Selection Sort

Page 32: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

Sorting: Selection Sort

Page 33: Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways

etc.