Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
dynamic visualisation: Lafore's Bubble Sort YouTube Visualization static visualisation: hatful of hollow ALG0183 Algorithms & Data Structures Lecture 13 Bubble Sort 8/25/2009 comparison sort worse-case, average case O(N2) best-case O(N) (recognise no swaps) in-place algorithm (input overwritten by output) stable sort ALG0183 Algorithms & Data Structures by Dr Andy Brooks 1 case study article “Bubble Sort: An Archaeological Algorithmic Analysis” Owen Astrachan Proceedings SIGCSE´03, 2003, pp 1-5. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 2 Should you learn about Bubble Sort? “In short, the bubble sort seems nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems.” D. E. Knuth “For N< 50, roughly, the method of straight insertion is concise and fast enough. We include it with some trepidation: it is an N2 algorithm, whose potential for misuse (by using it for too large an N) is great. The resultant waste of computer time is so awesome, that we were tempted not to include any N2 routine at all. We will draw the line, however, at the inefficient N2 algorithm bubble sort.Ifyou know what bubble sort is, wipe it from your mind; if you don’t know, make a point of never finding out!” W. H. Press et. al. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 3 Origins of Bubble Sort • An 1956 article refers to “sorting by exchange”. – Friend, E. Sorting on electronic computer systems. J. ACM 3 (1956), 134–168. • A 1959 book refers to “exchange sort”. – McCracken, D., Weiss, H., and Lee, T. Programming Business Computers. John Wiley, 1959. • The 1962 ACM Conference on Sorting refers to “sorting by exchange”. – Gotlieb, C. Sorting on computers. Communications of the ACM 6, 5 (May 1963), 194–201. • In 1962, Iverson uses the term “bubble sort”. – Iverson, K. A Programming Language. John Wiley, 1962 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 4 Origins of Bubble Sort • In 1963, Bubble Sort enters the ACM algorithm repository as Algorithm 175, named Shuttle Sort. – Shaw, C., and Trimble, T. Algorithm 175: Shuttle sort. Communications of the ACM 6, 6 (June 1963), 312– 313. • The published algorithm does not stop early when no swaps are made. – There can be unnecessary comparisons. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 5 ©ACM 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 6 ©ACM 25 50 100 250 500 8/25/2009 x4 x16 x100 x400 1,60 6,40 25,60 160,00 640,00 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 7 Origins of the popularity of Bubble Sort • In a major 1971 survey article it is stated that bubble sort is easy to remember and that little time is required to complete a single step! – Martin, W. A. Sorting. ACM Computing Surveys 3, 4 (1971), 147– 174 • A 1974 textbook on algorithms states that if only a handful of items are to be sorted then a simple strategy such as bubble sort is appropriate! – Aho, A. V., Hopcroft, J. E., and Ullman, J. D. The Design and Analysis of Computer Algorithms. Addison-Wesley, 1974. • Owen Astrachan writes: “Perhaps early concerns with allocating registers and using memory led to the adoption of bubble sort which in its primitive form (no early stopping) requires no storage other than the array and two index variables.” 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 8 Figure 1: Sorting Strings in Java ©ACM data by Owen Astrachan • Bubble Sort´s performance on random data is worse than insertion sort and selection sort. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 9 Table 2: Halstead Complexity of Sorts ©ACM • The Halstead metrics are based on counts of operators and operands. – D is Halstead difficulty. – E is Halstead effort. • Notice that recursive quicksort is “better” than Bubble Sort. • Remember, however, that software metrics are controversial: not everyone agrees that software metrics really measure software. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 10 Pseudocode implementation Wikipedia download 4.10.2009 • The for loop is repeatedly executed (a loop within a loop) until a pass is made when no swaps are made. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 11 Step-by-step example (5 1 4 2 8) Wikipedia download 4.10.2009 elements written in bold are being compared 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 12 Alternative pseudocode implementation Wikipedia download 4.10.2009 • After the first pass, the biggest element is in its right position and there is no need to iterate over the whole array. • Each pass can be one step shorter than the previous. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 13 Lewis code © Addison Wesley public static void bubbleSort (Comparable[] data) { int position, scan; for (position = data.length - 1; position >= 0; position--) { for (scan = 0; scan <= position - 1; scan++) if (data[scan].compareTo(data[scan+1]) > 0) swap (data, scan, scan+1); } } • The code is not as efficient as it could be as it does recognise a pass with no swaps and so it will not stop early when it is possible to do so. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 14 Gaddis code © Addison Wesley bubble sort on an integer array public static void bubbleSort(int[] array) { int maxElement; // Marks the last element to compare int index; // Index of an element to compare int temp; // Used to swap to elements for (maxElement = array.length - 1; maxElement >= 0; maxElement--) { for (index = 0; index <= maxElement - 1; index++) { // Compare an element with its neighbor. if (array[index] > array[index + 1]) { // Swap the two elements. temp = array[index]; array[index] = array[index + 1]; array[index + 1] = temp; } } } } 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 15 Gaddis code © Addison Wesley bubble sort on an array of objects public static void bubbleSort(Comparable[] array) { int maxElement; // Marks the last element to compare int index; // Index of an element to compare Comparable temp; // Used to swap to elements for (maxElement = array.length - 1; maxElement >= 0; maxElement--) { for (index = 0; index <= maxElement - 1; index++) { // Compare an element with its neighbor. if (array[index].compareTo(array[index + 1]) > 0) { // Swap the two elements. temp = array[index]; array[index] = array[index + 1]; array[index + 1] = temp; } } } } 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 16 Contact.java implements the Comparable interface: has a compareTo method public class Contact implements Comparable { private String firstName, lastName, phone; ... // Uses both last and first names to determine lexical ordering. public int compareTo (Object other) { int result; if (lastName.equals(((Contact)other).lastName)) result = firstName.compareTo(((Contact)other).firstName); else result = lastName.compareTo(((Contact)other).lastName); return result; } 8/25/2009 Note: The String class has its own CompareTo method. ALG0183 Algorithms & Data Structures by Dr Andy Brooks 17 The Comparable interface and compareTo. http://java.sun.com/javase/6/docs/api/ public interface Comparable<T> This interface imposes a total ordering on the objects of each class that implements it. Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator. The natural ordering for a class C is said to be consistent with equals if and only if e1.compareTo(e2) == 0 has the same boolean value as e1.equals(e2) for every e1 and e2 of class C. Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false. This interface is a member of the Java Collections Framework. int compareTo(T o) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 18 Some Big-Oh for the worse-case worse-case: numbers in reverse order • In the worse-case, and without recognising that elements which have been placed in their correct position no longer need to be compared, the number of comparisons is: n*(n-1). • In the worse-case, but recognising that each pass can be one shorter than the previous one, the number of comparisons is: (n-1)+(n-2)+…+1 = n(n-1)/2. • Big-Oh is O(N2) in both cases but the slightly more intelligent algorithm is twice as fast in the worse-case (in terms of the number of comparisons). n(n 1) x 2 x 1 n 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 19 Static visualisation of Bubble Sort http://www.hatfulofhollow.com//posts/code/visualisingsorting/ • “Rabbits” and “turtles” are clearly visible. • “The magnitude of a number is indicated by shading - higher numbers are darker, and lower numbers are lighter.” • “We begin on the left hand side with the numbers in a random order, and the sorting progression plays out until we reach the right hand side with a sorted sequence. Time, in this particular case, is measured by the number of swaps performed.” • This technique works only for in-place algorithms. 8/25/2009 ALG0183 Algorithms & Data Structures by Dr Andy Brooks 20