Sort an array - the  selection sort  algorithm

24
Sort an array - the selection sort algorithm

description

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. - PowerPoint PPT Presentation

Transcript of 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

• 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

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

Overview of the Algorithm (2)

• Repeat the steps above for the remainder of the list 

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

Overview of the Algorithm (3)

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]

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

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)

Algorithm (1) 

Algorithm (2) 

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

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]

Solving Subproblem 1 (2)

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:

Solving Subproblem 2

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

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

Solution: the 3-way exchange algorithm (2)

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)

Solution: the 3-way exchange algorithm (4)

Refine the Selection Sort algorithm further

The Selection Sort algorithm - in Java

 The Algorithm is Simple

Develop Computer Algorithm

• Formulate the algorithm using abstract operations

• Refine (flesh out) the abstract steps.

I.e., make the abstract steps more concrete