
Revised 27/07/03 2 About This Lecture Sorting Operations - Comparisons In this lecture we will learn about the two basic operations performed by Sort and Data Movement Algorithms: comparison and element movement We will also learn how to express them as Cmput 115 - Lecture 8 a number of access operations. Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package 3 4 Outline The Sort Problem Comparing Elements Given a collection, with elements that can Moving Elements be compared, put the elements in increasing or decreasing order. 0 1 2 3 4 5 6 7 8 60 30 10 20 40 90 70 80 50 0 1 2 3 4 5 6 7 8 10 20 30 40 50 60 70 80 90 5 6 Operations Comparing Primitive Values ¡ ¡ Given a collection, with elements that can be The sorting algorithms we will consider are based on compared, put the elements in increasing or comparing individual elements. decreasing order. ¡ If the elements are primitive values, we can use the < ¡ We must perform two operations to sort a operator to compare them. collection: ¡ If the elements are objects, we cannot use < – compare elements ¡ However, Java has an interface called Comparable (in – move elements the java.lang package) that defines the method ¡ The time to perform each of these two operations, compareTo(Object object) which can be used to and the number of times we perform each operation, compare objects from classes implementing the is critical to the time it takes to sort a collection. Comparable interface. 1 7 8 Comparing Objects - CompareTo() Designing Classes for Sorting ¡ compareTo(Object object) If we write our sorting algorithms using the – If the receiver is “less” than the argument, it compareTo() method, we can apply them to returns a negative int. collections that hold objects from any classes that implement the Comparable interface. – If the receiver is “equal” to the argument, it returns the zero int. ¡ – If the receiver is “greater” than the argument, it For example, the String and Integer classes implement returns a positive int. Comparable. ¡ Any class designer who wants the objects in a class to be sortable using our algorithms only needs to make the class implement the Comparable interface. 9 10 The Time for Comparing Values The Time for Comparing Objects 1 ¡ ¡ The time to actually compare two primitive values is It takes longer to compare two Java objects than two small (one virtual machine instruction). Java primitive values. ¡ ¡ The comparison time for values is dominated by the To compare objects, the compareTo() method must time it takes to access the two elements being access not only the object, but its internal state as well. compared. ¡ ¡ For example, the next slide shows the Java source For example, the two array accesses take much more code from the library (java.String) to compare two time than the actual comparison in the code: Strings. if (data[i] < data[j]) – The details are not important, just notice that it requires many ¡ Therefore, a comparison of primitive values "costs" accesses to the array that each String uses to store its chars. two data accesses. ¡ The important point is that comparing objects can "cost" many data accesses. 11 12 The Time for Comparing Objects 2 Moving Elements public int compareTo(String anotherString) { ¡ Besides comparing elements, the only other operation int len1 = this.count; that is essential to sorting is moving elements. int len2 = anotherString.count; ¡ int n = Math.min(len1, len2); The exact code for moving elements depends on the char v1[] = this.value; type of collection and the pattern of element char v2[] = anotherString.value; movement, but it consists of a series of data int i = this.offset; accesses. int j = anotherString.offset; ¡ One common form of element movement is an while (n-- != 0) { exchange which is done using a single temporary char c1 = v1[i++]; char c2 = v2[j++]; variable and three assignments. if (c1 != c2) { ¡ This process usually involves four container accesses return c1 - c2; and two local variable accesses. } ¡ } Since the local variable accesses often get mapped to return len1 - len2; registers or cache memory we won't count them. } 2 13 14 Exchange Algorithm (1) Exchange Algorithm (2) ref i ref j ref i ref j ref i ref j ref i ref j temp = c[i]; c[j] = temp; £¥¤§¦¨¡©¦©¤©¤§ ¨ § ¢¡ £¥¤§¦¨¡©¦©¤©¤§ ¨ § data a data b ¢¡ data a data b data a data b data a data b ¤§£ ¨ ¦ ¨ ¨¦¤¤§ ¨ § ¤§£ ¨ ¦¨ ¨ ¨¦©¤¤§ ¨ § temp temp temp temp ref i ref j c[i] = c[j]; ¦¨ ¨¦©¤§¤ ¢¤£ data a data b temp 15 16 Comparison and Movement Times Sorting ¡ To predict the total time for an algorithm, we can We will look at these sorting algorithms add the accesses used for comparison and the accesses used for movement in the algorithm. – Selection sort ¡ If the container holds primitive values each – Insertion sort comparison requires two accesses, but if the – Merge sort container holds objects the number of accesses – Quick sort may be harder to compute. ¡ If the algorithm uses exchanges, each exchange requires four accesses, but if the algorithm uses a different style of data movement, the number of accesses may be harder to compute. Revised 27/07/03 18 About This Lecture In this lecture we will review the sorting Sorting - Selection Sort algorithm called Selection Sort which was presented in CMPUT 114. We will analyze the time and space complexity of a standard implementation Cmput 115 - Lecture 9 for sorting arrays. Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package 3 19 20 Selection Sort Algorithm Outline Input: anArray – array of Comparable objects Selection Sort Algorithm n – sort the elements in positions 0…(n-1) Selection Sort – Implementation for Arrays Output: Time and Space Complexity of Selection a sorted array [0..(n-1)] Sort Idea: We first pick up the largest item and store it at the end, and then pick up the 2nd largest item and put it at the 2nd last, … Algorithm For last =(n-1),(n-2),…,1 – Find the largest element in positions 0…last – exchange it with the element at the last index 21 22 Selection Sort Code - Arrays Method - getMaxIndex() - Arrays public static void selectionSort(Comparable anArray[], public static int getMaxIndex(Comparable anArray[], int n) { int last) { // pre: 0 <= n <= anArray.length // pre: 0 <= last < anArray.length // post: values in anArray positions 0…..(n-1) // post: return the index of the max value in // positions 0…..(last-1) of the given array // are in ascending order int maxIndex; //index of largest object int maxIndex; //index of largest object int index; //index of inspected element int last; //index of last unsorted element maxIndex = last; for (index = last - 1; index >= 0; index--) { for (last = n-1; last > 0; last--) { if (anArray[index].compareTo(anArray[maxIndex]) > 0) maxIndex = getMaxIndex(anArray, last); maxIndex = index; swap(anArray, last, maxIndex); } } // we could check to see if maxIndex != last return maxIndex } // and only swap in this case. } code based on Bailey pg. 82 code based on Bailey pg. 82 23 24 Counting Comparison Accesses Selection Sort Code (recursion) public static void selectionSort(Comparab lanArray[], How many comparison accesses are required for a selection int n) sort of n elements ? { All the comparisons are done in getMaxIndex. // pre: 0 <= n <= anArray.length // post: objects in anArray positions 0…..(n-1) getMaxIndex’s loop does one comparison per iteration, and // are in ascending order iterates “last” times, so each call to getMaxindex does “last” comparisons. int maxIndex; //index of largest object The loop body in the sort method is executed (n-1) times with if ( n > 1 ) { “last” taking on the values n-1, n-2, … 1. getMaxIndex is called maxIndex = getMaxIndex(anArray, n); once with each value of “last”. swap(anArray, n, maxIndex); The total number of comparisons is: selectionSort(anArray,n-1); (n-1)+(n-2) + … + 1= (1+2+ … n)-n = [n(n+1)/2] - n } Since each comparison requires two accesses there are: } n(n+1) - 2n = n2 - n = O(n2) comparison accesses. 4 25 26 Counting Move Accesses Time Complexity of Selection Sort ¡ How many move accesses are required for a The number of comparisons and moves is selection sort of n elements? independent of the data (the initial order of ¡ The only time we do a move is in a reference exchange (swap) which required 4 accesses. elements doesn’t matter). ¡ The sort method executes swap() once on each Therefore, the best, average and worst iteration of its loop. The loop iterates n-1 times. case time complexities of the Selection ¡ The total number of move accesses is: Sort are all O(n2). 4*(n-1) = O(n). ¡ Since the number of comparison accesses is O(n2), the move accesses are insignificant. ¡ In total the code does O(n2) accesses. 27 Revised 27/07/03 Space Complexity of Selection Sort Besides the collection itself, the only extra storage for this sort is the single temp Sorting - Insertion Sort reference used in the swap method. Therefore, the space complexity of Selection Sort is O(n). Cmput 115 - Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package 29 30 About This Lecture Outline In this lecture we will learn about a sorting The Insertion Sort Algorithm algorithm called the Insertion Sort.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages21 Page
-
File Size-