Bubble Sort

array data structures

Let's populate an array with random integers:

0123456
  1. Every time we click the sort button, an algorithm processes the array, starting at the first element.
  2. It compares the value at the element with index i with the value at element i+1 and if the value at i is bigger, it swaps them!
  3. It keeps doing this until i reaches the second-last index in the array.

FOR i ← 0 TO 5

IF numArr[i] > numArray[i+1] THEN

temp ← arr[i]

arr[i] ← arr[i+1]

arr[i+1] ← temp

END IF

END FOR

OUTPUT numArray

Can you see why we need to stop the loop at index 5 is this example?

Also, what do you notice at the end of each pass through the array - ie every time you press sort?

   Practical Activity #1

Using an IDE and high-level language of your choice, set up a scenario where an array of integers has been declared and initialised with values.

Create code using the above algorithm to process 1 pass through the array.

You should see that after this first pass, the largest number in the array will be found at the last index of the array.

Copy and paste your solution into your evidence document. Include any questions or observations you have about the algorithm.

   Practical Activity #2

How many times would we need to run the above algorithm until the array is fully sorted?

For example, if we have a small array, we could do this:

FOR i ← 0 TO 999

sorting algorithm above

END FOR

Of course, we are probably doing some unnecessary processing. Surely a small 7-element array will be sorted without needing 1000 passes!

Test your own ideas. How many times does the sorting algorithm need to be repeated before an array is full sorted?

Hint: to test an extreme situation, you could create an array sorted in descending order of value and see how many passes are required to sort it into ascending order of value eg

1000900800700600500400

Copy and paste your code into your evidence document and comment on any conclusions you have made.

   Practical Activity #3

There is a 2-diemsional array, nums of 5 rows and 2 columns. It stores temperature samples for 5 days in a month eg

day temp
1630 1129 1230 1933 3129

Can we sort this 2-dimensional array into ascending order of day?

See if you can figure out an algorithm for this problem. Paste pseudocode of a solution into your evidence document.

   Practical Activity #4 - Efficiency

Imagine if an array was already sorted before we run the sorting algorithm.

Is it possible to write an algorithm which will stop running when it realises the array is fully sorted?

For example, in the following array, how many passes would the algorithm need to make for the array to be fully sorted? The answer is 2. Can you see why?

774599104120123129

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?


   

This unit requires you to create an evidence document.

Tags

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


Feelings

How do you feel right now?