Bubble Sort. Sorting Computers only use numeric values for sorting Does this mean you cannot sort...
-
Upload
lesley-holmes -
Category
Documents
-
view
215 -
download
0
description
Transcript of Bubble Sort. Sorting Computers only use numeric values for sorting Does this mean you cannot sort...
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 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)
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
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
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
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
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
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
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
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:
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
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
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
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
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
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
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
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
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
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
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?
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
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