A bus joins Saturday Bus Racing.
It joins 5 races.
Its times are recorded in an array of doubles.
The slowest time will be compared with other racing buses slowest times once the 5 races are complete.
double[] racer1Times = new double[5];
We need to find out which which value is the biggest in the array ie the slowest time.
One strategy is to implement a
Let's simulate sorting an array of bus times in ascending order.
When we have finished, we know the value at index 5 is the slowest time!
| 0 | 1 | 2 | 3 | 4 |
Every time we click the sort button, an algorithm processes the array, starting at the first element. This is the algorithm:
Bubble Sort has a of O(n2)
By the end of this unit you should be able to explain why.
Note in the sample code we get an idea of how many times the algorithm is performing a swap.
Change the values in the array so that the array is already sorted.
How many times do we see the swap message (hopefully 0).
But how many times is the algorithm attempting to swap?
Is there a way to make the algorithm smarter so that it stops trying to swap when the array is sorted?
Do some research on this.
You can buffer your notes by adding any insights into the worksheet.
Using your solution from Practical Activity #2, what do you notice for different data sets.
For example, test your algorithm for an array that is already sorted in ascending order of value eg.
| 45 | 77 | 99 | 104 | 120 | 123 | 129 |
Is your algorithm smart enough to realise that no sorting is required?
Research a more efficient Bubble Sort algorithm. It should stop processing the array when no more sorting is required and, during each pass, it should only process elements that need to be processed.
Use the Internet; use your textbook; use your creative, problem-solving mind.
Copy and paste your code into your evidence document and comment on any observations and/or conclusions you have made. Did you manage to create a program that efficiently bubble sorts an array of integers?
Put all of the elements of numArr in ascending order and then run the code. How many passess occur?
data type INTEGER pseudocode REAL program code DATE STRING CHAR bubble sort BOOLEAN declare variable