160 likes | 335 Views
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.
E N D
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 CS126 ZV 2004 Selection Sort
Task in pairs – sort the following books in order of height shortest book first – how would you do it? • There are several ways CS126 ZV 2004 Selection Sort
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 CS126 ZV 2004 Selection Sort
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. CS126 ZV 2004 Selection Sort
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. Selection Sort – Array Based List CS126 ZV 2004 Selection Sort
List of 10 elements Initially the entire list is unsorted. So we find the smallest item in the list, which is at position 7 Smallest Unsorted list Smallest element of the unsorted list CS126 ZV 2004 Selection Sort
List of 10 elements Initially the entire list is unsorted. So we find the smallest item in the list, which is at position 7 Smallest Unsorted list Smallest element of the unsorted list 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] CS126 ZV 2004 Selection Sort
List after swapping list [0] and list [7] 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 Unsorted list Smallest element in the unsorted portion of the list CS126 ZV 2004 Selection Sort
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]) swap Swap list[1] with list[3] Unsorted list CS126 ZV 2004 Selection Sort
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]) swap Swap list[1] with list[3] Unsorted list List after swapping list[1] with list[3] Unsorted list CS126 ZV 2004 Selection Sort
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. CS126 ZV 2004 Selection Sort
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; • } CS126 ZV 2004 Selection Sort
exercise • Trace the steps that a selection sort takes when sorting the following array in ascending order • 7 4 1 2 6 CS126 ZV 2004 Selection Sort
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 CS126 ZV 2004 Selection Sort
Demonstration of Selection Sort http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/SSort.html CS126 ZV 2004 Selection Sort