Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bubble sort.

Similar presentations


Presentation on theme: "Bubble sort."— Presentation transcript:

1 Bubble sort

2 Sorting algorithms Basic components Selecting the best solution
How to choose items to compare? (algorithm) How to compare? (data types, what comes first?) Repositioning elements (data-structure dependent) Selecting the best solution Compatibility with data structure Nature of the data (e.g. data is nearly sorted, many similar items, …) Speed vs memory 2018 Risto Heinsar

3 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 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] 5 10 1 9 2 1 2 5 9 10 2018 Risto Heinsar

4 Example <- inner loop -> <- outer loop -> Original array:
5 10 1 9 2 1. iteration 2. iteration 3. iteration 4. iteration Result: <- outer loop -> 2018 Risto Heinsar

5 Swapping numbers When swapping 2 numbers in computer memory, one needs to be stored in a temporary location This is so that we don’t overwrite anything and lose data temp = numArray[i]; numArray[i] = numArray[i + 1]; numArray[i + 1] = temp; numArray[i] numArray[i + 1] 57 43 temp 2018 Risto Heinsar

6 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]) It’s recommended to sort in increasing order unless you do some modifications for the algorithm 2018 Risto Heinsar

7 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! 2018 Risto Heinsar

8 Home task (1/2) Take the UML we did last week and make a program out of it The cargo is being brought from two warehouses to sorting facility (combine two arrays into one) The cargo must exit, heaviest items first (sort) Some notes: Declare all variables in the beginning of main() function For simplicity, use #define to limit warehouse size Use can initialize the arrays (warehouses) Output both the original arrays and the exit order 2018 Risto Heinsar

9 Home task (2/2) Take the bubble sort code we made today
Convert it into UML Think carefully about the following: How to do nested loops!? When do we increment the counters? When do we initialize counters to zero? Where is the switching of the numbers? 2018 Risto Heinsar

10 Lab task (bubble sort) 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%! 2018 Risto Heinsar

11 Advanced Instead of 5 number vector, use a 5x5 matrix and sort it row-by-row. Initialize your array, there is a suitable coding sample available 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. For even more advanced optimization, remember how many numbers in the end are already in their place. Limit the next iteration by that. Modify the bubble sort so that you could sort strings Hint: strcmp() 2018 Risto Heinsar


Download ppt "Bubble sort."

Similar presentations


Ads by Google