sorting. background as soon as you create a significant database, you’ll probably think of reasons...

Post on 28-Dec-2015

220 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Sorting

Background• As soon as you create a significant database, you’ll

probably think of reasons to sort it in various ways. You need to arrange names in alphabetical order, students by grade, customers by ZIP code, home sales by price, cities in order of increasing population, countries by GNP, stars by magnitude, and so on.

• Sorting data may also be a preliminary step to searching it. As we saw in “Arrays,” a binary search, which can be applied only to sorted data, is much faster than a linear search.

• Imagine that your kids-league baseball team 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?

• 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.

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.

•Bubble Sort on the Baseball Players1. Compare two players.2. If the one on the left is taller, swap

them.3. Move one position right.

Kondisi Awal0 1 2 3 4 5 6 7 8 9

77 99 44 55 22 88 11 00 66 33

0 1 2 3 4 5 6 7 8

77 44 55 22 88 11 00 66 33 99

Perulangan outer: 9

0 1 2 3 4 5 6 7

44 55 22 77 11 00 66 33 88 99

Perulangan outer: 8

0 1 2 3 4 5 6

44 22 55 11 00 66 33 77 88 99

Perulangan outer: 7

0 1 2 3 4 5

22 44 11 00 55 33 66 77 88 99

Perulangan outer: 6

0 1 2 3 4

22 11 00 44 33 55 66 33 88 99

Perulangan outer: 5

0 1 2 3

11 00 22 33 44 55 66 77 88 99

Perulangan outer: 4

0 1 2

00 11 22 33 44 55 66 77 88 99

Perulangan outer: 3

0 1

00 11 22 33 44 55 66 77 88 99

Perulangan outer: 2

Selection Sort• The selection sort improves on the bubble sort by reducing

the number of swaps. Unfortunately, the number of comparisons remains the same.

• 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.

• What’s involved in the selection sort is making a pass through all the players and selecting the shortest one. This shortest player is then swapped with the player on the left end of the line, at position 0. Now the leftmost player is sorted and won’t need to be moved again.

• Notice that in this algorithm the sorted players accumulate on the left (lower indices), whereas in the bubble sort they accumulated on the right.

• The next time you pass down the row of players, you start at position 1, and, finding the minimum, swap with position 1. This process continues until all the players are sorted.

Kondisi Awal0 1 2 3 4 5 6 7 8 9

77 99 44 55 22 88 11 00 66 33

1 2 3 4 5 6 7 8 9

00 99 77 55 44 88 22 11 66 33

Perulangan outer: 0

2 3 4 5 6 7 8 9

00 11 99 77 55 88 44 22 66 33

Perulangan outer: 1

3 4 5 6 7 8 9

00 11 22 99 77 88 55 44 66 33

Perulangan outer: 2

4 5 6 7 8 9

00 11 22 33 99 88 77 55 66 44

Perulangan outer: 3

5 6 7 8 9

00 11 22 33 44 99 88 77 66 55

Perulangan outer: 4

6 7 8 9

00 11 22 33 44 55 99 88 77 66

Perulangan outer: 5

7 8 9

00 11 22 33 44 55 66 99 88 77

Perulangan outer: 6

8 9

00 11 22 33 44 55 66 77 99 88

Perulangan outer: 7

9

00 11 22 33 44 55 66 77 88 99

Perulangan outer: 8

Insertion Sort• In most cases the insertion sort is the best of the elementary

sorts described in this chapter. It’s about twice as fast as the bubble sort and somewhat faster than the selection sort in normal situations.

• Insertion Sort on the Baseball Players• To begin the insertion sort, start with your baseball players

lined up in random order.• It’s easier to think about the insertion sort if we begin in the

middle of the process, when the team is half sorted.• What we’re going to do is insert the marked player in the

appropriate place in the (partially) sorted group. However, to do this, we’ll need to shift some of the sorted players to the right to make room. To provide a space for this shift, we take the marked player out of line. (In the program this data item is stored in a temporary variable.)

Kondisi Awal0 1 2 3 4 5 6 7 8 9

77 99 44 55 22 88 11 00 66 33

0 1

77 99 44 55 22 88 11 00 66 33

Perulangan outer: 1

0 1 2

44 77 99 55 22 88 11 00 66 33

Perulangan outer: 2

0 1 2 3

44 55 77 99 22 88 11 00 66 33

Perulangan outer: 3

Temp

99

Temp

44

Temp

55

0 1 2 3 4

22 44 55 77 99 88 11 00 66 33

Perulangan outer: 4Temp

22

0 1 2 3 4 5

22 44 55 77 88 99 11 00 66 33

Perulangan outer: 5Temp

88

0 1 2 3 4 5 6

11 22 44 55 77 88 99 00 66 33

Perulangan outer: 6Temp

11

0 1 2 3 4 5 6 7

00 11 22 44 55 77 88 99 66 33

Perulangan outer: 7Temp

00

0 1 2 3 4 5 6 7 8

00 11 22 44 55 66 77 88 99 33

Perulangan outer: 8Temp

66

0 1 2 3 4 5 6 7 8 9

00 11 22 33 44 55 66 77 88 99

Perulangan outer: 9Temp

33

Sorting Objects

Comparing the Simple Sorts• The bubble sort is so simple that you can write it

from memory. Even so, it’s practical only if the amount of data is small.

• The selection sort minimizes the number of swaps, but the number of comparisons is still high. This sort might be useful when the amount of data is small and swapping data items is very time-consuming compared with comparing them.

• The insertion sort is the most versatile of the three and is the best bet in most situations, assuming the amount of data is small or the data is almost sorted.

top related