Sort an array - the selection sort algorithm. Selection Sort Selection sort a classic computer...

24
Sort an array - the selection sort algorithm

Transcript of Sort an array - the selection sort algorithm. Selection Sort Selection sort a classic computer...

Page 1: 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.

Sort an array - the selection sort algorithm

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

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.

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

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

Page 4: 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.

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

Page 5: 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.

Overview of the Algorithm (2)

• Repeat the steps above for the remainder of the list 

• (starting at the second position and advancing each time)

Page 6: 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.

Overview of the Algorithm (3)

Page 7: 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.

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]

Page 8: 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.

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

Page 9: 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.

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)

Page 10: 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 (1) 

Page 11: 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 (2) 

Page 12: 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.

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

Page 13: 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.

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]

Page 14: 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.

Solving Subproblem 1 (2)

Page 15: 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.

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:

Page 16: 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.

Solving Subproblem 2

• Swap the elements a[i] and a[min_j]:

Page 17: 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.

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

Page 18: 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.

Solution: the 3-way exchange algorithm (2)

Page 19: 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.

Solution: the 3-way exchange algorithm (3)

• Pour cup A into the help cup 

(this will free up cup A)

• Pour cup 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 B also has the correct content)

Page 20: 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.

Solution: the 3-way exchange algorithm (4)

Page 21: 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.

Refine the Selection Sort algorithm further

Page 22: 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.

The Selection Sort algorithm - in Java

Page 23: 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.

 The Algorithm is Simple

Page 24: 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.

Develop Computer Algorithm

• Formulate the algorithm using abstract operations

• Refine (flesh out) the abstract steps.

I.e., make the abstract steps more concrete