Download presentation

Presentation is loading. Please wait.

Published bySebastian Clark Modified over 4 years ago

1
CS126 ZV 2004 Selection Sort1 Zabin Visram Room 9 Lecture Structure Previous Lectures Searching Algorithms – analysis - performance Next few lecturers - Sorting Algorithms Several - sorting algorithms & some analysis on these algorithms Simple Sorts Selection Sort Bubble Sort

2
CS126 ZV 2004 Selection Sort2 Task in pairs – sort the following books in order of height shortest book first – how would you do it? There are several ways

3
CS126 ZV 2004 Selection Sort3 Did you use this method ? Select the shortest book since you want it to be the first on shelf, you remove the first book on the shelf and put the shortest book in its place. You still have a book in your hand so put it in the place – formerly occupied by the shortest book Now ignore the shortest book and repeat the process for the rest of the bookshelf This is Selection Sort in a way

4
CS126 ZV 2004 Selection Sort4 Selection Sort smallest element method The basic idea of selection sort is to scan through the list finding the smallest element and placing the element in the first position of the list.

5
CS126 ZV 2004 Selection Sort5 Selection Sort – Array Based List Let consider the smallest element sorting algorithm The selection sort algorithm sorts a list by selecting the smallest element in the(unsorted portion of the) list, and then moving this smallest element to the beginning of the (unsorted) list The first time we locate the smallest item in the entire list, the second time we locate the smallest item in the list starting from the second element in the list, and so on.

6
CS126 ZV 2004 Selection Sort6 Initially the entire list is unsorted. So we find the smallest item in the list, which is at position 7 [0][1][2][3][4][5][6][7][8][9] 163024725624556550 Smallest element of the unsorted list Unsorted list Smallest List of 10 elements

7
CS126 ZV 2004 Selection Sort7 Initially the entire list is unsorted. So we find the smallest item in the list, which is at position 7 [0][1][2][3][4][5][6][7][8][9] 163024725624556550 Smallest element of the unsorted list Unsorted list Smallest List of 10 elements [0][1][2][3][4][5][6][7][8][9] 163024725624556550 Because this is the smallest item, it must be moved to position 0. We therefore swap 16 (that is list [0] with 5 (that is list [7]) swap Swap elements list[0] and list[7]

8
CS126 ZV 2004 Selection Sort8 [0][1][2][3][4][5][6][7][8][9] 530247256245166550 Unsorted list List after swapping list [0] and list [7] [0][1][2][3][4][5][6][7][8][9] 530247256245166550 Unsorted list Now the unsorted list is list[1]…list[9], Next we find the smallest element in the unsorted portion of the list. The smallest element is at position 3 Smallest element in the unsorted portion of the list

9
CS126 ZV 2004 Selection Sort9 Unsorted list Swap list[1] with list[3] [0][1][2][3][4][5][6][7][8][9] 530247256245166550 swap Because the smallest element in the unsorted list is at position 3, it must be moved to position 1. That is, we swap 7 (list [3]) with 30 (list[1])

10
CS126 ZV 2004 Selection Sort10 Unsorted list Swap list[1] with list[3] [0][1][2][3][4][5][6][7][8][9] 530247256245166550 swap Because the smallest element in the unsorted list is at position 3, it must be moved to position 1. That is, we swap 7 (list [3]) with 30 (list[1]) Unsorted list [0][1][2][3][4][5][6][7][8][9] 572430256245166550 List after swapping list[1] with list[3]

11
CS126 ZV 2004 Selection Sort11 Consider the smallest element sorting algorithm The basic idea of this selection sort is to scan through the list finding the smallest element and placing the element in the first position of the list. That smallest element is the beginning of a sorted segment. Then we find the smallest in the remaining unsorted list and place it again at the beginning of the unsorted list.

12
CS126 ZV 2004 Selection Sort12 Consider the largest element sorting algorithm To place it at the beginning of the unsorted segment we use a standard swap method as follows: public static void swap(int data[], int i, int j) { //pre: 0 <= i, j <= data.length //post: data[i] and data[j] are exchanged int temp; temp = data[i]; data[i] = data[j]; data[j] = temp; }

13
CS126 ZV 2004 Selection Sort13 exercise Trace the steps that a selection sort takes when sorting the following array in ascending order 7 4 1 2 6

14
CS126 ZV 2004 Selection Sort14 Answer 7 4 1 2 6 1 4 7 2 6 1 2 7 4 6 1 2 4 7 6 1 2 4 6 7

15
CS126 ZV 2004 Selection Sort 15 http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/SS ort.html Demonstration of Selection Sort

Similar presentations

Presentation is loading. Please wait....

OK

CSE 373 Data Structures and Algorithms

CSE 373 Data Structures and Algorithms

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google