Bubble Sort: an Archaeological Algorithmic Analysis
Total Page:16
File Type:pdf, Size:1020Kb
Bubble Sort: An Archaeological Algorithmic Analysis Owen Astrachan 1 Computer Science Department Duke University [email protected] Abstract 1 Introduction Text books, including books for general audiences, in- What do students remember from their first program- variably mention bubble sort in discussions of elemen- ming courses after one, five, and ten years? Most stu- tary sorting algorithms. We trace the history of bub- dents will take only a few memories of what they have ble sort, its popularity, and its endurance in the face studied. As teachers of these students we should ensure of pedagogical assertions that code and algorithmic ex- that what they remember will serve them well. More amples used in early courses should be of high quality specifically, if students take only a few memories about and adhere to established best practices. This paper is sorting from a first course what do we want these mem- more an historical analysis than a philosophical trea- ories to be? Should the phrase Bubble Sort be the first tise for the exclusion of bubble sort from books and that springs to mind at the end of a course or several courses. However, sentiments for exclusion are sup- years later? There are compelling reasons for excluding 1 ported by Knuth [17], “In short, the bubble sort seems discussion of bubble sort , but many texts continue to to have nothing to recommend it, except a catchy name include discussion of the algorithm after years of warn- and the fact that it leads to some interesting theoreti- ings from scientists and educators. For example, in a cal problems.” Although bubble sort may not be a best popular new breadth-first text [6] bubble sort is given practice sort, perhaps the weight of history is more than equal footing with selection sort and quicksort in online enough to compensate and provide for its longevity. student exercises. Starting with Knuth’s premise that “bubble sort seems Categories and Subject Descriptors K.3.2 to have nothing to recommend it”[17], we trace the ori- [Computers & Education]: Computer & Infor- gins and continued popularity of the algorithm from its mation Science Education — Computer Science earliest days as an unnamed sort to its current status Education as perhaps the most popular O(n2) sort (see below) de- General Terms Algorithms, Measurement, Theory spite wide-spread ridicule. We began this study with the intent to document (and ridicule) the continued popu- Keywords Analysis, Performance, Bubble sort larity of bubble sort, but the algorithmic archaeological investigation has proven more interesting than casting aspersions. Our initial premise that bubble sort should not be stud- 1 This work was supported by NSF grants CAREER ied is reflected in [23] with a warning for potential mis- 9702550 and CRCD 0088078. use. Permission to make digital or hand copies of all or part of this work for personal or classroom use is granted without fee For N<50, roughly, the method of straight inser- provided that copies are not made or distributed for profit tion is concise and fast enough. We include it with or commercial advantage and that copies bear this notice N 2 and the full citation on the first page. To copy otherwise, some trepidation: it is an algorithm, whose po- or republish, to post on servers or to redistribute to lists, tential for misuse (by using it for too large an N)is require prior specific permission and/or a fee. great. The resultant waste of computer time is so SIGCSE ’03, February 19-23, Reno, Nevada, USA. awesome, that we were tempted not to include any Copyright 2003 ACM 1-58113-648-X/03/0002....$5.00 1A discussion of bubble sort with warnings that the per- formance is bad and the code isn’t simple (arguably) is like telling someone “don’t think about pink elephants.” 2 N routine at all. We will draw the line, however, 2.1 Bubble Sort, The Code at the inefficient N 2 algorithm bubble sort.Ifyou Taking the description of bubble sort in [17] as definitive know what bubble sort is, wipe it from your mind; 2 if you don’t know, make a point of never finding the code below is bubble sort. This version “bubbles” out! the largest elements to the end of the vector. void BubbleSort(Vector a, int n) This sentiment is similar to the reference to bubble sort { found in [1], where it says of bogo sort, “The archetypical for(int j=n-1; j > 0; j--) perversely awful algorithm (as opposed to bubble sort, for(int k=0; k < j; k++) which is merely the generic bad algorithm).” if (a[k+1] < a[k]) Swap(a,k,k+1); In Section 2 we trace the origin of the algorithm, both } in name and in code. In Section 3 we analyze the perfor- mance of the algorithm and the simplicity of the code. Nearly every description of bubble sort describes how In Section 4 we summarize our study. to terminate the sort early if the vector becomes sorted. This optimization requires checking if any swaps are made and terminating if no swaps are made after j it- 2 Origins of Bubble Sort erations of the inner loop. Another optimization is to alternate the direction of In an effort to determine why bubble sort is popular we bubbling each time the inner loop iterates. This is traced its origins. Knuth [17] does not provide informa- shaker sort or cocktail shaker sort (see, e.g., [17, 13]). tion on the origin of the name, though he does provide a 1956 reference [10] to an analysis of the algorithm. 2.2 A Sort by Any Other Name . That paper refers to “sorting by exchange”, but not to bubble sort. An extensive bibliography and sequence of Nomenclature is interesting. An early (1963) Fortran articles from the 1962 ACM Conference on Sorting [11] textbook [22] refers to the following code as “jump- do not use the term bubble sort, although the “sorting down” sort. by exchange” algorithm is mentioned. With no obvious definitive origin of the name “bubble sort”, we investi- void JumpDownSort(Vector a, int n) gated its origins by consulting early journal articles as { well as professional and pedagogical texts. for(int j=n-1; j > 0; j--) for(int k=0; k < j; k++) An early (1959) book on programming [21] devotes a if (a[j] < a[k]) chapter to sorting, but uses the term exchange sort- Swap(a,k,j); ing rather than bubble sort. The same term is used } in a 1962 [4] JACM article as well as in the earlier (1961, submitted 1959) [9] JACM article referenced as Bubble sort as we’ve identified it is called a “push- the definitive source. Iverson uses the name “bubble down” sort. In another early (1962) work [19] the sort” in 1962 [13]; this appears to be the first use of the “jump-down” version is presented first in the book, with term in print. As we note below, each work cited in [13] no name. The bubble sort follows also with no name. uses a phrase other than “bubble sort” to describe the In two early works [3, 10] the jump-down sort is referred algorithm we describe in Section 2.1. This reinforces the to as selection sort. Bubble sort is also covered, but re- claim that Iverson is the first to use the term, though ferred to as sorting by repeated comparison and exchang- obviously not conclusively. However, we could find no ing, respectively. In the latter paper, one of the earliest work published after 1962 with a reference to bubble works comparing algorithms, the exchange/bubble sort sort in an earlier publication than Iverson’s. is described thus: “Exchanging requires at least twice as many non-housekeeping comparisons as Inserting and Despite these earlier publications the algorithm offi- for most computers will be inferior”. cially enters the ACM algorithm repository as Algo- rithm 175 [25] in 1963 where it is named Shuttle Sort. In moving from bubble sort to jump-down sort the only Soon thereafter [14] the published algorithm is found to change to the code above is that one array index has k j be “not free from errors”, a gentle way of saying the been changed from +1 to . How does this differ from m published code is wrong. There a modified version of bubble sort? After iterations of the outer loop the m the code (that stops early when no swaps are made) is last elements are in their final position—the same given and the author says that in this form the code 2This code is close to legal in both C++ and Java and was “studied in this form on the ORDVAC computer, should be readable by anyone with a working knowledge of Aberdeen Proving Ground, in 1955.” Algol-like languages. invariant as bubble sort. However, items are not “bub- of selection sort (e.g., [3, 10]) in which only the ver- bled” to the top; this code implements what is essen- sion described in Section 2.1 as “jump-down” sort is tially a selection sort, but the current maximal element described, no separate minimum index variable is kept, is stored/swapped into location a[j]oneachpass.We and the minimal element selected in each pass of selec- see why early works sometimes refer to jump-down as tion sort is replaced by “a series of 9’s ...sothatsaid selection sort. item will never again be selected.” [10]. Another early work [7] is referenced in [18] with the 2.3 Origins of Popularity following warning.