Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Sorting

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

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

4 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 Players 1. Compare two players. 2. If the one on the left is taller, swap them. 3. Move one position right.

5

6

7 Kondisi Awal 0123456789 77994455228811006633 012345678 77445522881100663399 Perulangan outer: 9 01234567 44552277110066338899 Perulangan outer: 8 0123456 44225511006633778899 Perulangan outer: 7

8 012345 22441100553366778899 Perulangan outer: 6 01234 22110044335566338899 Perulangan outer: 5 0123 11002233445566778899 Perulangan outer: 4 012 00112233445566778899 Perulangan outer: 3 01 00112233445566778899 Perulangan outer: 2

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

10

11

12 Kondisi Awal 0123456789 77994455228811006633 123456789 00997755448822116633 Perulangan outer: 0 23456789 00119977558844226633 Perulangan outer: 1 3456789 00112299778855446633 Perulangan outer: 2

13 456789 00112233998877556644 Perulangan outer: 3 56789 00112233449988776655 Perulangan outer: 4 6789 00112233445599887766 Perulangan outer: 5 789 00112233445566998877 Perulangan outer: 6 89 00112233445566779988 Perulangan outer: 7 9 00112233445566778899 Perulangan outer: 8

14 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.)

15

16

17

18 Kondisi Awal 0123456789 77994455228811006633 01 77994455228811006633 Perulangan outer: 1 012 44779955228811006633 Perulangan outer: 2 0123 44557799228811006633 Perulangan outer: 3 Temp 99 Temp 44 Temp 55 01234 22445577998811006633 Perulangan outer: 4 Temp 22

19 012345 445577889911006633 Perulangan outer: 5 Temp 88 0123456 11224455778899006633 Perulangan outer: 6 Temp 11 01234567 00112244557788996633 Perulangan outer: 7 Temp 00 012345678 112244556677889933 Perulangan outer: 8 Temp 66 0123456789 00112233445566778899 Perulangan outer: 9 Temp 33

20 Sorting Objects

21

22

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


Download ppt "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."

Similar presentations


Ads by Google