Question: Here is code of bubble sort. It's run time complexity is O(na). Change the code in a way so that its time complexity is O(n)

 Here is code of bubble sort. It's run time complexity is

Here is code of bubble sort. It's run time complexity is O(na). Change the code in a way so that its time complexity is O(n) for the best case scenario. Find the best case and change the code a little bit. And explain how you did it in a comment block of your code. def bubbleSort(arr): for i in range(len(arr)-1): for j in range(len(arr)-i-1): if arr[j] > arr[j+1]: swap( arr[j+1], arr[j] ) The first line of the input will contain N, which is the size of the array. Next line will contain the N number of elements. Output will contain the sorted elements. P.S: sample input and output may not be the preferred answer choice. Input 1: 5 32145 Input 2: 6 10 20 5 15 25 30 Output 1: 1 2 3 4 5 Output 2: 5 10 15 20 25 30

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!