1 / 15

Zabin Visram Room 9

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.

urbano
Télécharger la présentation

Zabin Visram Room 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related