Download presentation
Presentation is loading. Please wait.
1
TUTORIAL 6 – BUBBLE SORTING AN ARRAY
COS 151 TUTORIAL TUTORIAL 6 – BUBBLE SORTING AN ARRAY ENKH & IVA S March 16, 2018
2
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”
3
Functional, but NOT structured
4
Cannot have multiple decision diamonds point to the same statement block
5
Two loops cannot point back to the same position – a loop must always point back above its own diamond
6
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.
7
Manipulating arrays Addition e.g. arr[0] + arr[1] = 3 + 1 Subtraction
Multiplication Max, Min, etc… num arr[5] = 3, 1, 7, 9, 4;
8
Bubble Sort Simple Sorting Algorithm Comparison-based Adjacent elements are compared If elements are not in order, SWAP them
9
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] )
10
Bubble Sort - Example
11
https://visualgo.net/bn/sorting
Bubble Sort Animation
12
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.
13
References http://www.algorithmist.com/index.php/Bubble_sort
le_sort_algorithm.htm
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.