FIT Objectives By the end of this lecture, students should: understand the basic principles of sorting efficiency considerations in the context of sorting be able to implement, modify and use the basic sorting algorithms Reading: Savitch, Chapter 6
FIT Sorting Aim: start with an unsorted array end with a sorted array How to sort student records? depends on purpose by name, ID number, marks If the base type of the array is a primitive type, this can be sorted directly. ( =, >) If the base type of the array is an object type the object type, the objects must be comparable (an order must be defined). This is usually done by using a comparable key instance attribute (eg. the name) Exercise: How do you make the Person class comparable according to a lexicographical ordering?
FIT Reminder: List Structure used: 3 FinnXavier??? theList ??? Dave we use a “ListInArray” class to represent a list. The class has two instance variables, an array and a counter. The size of this array dictates the maximum list length The counter (“used”) of how many array positions are used, ie. valid We now subclass “ListInArray” to obtain a class “SortedListInArray” and define sorting methods for it.
FIT Insertion Sort Basic idea: use the SortedListInArray implementation create an (empty) sorted list insert each element of the original array into the list using addSorted
FIT Insertion Sort via SortedListInArray public class SortedListInArray extends ListInArray { public SortedListInArray(int[] xs) { this(xs.length); for(int x : xs) addSorted(x); } … } … we can easily write a constructor for SortedListInArray that does this …
FIT Insertion Sort (in-situ) in-situ (lat) = in place, ie. in the same array without allocating a new data structure Basic idea: Take the first unsorted item (assume that the portion of the array in front of this item is sorted) Find the correct position create a gap by shuffling remaining items along insert new item into the gap
FIT Insertion Sort -- Example a: a: a: a:
FIT Insertion Sort: Algorithm and Code insertionSort( array ) { set count to 1 while ( count < N ) { set val to array[count] set pos to count-1 while (pos is in the array and val < item in pos) { shuffle item in pos one place to right decrement pos by 1 } put val in pos+1 add 1 to count } public void insertionSort() { for (int count=1; count<used; count++) { int val = theList[count]; int pos=count-1; boolean finished=false; while (!finished && pos>=0) { if (theList[pos]>val) theList[pos+1] = theList[pos]; else finished=true; pos--; } theList[pos+1]=val; }
FIT Insertion Sort -- Example a: a: count val pos a: a: 12
FIT Insertion Sort -- Example (cont) a: count val pos a: a: a: a: 5
FIT Insertion Sort -- Example (cont) a: count val pos a: a: a: a:
FIT Insertion Sort Analysis What is the time complexity of this algorithm? Worst case > Average case > Best case Each iteration inserts an element at the start of the array, shifting all sorted elements along second element 2 + … penultimate element N-1 + last element N Total (2+N)(N-1)/2 = O(N 2 )
FIT Selection Sort (in-situ) Basic idea: find the minimum element exchange it with the first unsorted element of the array repeat for the rest of the array
FIT a: a: a: 7 Selection Sort -- Example a: a:
FIT Selection Sort: Algorithm and Code selectionSort(array, N) { set count to 0 while ( count < N ) { set posmin to index of smallest element in rest of array swap item at posmin with item at count add 1 to count } public void selectionSort() { for (int i=0; i<used; i++) {int minIndex = findMin(i); swap(i, minIndex); }
FIT Selection Sort: Algorithm and Code selectionSort(array, N) { set count to 0 while ( count < N ) { set posmin to index of smallest element in rest of array swap item at posmin with item at count add 1 to count } private int findMin(int startIndex) { int posmin=startIndex; for (int i=posmin; i<used; i++) if(theList[i]<theList[posmin]) posmin=i; return posmin; } private void swap(int i, int j) { int tmp = theList[i]; theList[i]=theList[j]; theList[j]=tmp; }
FIT a: a: a: a: 5 Selection Sort -- Example count posmin a: 4 3
FIT Selection Sort Analysis What is the time complexity of this algorithm? Worst case == Best case == Average case Each iteration performs a linear search on the rest of the array first element N + second element N-1 + … penultimate element 2 + last element 1 Total N(N+1)/2 = O(N 2 )
FIT Bubble Sort Basic idea ( lighter bubbles rise to the top ): Exchange neighbouring items until the largest item reaches the end of the array Repeat for the rest of the array
FIT a: Bubble Sort -- Example a: a: a: a: a: a: a: a: a:
FIT Bubble Sort: Algorithm and Code bubbleSort( array, N ) { set bound to N-1 set swapped to 1 while ( swapped > 0) { set swapped to 0 set count to 0 while ( count < bound ) { if ( array[count] > array[count+1] ) { swap array[count] and array[count+1] set swapped to count } increment count by 1 } set bound to swapped } public void bubbleSort() { int bound = used-1; int swapped = 1; `while (swapped >0) { swapped = 0; for (int count=0; count<bound; count++) if (theList[count] > theList[count+1]) { swap(count, count+1); swapped=count; } bound = swapped; }
FIT a: a: Bubble Sort -- Example a: a: a: 1230 bound ct swapped
FIT Bubble Sort -- Example (cont) 1230 a: a: a: a: a: a: bound ct swapped
FIT Bubble Sort Analysis What is the time complexity of this algorithm? Worst case > Average case > Best case Each iteration compares all the adjacent elements, swapping them if necessary first iteration N + second iteration N-1 + … last iteration 1 TotalN(1+N)/2 = O(N 2 )
FIT Summary Insertion, Selection and Bubble sort: Worst case time complexity is O(N 2 ) Best sorting routines are O(N log(N)) mergesort quicksort bottom-up heapsort In FIT1008 next semester