Sorting Algorithms COEN 352 TAWFIQ JAWHAR Bubble Sort
Sorting Algorithms COEN 352 TAWFIQ JAWHAR
Bubble Sort Animation: https: //youtu. be/nmhjr. I-a. W 5 o
What would the following array looks like after one iteration of bubble sort? Array = [12, 3, 5, 1, 20, 0]
What would the following array looks like after one iteration of bubble sort? Array = [12, 3, 5, 1, 20, 0] [3, 5, 1, 12, 0, 20] Biggest value will be at the end
What is the best time complexity of bubble sort and when does it happen?
What is the best time complexity of bubble sort and when does it happen? Best time complexity O(N) if we optimize it otherwise it’s O(N 2) because it will have to go till the end. In the optimized version: It happens when the array is already sorted. We can keep track of whether any element was swapped during the iteration and we can stop the iterations if there’s no swaps. Which means we only go through the numbers once Note there's a difference between upper and lower bounds and worst and best cases. The growth rate applies to the change in cost as a change in input size occurs.
Is bubble sort stable?
Is bubble sort stable? A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted. Bubble sort is stable If two elements are equal they will not be swapped and they will stay in the same order they were in before sorting. (unless you implemented otherwise)
Is bubble sort in place?
Is bubble sort in place? in-place algorithm is an algorithm which transforms input using no auxiliary data structure. However a small amount of extra storage space is allowed for auxiliary variables.
Basically never use bubble sort
Selection Sort Animation: https: //youtu. be/x. WBP 4 lzkoy. M What is worst case? Best case? Can we optimize it like we did with bubble sort for best case? Less swaps than Bubble sort. Still bad.
Can we do better? Yes, yes we can! If we can divide the problem into smaller and simpler problems then use those solutions to reach a solution to the main problem Divide and conquer
Quick Sort Animation with implementation: https: //youtu. be/SLau. Y 6 Ppj. W 4 What controls whether this algorithm in the best case or worst case? What would you do to lower the chance of having a worst case quick sort?
Out of all the algorithms we have seen so far (including insertion sort), which one is the best? ?
Data Structure and Algorithms 101 IT DEPENDS. ALWAYS.
Banks often record transactions on an account in order of the times of the transactions, but many people like to receive their bank statements with checks listed in order by check number. People usually write checks in order by check number, and merchants usually cash them with reasonable dispatch. The problem of converting time-of-transaction ordering to checknumber ordering is therefore the problem of sorting almost-sorted input. The number of checks to be sorted is less than 50. What sorting algorithm would you use? Question from: Introduction to Algorithms Hardcover by Thomas H. Cormen (Author), Charles E. Leiserson (Author), Ronald L. Rivest (Author), Clifford Stein (Author)
- Slides: 17