Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Sorting: Selection Sort Damian Gordon

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

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

4 Sorting: Selection Sort 4423423316543418 01234567

5 Sorting: Selection Sort 4423423316543418 01234567

6 Sorting: Selection Sort 4423423316543418 01234567

7 Sorting: Selection Sort 1623423344543418 01234567

8 Sorting: Selection Sort 1623423344543418 01234567

9 Sorting: Selection Sort 1623423344543418 01234567

10 Sorting: Selection Sort 1618423344543423 01234567

11 Sorting: Selection Sort 1618423344543423 01234567

12 Sorting: Selection Sort 1618423344543423 01234567

13 Sorting: Selection Sort 1618233344543442 01234567

14 Sorting: Selection Sort 1618233344543442 01234567

15 Sorting: Selection Sort 1618233344543442 01234567

16 Sorting: Selection Sort 1618233344543442 01234567

17 Sorting: Selection Sort 1618233344543442 01234567

18 Sorting: Selection Sort 1618233334544442 01234567

19 Sorting: Selection Sort 1618233334544442 01234567

20 Sorting: Selection Sort 1618233334544442 01234567

21 Sorting: Selection Sort 1618233334424454 01234567

22 Sorting: Selection Sort 1618233334424454 01234567

23 Sorting: Selection Sort 1618233334424454 01234567

24 Sorting: Selection Sort 1618233334424454 01234567

25 Sorting: Selection Sort 1618233334424454 01234567

26 Sorting: Selection Sort So we know we only have to swap if the value is not already in the correct location.

27 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:

28 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;

29 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

30 So we can put these two bits together,

31 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

32

33 etc.


Download ppt "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."

Similar presentations


Ads by Google