Presentation is loading. Please wait.

Presentation is loading. Please wait.

FIT1002 2006 1 Objectives By the end of this lecture, students should: understand the basic principles of sorting efficiency considerations in the context.

Similar presentations


Presentation on theme: "FIT1002 2006 1 Objectives By the end of this lecture, students should: understand the basic principles of sorting efficiency considerations in the context."— Presentation transcript:

1 FIT1002 2006 1 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

2 FIT1002 2006 2 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?

3 FIT1002 2006 3 Reminder: List Structure used: 3 FinnXavier??? 012345 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.

4 FIT1002 2006 4 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

5 FIT1002 2006 5 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 …

6 FIT1002 2006 6 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

7 FIT1002 2006 7 Insertion Sort -- Example 1230 125719 a: 1230 195712 a: 1230 121975 a: 1230 712195 a:

8 FIT1002 2006 8 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; }

9 FIT1002 2006 9 Insertion Sort -- Example 1230 125719 a: 1230 1957 a: count val pos 1 12 0 1 12 -1 1230 5719 a: 12 19 1230 5712 a: 12

10 FIT1002 2006 10 Insertion Sort -- Example (cont) 1230 121975 a: count val pos 2 5 1 2 5 -1 1230 195712 a: 1230 5712 a: 19 1230 712 a: 19 12 2 5 0 1230 1219712 a: 5

11 FIT1002 2006 11 Insertion Sort -- Example (cont) 1230 121975 a: count val pos 3 7 2 3 7 0 3 7 1 1230 121975 a: 19 1230 1219 5 a: 12 1230 195 a: 7 1230 712195 a:

12 FIT1002 2006 12 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 )

13 FIT1002 2006 13 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

14 FIT1002 2006 14 1230 712195 a: 19 1230 7 125 a: 12 1230 197125 a: 7 Selection Sort -- Example 1230 712195 a: 1230 571219 a: 1230 5

15 FIT1002 2006 15 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); }

16 FIT1002 2006 16 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; }

17 FIT1002 2006 17 1230 712195 a: 19 1230 712195 a: 12 1230 7195 a: 7 1230 127519 a: 5 Selection Sort -- Example count posmin 0 3 1 2 2 2 3 3 1230 712195 a: 4 3

18 FIT1002 2006 18 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 )

19 FIT1002 2006 19 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

20 FIT1002 2006 20 1230 127195 a: Bubble Sort -- Example 1230 127195 a: 191275 1230 a: 121975 a: 1230 512719 1230 a: 1230 1230 127195 a: 1230 712195 a: 1230 712195 a: 1230 712195 a: 1230 712195 a:

21 FIT1002 2006 21 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; }

22 FIT1002 2006 22 512719 1230 a: 1230 127195 a: Bubble Sort -- Example 1230 127195 a: 191275 1230 a: 121975 a: 1230 bound ct swapped 3 0 0 3 1 1 3 2 2 2 3 2 3 1

23 FIT1002 2006 23 Bubble Sort -- Example (cont) 1230 a: 1230 127195 a: 1230 712195 a: 1230 712195 a: 1230 712195 a: 1230 712195 a: bound ct swapped 2 0 0 2 1 1 1 0 0 1 2 1 0 1 0

24 FIT1002 2006 24 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 )

25 FIT1002 2006 25 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


Download ppt "FIT1002 2006 1 Objectives By the end of this lecture, students should: understand the basic principles of sorting efficiency considerations in the context."

Similar presentations


Ads by Google