Let's populate an array with random integers:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 |
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?
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.
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
| 1000 | 900 | 800 | 700 | 600 | 500 | 400 |
Copy and paste your code into your evidence document and comment on any conclusions you have made.
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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| 16 | 30 | 11 | 29 | 12 | 30 | 19 | 33 | 31 | 29 |
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.
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?
| 77 | 45 | 99 | 104 | 120 | 123 | 129 |
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?
data type INTEGER pseudocode REAL program code DATE STRING CHAR bubble sort BOOLEAN declare variable