Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number.

Similar presentations


Presentation on theme: "Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number."— Presentation transcript:

1 Bubble sort

2 Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number is larger (smaller) than the next one, swap the numbers Move to the next member of array numArray[0]numArray[1]numArray[2]numArray[3]numArray[4] 510192 1259 20152

3 Swapping numbers When swapping 2 numbers in computer memory, one needs to be stored in a temporary location to overwriting losing it! 1.temp = numArray[i]; 2.numArray[i] = numArray[i + 1]; 3.numArray[i + 1] = temp; 5743 numArray[i] numArray[i + 1] temp 20153

4 The direction of sorting The data can be sorted in both ways (increasing and decreasing order) using the bubble sort algorithm The comparison is always between 2 items next to each other To sort to the increasing order if (numArray[i] > numArray[i + 1]) Using the basic, unmodified algorithm, its recommended to only use the increasing order due to limitations in the basic algorithm 20154

5 Necessary loops You’ll need to use nested loops, otherwise only the largest / smallest item will be in the right place for (i = 0; i < ARRAY_LEN; i++) { for (j = 0; j < ARRAY_LEN; j++) { // write code here } NB! Think about the loops given, make corrections and improvements! 20155

6 Example Original array: 510192 1. iteration 511092 519 2 5192 2. iteration 159210 1529 3. iteration 125910 4. iteration 125910 Result: 125910 20156

7 Lab task [algorithm + code] Ask the user for 5 numbers, store them in an array Sort the array in ascending order Show the result from the array in ascending array Output the result in descending order without resorting the array! Optimize the inner loop to run 1 time less by each iteration of the outer loop. This will save workload and time consumed by sorting about 50%! Ready? Draw the algorithm 20157

8 Advanced improvements 1)Instead of 5 number vector, use a 5x5 matrix and sort it row-by-row initialize your array, look for code on the web 2)Optimize your loops even more. If there were no swaps made, then the array is already in order. No need to reduce inner loop by 1. 3)For even more advanced optimization, remember how many numbers in the end are already in their place and skip then in whole. 4)Feeling brave? Ask us about turtles 20158

9 Voluntary homework Remember the warehouses algorithm? Packages are stored in 2 warehouses They are combined in a sorting center (third warehouse) They exit the sorting center heavier first Write code based on the algorithm Requirements Initialize the cargo weights in warehouses Declare all of the variables in the beginning of main() function For simplicity, limit the size of the warehouses using #define Output the original warehouse cargo weights and the exit order from sorting center 20159

10 Base code #include #define SIZE_A 8 #define SIZE_B 5 int main(void) { // write the necessary variable declarations here // write the combining of warehouses here // write the sort here // write the output here return 0; } 201510


Download ppt "Bubble sort. Quite slow, but simple Principles: Compare 2 numbers next to each other (lets call it current and the one next to it) If the current number."

Similar presentations


Ads by Google