Objectives

Students will be able to:

  • design and call a basic function in Java

Big O

Big O describes the performance of an algorithm in terms of time or space (ie memory usage) as the input size increases.

Bubble Sort

toptal visualisations
visualgo visualisations

Bubble Sort

 

Scenario

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

Bubble Sort Simulation

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!

01234

Algorithm

Every time we click the sort button, an algorithm processes the array, starting at the first element. This is the algorithm:

  1. set index = 0 (we could use a for loop to do this)
  2. compare value at index with value at index + 1
  3. if the value at index is greater than index + 1, swap the values
  4. increment index and repeat steps 2 and 3 until index is 3 (not 4, can you see why?!)

Big O

Bubble Sort has a of O(n2)

By the end of this unit you should be able to explain why.

Practical Activity #1

Copy/Paste the code into your IDE and complete Worksheet

Practical Activity #2

Continue to Part 2.

Practical Activity #3

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.

Helper video

Practical Activity #3 - Intended for students chasing high grades.

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.

457799104120123129

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?


Tags

data type INTEGER pseudocode REAL program code DATE STRING CHAR bubble sort BOOLEAN declare variable


Feelings

How do you feel right now?