bubble sort. sorting computers only use numeric values for sorting does this mean you cannot sort...

23
Bubble Sort

Upload: lesley-holmes

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Bubble Sort Logic  Let’s look at the logic to sort 5 test scores in a file  The mainline logic will consist of 3 subroutines  Housekeeping  Sort-Scores  Finish-Up

TRANSCRIPT

Page 1: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Bubble Sort

Page 2: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Sorting

Computers only use numeric values for sorting Does this mean you cannot sort a file by a character

field (such as last name or state)? No, it doesn’t. The computer converts all characters

into a series of 0’s and 1’s. It’s the 0’s and 1’s that the computer uses to compare fields for sorting.

You can sort a file by ascending order (lowest to highest) descending order (highest to lowest)

Page 3: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Bubble Sort Logic

Let’s look at the logic to sort 5 test scores in a file

The mainline logic will consist of 3 subroutines Housekeeping Sort-Scores Finish-Up

Page 4: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Starting the Sort-Scores Routine

When Housekeeping is done, the five scores from the In-Score file are in the Score array.

Let’s assume the scores are:

Score(1) = 90Score(2) = 85Score(3) = 65Score(4) = 95Score(5) = 75

• The next slide shows the start of the routine

Page 5: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Swapping Scores

To start the sort, the first two values in the array are compared.

Score(1) = 90Score(2) = 85Score(3) = 65Score(4) = 95Score(5) = 75

• If they are out of ascending order, switch (or swap) their positions in the array

Score(1) = 85Score(2) = 90Score(3) = 65Score(4) = 95Score(5) = 75

• The order is a little better than before

Page 6: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Swapping Dilemma

Here is our original score array If we move the value 90 from Score(1) to

Score(2), both array elements would contain the

value 90 the value 85 would be lost

If you move 85 to Score(1), then the value 90 would be lost

How can we solve this problem?

Score(1) = 90Score(2) = 85Score(3) = 65Score(4) = 95Score(5) = 75

Page 7: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Swapping Solution

The solution: create a temporary variable to hold one of the scores

The solution work like this: Move 85 - in Score(2) - to Temp

– Move 90 - in Score(1) - to Score(2)– Move 85 - in Temp - the Score(1)

• Now Score(1) equals 85 and Score(2) equals 90

Page 8: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

More on Swapping The previous routine is written

only to switch elements 1 and 2 Here is a more universal routine

that can be used with any two adjacent elements to be swapped Variable X represents the position

of the first element Value X+1 represents the position

of the second element

Page 9: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Sorting Logic The decision -

Score(X) > Score(X+1) has to be executed four times.

Why not five times since there are five elements? 5 is the last element, so

there is no element after it to compare

Page 10: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Following the Swapping Logic - Step 1

The original array

Score(1) = 85Score(2) = 90Score(3) = 65Score(4) = 95Score(5) = 75

Score(1) = 90Score(2) = 85Score(3) = 65Score(4) = 95Score(5) = 75• The logic:

– Set X to 1– X < 5, so the loop is entered– Compare Score(X) = 90 to

Score(X+1) = 85. They are out of order so swap them

• The result is:

Page 11: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Following the Swapping Logic - Step 2

Step 2 logic: Add 1 to X; X now equals 2 X < 5, so the loop is entered Compare Score(X) = 90 to Score(X+1)

= 65. They are out of order so swap them

The result is:

Score(1) = 85Score(2) = 90Score(3) = 65Score(4) = 95Score(5) = 75

Score(1) = 85Score(2) = 65Score(3) = 90Score(4) = 95Score(5) = 75

Page 12: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Following the Swapping Logic - Step 3

Step 3 logic: Add 1 to X; X now equals 3 X < 5, so the loop is entered Compare Score(X) = 90 to Score(X+1)

= 95. They are in order. No swap is made

The result is:

Score(1) = 85Score(2) = 65Score(3) = 90Score(4) = 95Score(5) = 75

Score(1) = 85Score(2) = 65Score(3) = 90Score(4) = 95Score(5) = 75

Page 13: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Following the Swapping Logic - Step 4

Step 4 logic: Add 1 to X; X now equals 4 X < 5, so the loop is entered Compare Score(X) = 95 to Score(X+1)

= 75. They are out of order so swap them

The result is:

Score(1) = 85Score(2) = 65Score(3) = 90Score(4) = 95Score(5) = 75

Score(1) = 85Score(2) = 65Score(3) = 90Score(4) = 75Score(5) = 95

Page 14: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Following the Swapping Logic - Step 5

Step 5 logic: Add 1 to X; X now equals 5 X = 5, so the loop is not entered and

the swapping logic is completed Every element has been compared to

the one adjacent to it

Score(1) = 85Score(2) = 65Score(3) = 90Score(4) = 75Score(5) = 95

• The largest amount (95) has sunk to the bottom of the list

Page 15: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Continuing the Swapping Logic

Although the list is in a better ascending order, it is still not in order

The five steps need to be performed again so the following values will swap places 85 and 65 90 and 75

Score(1) = 85Score(2) = 65Score(3) = 90Score(4) = 75Score(5) = 95

Score(1) = 65Score(2) = 85Score(3) = 75Score(4) = 90Score(5) = 95

Page 16: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Continuing the Swapping Logic

The five steps will need to be performed one more time to swap 85 and 75

The final result

Score(1) = 65Score(2) = 85Score(3) = 75Score(4) = 90Score(5) = 95

Score(1) = 65Score(2) = 75Score(3) = 85Score(4) = 90Score(5) = 95

Page 17: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

If the list were in the worst possible order....

The five steps would have to be performed four time to get the list in correct ascending order

Score(1) = 95Score(2) = 90Score(3) = 85Score(4) = 75Score(5) = 65

Score(1) = 65Score(2) = 75Score(3) = 85Score(4) = 90Score(5) = 95

To go from this list to

this listHave to pass

throughthe loop 4 times

Page 18: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Complete Sort Logic

X is used to determine that all elements in the list have been compared for swapping

• Y is used to determine if there have been enough passes through the list to sort the list correctly

Page 19: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

More on Bubble Sorts

Using a variable for the array size Previously, a constant (5) was

used to determine the end of loop processing

Using a variable means the code doesn’t change if the size of the array changes

Page 20: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Rules for Sorting

The greatest number of pair comparisons needed to be made during each loop is one fewer than the number of elements is the array

The pair comparison loop needs to be processed one time fewer than the number of elements in the array

Page 21: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Reducing Unnecessary Comparisons

In a bubble sort, the first pass through the list guarantees the last item on the list will be at the bottom

For second pass, the second to the last item will be in the correct position

Each pass will correctly place an item towards the bottom of the list

When performing each pass, why continue to compare items known to be correct?

Page 22: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Reducing Comparisons

Logic Pairs-To-Compare is set to X-1 the

first time through the list

• Comparisons are done Pairs-To-Compare times

• 1 is subtracted from Pairs-To-Compare, so next comparison processing will pass one less time

Page 23: Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or

Eliminating Unnecessary Passes Logic

To eliminate unneeded passes - Set a flag (Switch-

Occurred) to “No” before starting – When a switch occurs, set the

flag to “Yes”– If a pass has no switches, the

flag remains set to “No”– If the flag is “No” when exiting

the loop, the sort is finished