1 / 24

Sort an array - the  selection sort  algorithm

Sort an array - the  selection sort  algorithm. Selection Sort. Selection sort a  classic computer algorithm  that is used to  sort an array The  selection sort algorithm  can be used to  sort  any data set where there is an  ordering  among the data items. Example Problem.

Télécharger la présentation

Sort an array - the  selection sort  algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sort an array - the selection sort algorithm

  2. Selection Sort • Selection sort a classic computer algorithm that is used to sort an array • The selection sort algorithm can be used to sort any data set where there is an ordering among the data items.

  3. Example Problem • You are given the following 5 numbers: 6.4 2.5 1.2 2.2 1.1 • Sort the number in ascending order. I.e., form the following sequence: 1.1 1.2 2.2 2.5 6.4

  4. Overview of the Algorithm (1) • Find the minimum value in the list of number • Swap the minimum value with the value in the first position

  5. Overview of the Algorithm (2) • Repeat the steps above for the remainder of the list • (starting at the second position and advancing each time)

  6. Overview of the Algorithm (3)

  7. Pseudo code of the Selection Sort algorithm (1) • Let: a = array containing the values • Let: n = # of elements • 1. Find the array element with the min. value among a[0], a[1], ..., a[n-1]; • 2. Swap this element with a[0]

  8. Pseudo code of the Selection Sort algorithm (2) • Repeat: • 1. Find the array element with the min. value among a[1], ..., a[n-1]; • 2. Swap this element with a[1];

  9. Pseudo code of the Selection Sort algorithm (3) • Repeat: • 1. Find the array element with the min. value among a[2], ..., a[n-1]; • 2. Swap this element with a[2]; • And so on (until we reach the last element in the array)

  10. Algorithm (1)

  11. Algorithm (2)

  12. 2 (smaller) Problems • Find the array element with the min. value among a[i], ..., a[n-1] (for some given value i) • Swap two array elements

  13. Solving Subproblem 1 (1) Given the array elements a[i], a[i+1], ..., a[n-1] (n = a.length): Find the array element with the minimum value among the array elements a[i], a[i+1], ..., a[n-1]

  14. Solving Subproblem 1 (2)

  15. Refine the Selection Sort algorithm. • We have just develop an algorithm to solve subproblem 1. • Insert the algorithm and we obtain a more refined (more detailed) algorithm:

  16. Solving Subproblem 2 • Swap the elements a[i] and a[min_j]:

  17. Solution: the 3-way exchange algorithm (1) • Imagine you have 2 cups named A and B and each cup contains some liquids (different kinds of liquid in different cups):

  18. Solution: the 3-way exchange algorithm (2)

  19. Solution: the 3-way exchange algorithm (3) • Pourcup A into the help cup (this will free up cup A) • Pourcup B into cup A (now cup A has the correct content and it will free up cup B) • Finally, pour the help cup into cup B  (now cup Balso has the correct content)

  20. Solution: the 3-way exchange algorithm (4)

  21. Refine the Selection Sort algorithmfurther

  22. The Selection Sort algorithm - in Java

  23.  The Algorithm is Simple

  24. Develop Computer Algorithm • Formulate the algorithm using abstract operations • Refine (flesh out) the abstract steps. I.e., make the abstract steps more concrete

More Related