/---------------------\ | Notes for lecture09 | | 2 November 1999 | | CS 125-609 | | Michael Goldwasser | \---------------------/ ========================================================================= ELEMENTARY SORTING ALGORITHMS ========================================================================= Preface: Chapter 4.2 of the text discusses sorting (in particular, it discusses 'bubble sort'). We will discuss several elementary methods for sorting an array, in particular 'insertion sort', 'selection sort', as well as 'bubble sort'. ========================================================================= SELECTION SORT The general idea is to go through the entire array looking for the smallest item. Once you have found this item, you can place this item in the first array location (swapping whatever used to be there into the vacated spot so as not to lose track of that item). Now go through and select the second-smallest item, and so on. ------------------------------------------------------------------------ Pseudocode: Dim Selection As Integer ' we are selecting the smallest Dim Look As Integer ' loop variable when looking at remaining items Dim SoFar As Integer ' index of smallest item found thus far For Selection = 1 to size-1 ' ? why only size-1 ? SoFar = Selection ' a default value For Look = Selection+1 to size ' ? why those loop values ? If Array(Look) < Array(SoFar) Then SoFar = Look ' found an even smaller item! End If Next Look Swap Array(SoFar) and Array(Selection) ' place the selected item ' into its correct location Next Selection ------------------------------------------------------------------------ ========================================================================= INSERTION SORT General idea is to look at only the beginning part of the array, adding one new item at a time while keeping the beginning of the array sorted. ------------------------------------------------------------------------ Pseudocode: For NewIndex = 2 to size ' we want Array(1 to NewIndex) to be sorted NewValue = Array(NewIndex) ' must open up correct spot to place ' item currently at Array(NewIndex) i = NewIndex-1 ' starting one spot to the left Do While NewValue < Array(i) ' go until we find the correct place: Array(i+1) = Array(i) ' slide item i to the right i = i-1 ' keep moving left Loop Array(i+1) = NewValue ' place NewItem to the right of where ' we finally got stuck Next Partial ------------------------------------------------------------------------ Bug as written --- what if the current item is the smallest so far?! ========================================================================= BUBBLE SORT General idea is to do selection sort, but do a little extra work while scanning down the list (in hopes that the extra work will pay off later). To be consistent with the book, instead of selecting starting with the smallest, we consider selection starting with the largest element. Scan from left to right, but instead of just keeping track of the largest thus far, whenever you see two neighbors that are out of order, swap them and continue. Notice that whereever the biggest item is in the array, it will always be out of place compared to its neighbor and so it will keep getting swapped (bubbling) to the far end of the array. Now repeat this process to place the second largest item, and so on... ------------------------------------------------------------------------ Pseudocode: For Pass = 1 to size-1 ' ? why only size-1 passes ? For Walk = 1 to size - pass ' ? are limits correct ? If Array(Walk) > Array(Walk+1) Then Swap Array(Walk) and Array(Walk+1) ' swap the neighbors End If Next Walk Next Pass ------------------------------------------------------------------------ ========================================================================= DISCLAIMER: All of the pseudocode given here is just that (did you know that 'pseudo' is Greek for 'lie'). If you want to see code that works, please look at the program which is provided as part of lab9. =========================================================================