Sorting
Day 1
,
Day 2
,
Day 3
Introduction
We now begin a discussion of one of the most important problems in Computer Science: sorting.
Formally, we state the problem: Given a sequence of n elements a
0
, a
1
, . . ., a
n1
, arrange the
elements such that they are in ascending order.
We will discuss 5 algorithms that sort these elements, and compare and contrast them to see
which algorithm may be best in a given situation.
Bubble Sort
One of the more popular sorting techniques is known as "bubble sort". If we have an array of
unsorted elements, we start at the left end of the array and look at adjacent successive pairs of
elements, swapping the two if appropriate. It is important to note that each element of the array
(except the endpoints) belongs to two sets of pairs: one where it is the first element, and one
where it is the second. After we have done this for all pairs, the result is that the largest element in
the array to the far right position. That is, after the first pass on bubble sort, the largest element is
where it belongs. We then keep making passes similar to the one just described, until the array
ends up sorted. This description is formalized below:
bubbleSort(A)
Input: an unsorted array, A
Postcondition: A is sorted in ascending order
for pass = 1 to length(A)  1
for j = 0 to length(A)  pass
if A[j] > A[j+1]
swap(A, j, j+1)
Suppose our unsorted array is:
4 25
1 29 19 27 13 28
We would begin by seeing if the first two elements require swapping (I draw the two we are
comparing in
red
). Here, they do, resulting in:
4
25 1
29 19 27 13 28
Now, we compare the 29 and 1, and swap:
4 1
25 29
19 27 13 28
We continue in this fashion, until after the first pass we have:
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document4 1 25 19 27 13 28 29
Notice that the 29 is right where we want it.
We then begin our next pass:
4 1
25 19 27 13 28 29
The last set of comparisons we will have to make for this pass is:
1 4 19 25 13
27 28
29
Now, the last two elements are correct (actually, the last 3 are correct, but we won't be sure of that
until the next pass).
We still have a total of five passes to make, but I won't bother with that here. Instead, you can see
the full
bubble sort
. You should probably try it on your own first, though, to see if you get the
same results.
Time analysis
: How long does it take to perform bubble sort? First, we need to define what
operations we are performing: in bubble sort we need only worry about making comparisons and
making swaps. We assume that each of these operations takes O(1) time. Let us begin by
assuming our list is completely sorted to begin with: thus, we never have to make swaps, but only
need to perform comparisons. If we have n elements in our list, we will have to make n1
comparisons on our first pass, n2 comparisons on our second pass, and so on, until we make 1
comparison on our (n1)
th
pass. Thus, we add up these comparisons:
(n1) + (n2) + . . . + 1 = n*(n1)/2
Thus, we see that bubble sort will be O(n
2
) on a sorted list.
The worst case for bubble sort is when we have to make a lot of swaps. We make the maximum
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 MarvinSolomon
 Data Structures, Sort, Bubble Sort, Array

Click to edit the document details