Top Banner
Sort an array - the selection sort algorithm
24

Sort an array - the  selection sort  algorithm

Dec 30, 2015

Download

Documents

chloe-abbott

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sort an array - the  selection sort  algorithm

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

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

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

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

Overview of the Algorithm (3)

Page 7: Sort an array - the  selection sort  algorithm

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

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

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

Algorithm (1) 

Page 11: Sort an array - the  selection sort  algorithm

Algorithm (2) 

Page 12: Sort an array - the  selection sort  algorithm

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

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

Solving Subproblem 1 (2)

Page 15: Sort an array - the  selection sort  algorithm

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

Solving Subproblem 2

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

Page 17: Sort an array - the  selection sort  algorithm

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

Solution: the 3-way exchange algorithm (2)

Page 19: Sort an array - the  selection sort  algorithm

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

Solution: the 3-way exchange algorithm (4)

Page 21: Sort an array - the  selection sort  algorithm

Refine the Selection Sort algorithm further

Page 22: Sort an array - the  selection sort  algorithm

The Selection Sort algorithm - in Java

Page 23: Sort an array - the  selection sort  algorithm

 The Algorithm is Simple

Page 24: Sort an array - the  selection sort  algorithm

Develop Computer Algorithm

• Formulate the algorithm using abstract operations

• Refine (flesh out) the abstract steps.

I.e., make the abstract steps more concrete