1. Lesson 3 â Simple
Sorting
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
2. How Would You Do It?
Imagine that your kids-league baseball team (mentioned in Chapter 1, âOverviewâ) is lined up
on the field, as shown in Figure 3.1. The regulation nine players, plus an extra, have shown up
for practice. You want to arrange the players in order of increasing height (with the shortest
player on the left) for the team picture. How would you go about this sorting process?
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 2
3. How Would You Do It?
As a human being, you have advantages over a computer program. You can see all the kids at
once, and you can pick out the tallest kid almost instantly. You donât need to laboriously
measure and compare everyone. Also, the kids donât need to occupy particular places. They can
jostle each other, push each other a little to make room, and stand behind or in front of each
other. After some ad hoc rearranging, you would have no trouble in lining up all the kids, as
shown in Figure 3.2.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 3
4. The three algorithms:
ďBubble Sort
The bubble sort is notoriously slow, but itâs conceptually the simplest of the sorting algorithms and for
that reason is a good beginning for our exploration of sorting techniques.
ďSelection Sort
improves on the bubble sort by reducing the number of swaps necessary from O(N2)
to O(N).
ďInsertion Sort
It still executes in O(N2 ) time, but itâs about twice as fast as the bubble sort and somewhat
faster than the selection sort in normal situations.
This three algorithms involve two steps, executed over and over until the data is sorted:
1. Compare two items.
2. Swap two items, or copy one item.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 4
5. Bubble Sort on the Baseball Players
The bubble sort routine works like this: You
start at the left end of the line and compare the
two kids in positions 0 and 1. If the one on the
left (in 0) is taller, you swap them. If the one on
the right is taller, you donât do anything. Then
you move over one position and compare the
kids in positions 1 and 2. Again, if the one on
the left is taller, you swap them. This sorting
process is shown in Figure 3.3.
Here are the rules youâre following:
1. Compare two players.
2. If the one on the left is taller, swap them.
3. Move one position right.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 5
6. Bubble Sort on the Baseball Players
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 6
You continue down the line this way until you reach the right end. You have by no means
finished sorting the kids, but you do know that the tallest kid is on the right. This must be true
because, as soon as you encounter the tallest kid, youâll end up swapping him (or her) every
time you compare two kids, until eventually he (or she) will reach the right end of the line. This
is why itâs called the bubble sort: As the algorithm progresses, the biggest items âbubble upâ to
the top end of the array. Figure 3.4 shows the baseball players at the end of the first pass.
7. Bubble Sort on the Baseball Players
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 7
After this first pass through all the data, youâve made N-1 comparisons and somewhere between
0 and N-1 swaps, depending on the initial arrangement of the players. The item at the end of the
array is sorted and wonât be moved again. Now you go back and start another pass from the left
end of the line. Again, you go toward the right, comparing and swapping when appropriate.
However, this time you can stop one player short of the end of the line, at position N-2, because
you know the last position, at N-1, already contains the tallest player. This rule could be stated
as:
4. When you reach the first sorted player, start over at the left end of the line.
8. Java Code for a Bubble Sort
In the bubbleSort.java program, shown in Listing 3.1, a class called ArrayBub encapsulates an
array a[], which holds variables of type long.
In a more serious program, the data would probably consist of objects, but we use a primitive
type for simplicity. (Weâll see how objects are sorted in the objectSort.java program in Listing
3.4.) Also, to reduce the size of the listing, we donât show find() and delete() methods with the
ArrayBub class, although they would normally be part of a such a class.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
9. Java Code for a Bubble Sort
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 9
10. Java Code for a Bubble Sort
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
11. Java Code for a Bubble Sort
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 11
The constructor and the insert() and display() methods of this class are similar to those weâve seen before.
However, thereâs a new method: bubbleSort(). When this method is invoked from main(), the contents of
the array are rearranged into sorted order. The main() routine inserts 10 items into the array in random
order, displays the array, calls bubbleSort() to sort it, and then displays it again.
Hereâs the output:
77 99 44 55 22 88 11 0 66 33
0 11 22 33 44 55 66 77 88 99
12. The bubbleSort() method is only four lines
long. Here it is, extracted from the listing:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 12
13. Selection Sort
The selection sort improves on the bubble sort by reducing the number of swaps necessary from
O(N2 ) to O(N). Unfortunately, the number of comparisons remains O(N2 ). However, the
selection sort can still offer a significant improvement for large records that must be physically
moved around in memory, causing the swap time to be much more important than the
comparison time. (Typically, this isnât the case in Java, where references are moved around, not
entire objects.)
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
14. Selection Sort on the Baseball Players
Letâs consider the baseball players again. In the selection sort, you can no longer compare only
players standing next to each other. Thus, youâll need to remember a certain playerâs height; you
can use a notebook to write it down. A magenta-colored towel will also come in handy.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
15. Java Code for Selection Sort
The listing for the selectSort.java program is similar to that for bubbleSort.java, except that the
container class is called ArraySel instead of ArrayBub, and the bubbleSort() method has been
replaced by selectSort(). Hereâs how this method looks:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
16. Java Code for Selection Sort
The outer loop, with loop variable out, starts at the beginning of the array (index 0) and
proceeds toward higher indices. The inner loop, with loop variable in, begins at out and likewise
proceeds to the right. At each new position of in, the elements a[in] and a[min] are compared. If
a[in] is smaller, then min is given the value of in. At the end of the inner loop, min points to the
minimum value, and the array elements pointed to by out and min are swapped.
Listing 3.2 shows the complete selectSort.java program.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
17. Java Code for Selection Sort
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 17
18. Java Code for Selection Sort
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 18
The output from
selectSort.java is identical to
that from bubbleSort.java:
77 99 44 55 22 88 11 0 66 33
0 11 22 33 44 55 66 77 88 99
19. Insertion Sort
It still executes in O(N2 ) time, but itâs about twice as fast as the bubble sort and somewhat
faster than the selection sort in normal situations. Itâs also not too complex, although itâs slightly
more involved than the bubble and selection sorts. Itâs often used as the final stage of more
sophisticated sorts, such as quicksort.
Insertion Sort on the Baseball Players
To begin the insertion sort, start with your baseball players lined up in random order. (They
wanted to play a game, but clearly thereâs no time for that.) Itâs easier to think about the
insertion sort if we begin in the middle of the process, when the team is half sorted.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 19
20. Insertion Sort
Partial Sorting
At this point thereâs an imaginary marker somewhere in the middle of the line. (Maybe you
threw a red T-shirt on the ground in front of a player.) The players to the left of this marker are
partially sorted. This means that they are sorted among themselves; each one is taller than the
person to his or her left. However, the players arenât necessarily in their final positions because
they may still need to be moved when previously unsorted players are inserted between them.
Note that partial sorting did not take place in the bubble sort and selection sort. In these
algorithms a group of data items was completely sorted at any given time; in the insertion sort a
group of items is only partially sorted.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
21. The Marked Player
The player where the marker is, whom weâll call the âmarkedâ player, and all the players on her
right, are as yet unsorted. This is shown in Figure 3.11.a.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
22. Java Code for Insertion Sort
Hereâs the method that carries out the insertion sort, extracted from the insertSort.java
program:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
23. Java Code for Insertion Sort
In the outer for loop, out starts at 1 and moves right. It
marks the leftmost unsorted data. In the inner while
loop, in starts at out and moves left, until either temp is
smaller than the array element there, or it canât go left
any further. Each pass through the while loop shifts
another sorted element one space right. It may be hard
to see the relation between the steps in the InsertSort
Workshop applet and the code, so Figure 3.14 is an
activity diagram of the insertionSort() method, with the
corresponding messages from the InsertSort Workshop
applet.
Listing 3.3 shows the complete insertSort.java program.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 23
24. Java Code for Insertion Sort
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 24
25. Java Code for Insertion Sort
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 25
26. Output:
Hereâs the output from the insertSort.java program; itâs the same as that from the other
programs in this chapter:
77 99 44 55 22 88 11 0 66 33
0 11 22 33 44 55 66 77 88 99
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 26
27. Sorting Objects
For simplicity weâve applied the sorting algorithms weâve looked at
thus far to a primitive data type: long. However, sorting routines will
more likely be applied to objects than primitive types. Accordingly,
we show a Java program in Listing 3.4, objectSort.java, that sorts an
array of Person objects (last seen in the classDataArray.java program
in Chapter 2).
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 27
28. Java Code for Sorting Objects
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 28
29. Java Code for Sorting Objects
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 29
30. Output:
Hereâs the output of this program:
Before sorting:
Last name: Evans, First name: Patty, Age: 24
Last name: Smith, First name: Doc, Age: 59
Last name: Smith, First name: Lorraine, Age: 37
Last name: Smith, First name: Paul, Age: 37
Last name: Yee, First name: Tom, Age: 43
Last name: Hashimoto, First name: Sato, Age: 21
Last name: Stimson, First name: Henry, Age: 29
Last name: Velasquez, First name: Jose, Age: 72
Last name: Vang, First name: Minh, Age: 22
Last name: Creswell, First name: Lucinda, Age: 18
After sorting:
Last name: Creswell, First name: Lucinda, Age: 18
Last name: Evans, First name: Patty, Age: 24
Last name: Hashimoto, First name: Sato, Age: 21
Last name: Smith, First name: Doc, Age: 59
Last name: Smith, First name: Lorraine, Age: 37
Last name: Smith, First name: Paul, Age: 37
Last name: Stimson, First name: Henry, Age: 29
Last name: Vang, First name: Minh, Age: 22
Last name: Velasquez, First name: Jose, Age: 72
Last name: Yee, First name: Tom, Age: 43
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 30
31. Lexicographical Comparisons
The insertSort() method in objectSort.java is similar to that in insertSort.java, but it has been
adapted to compare the lastName key values of records rather than the value of a primitive
type. We use the compareTo() method of the String class to perform the comparisons in the
insertSort() method. Hereâs the expression that uses it:
a[in-1].getLast().compareTo(temp.getLast()) > 0
The compareTo() method returns different integer values depending on the lexicographical (that
is, alphabetical) ordering of the String for which itâs invoked and the String passed to it as an
argument, as shown in Table 3.1.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 31
32. Lexicographical Comparisons
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 32
For example, if s1 is âcatâ and s2 is âdogâ, the function will return a number less than 0. In the
objectSort.java program, this method is used to compare the last name of a[in-1] with the last
name of temp.
33. Summary
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 33
⢠The sorting algorithms in this chapter all assume an array as a data storage structure.
⢠Sorting involves comparing the keys of data items in the array and moving the items (actually, references
to the items) around until theyâre in sorted order.
⢠All the algorithms in this chapter execute in O(N2 ) time. Nevertheless, some can be substantially faster
than others.
⢠An invariant is a condition that remains unchanged while an algorithm runs.
⢠The bubble sort is the least efficient, but the simplest, sort.
⢠The insertion sort is the most commonly used of the O(N2 ) sorts described in this chapter.
⢠A sort is stable if the order of elements with the same key is retained.
⢠None of the sorts in this chapter require more than a single temporary variable, in addition to the
original array
34. PROGRAMMING PROJECT
3.1 Add a method called median() to the ArrayIns class in the insertSort.java program (Listing
3.3).
This method should return the median value in the array.
(Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the
easy way.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 34