TUTORIAL 6 – BUBBLE SORTING AN ARRAY COS 151 TUTORIAL TUTORIAL 6 – BUBBLE SORTING AN ARRAY ENKH & IVA S. March 16, 2018
Admin Bringing textbooks to Practicals and Tutorials Extra help – COSTutorium! See Study Guide for times Make sure you’re attending Practicals & Tutorials, you need them for Exam Entrance! “You are required to have a mark for at least 60% of the practical lab experience tasks (i.e. 8 of the 12 sessions) to gain examination entrance. Note that simply completing a practical lab experience is insufficient, and that a mark must be awarded (read the submission instructions of each lab experience for information on what is required to get a mark for your work).” “You must have a result for at least 70% of the tutorials (i.e. 9 of the 13 tutorials) to gain examination entrance. Once again, this means that you must have a mark for the tutorials in question”
Functional, but NOT structured
Cannot have multiple decision diamonds point to the same statement block
Two loops cannot point back to the same position – a loop must always point back above its own diamond
Declaring Arrays e.g. num arr[5] = 3, 1, 7, 9, 4; In the above example, arr holds 5 elements and has an index from 0 to 4. Remember: The index of the first element is always 0.
Manipulating arrays Addition e.g. arr[0] + arr[1] = 3 + 1 Subtraction Multiplication Max, Min, etc… num arr[5] = 3, 1, 7, 9, 4;
Bubble Sort Simple Sorting Algorithm Comparison-based Adjacent elements are compared If elements are not in order, SWAP them
Pseudocode while swapped swapped = false for j from 0 to N - 1 swapped = true while swapped swapped = false for j from 0 to N - 1 if a[j] > a[j + 1] swap( a[j], a[j + 1] )
Bubble Sort - Example
https://visualgo.net/bn/sorting Bubble Sort Animation https://visualgo.net/bn/sorting
Task - Question 1 [Total: 7] Draw a structured flowchart that declares an array containing 5 numeric values (initialized as follows: 5, 9, 2, 7, 3). The program should then use bubble sort to modify the array so that the values are stored in ascending order.
References http://www.algorithmist.com/index.php/Bubble_sort https://www.tutorialspoint.com/data_structures_algorithms/bubb le_sort_algorithm.htm https://visualgo.net/bn/sorting