Sorting algorithms more efficient than bubble sort [closed]
2024/11/23 18:20:25
I have written this algorithm to sort a list using bubble sort. Is this the most efficient way to sort a list?
If not, why?
What makes it less efficient and what are the alternatives present?
def BubbleSort(List):for i in range(len(List)-1):for Number in range(len(List)-1):if List[Number] > List[Number+1]:List[Number], List[Number+1] = List[Number+1], List[Number]print(BubbleSort([5,2,1,4,3])
Thanks!
Answer
In your above algorithm, if the length of your array is 5, it will execute the inner if statement25 times. In general when you will have a list of n size, it will for sure do n^2 operations excluding the for loop and swapping.
For a list of size 10^6, it will be 10^12 operations atleast. C or C++ do around 10^9 operations per second. So this code of yours will take 10^3 seconds which is more than half an hour. So that's very much inefficient.
There are better sorting algorithms which you can use instead of bubble sort as they are faster than this.
Merge Sort
Quick Sort
Heap Sort
A lot of other techniques are also there but these are most common used.
Apart from that, you don't need to implement these algorithms as one of the most efficient one is already implemented in standard library of mostly each language from C to Rust. You can just use that implementation and even pass you own comparator function if you want.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…
Here is my code:def numbers_in_lists(string):num = int(string)l = list(num)return lstring = 543987When i run it:print numbers_in_lists(string)I have the following error:l = list(num)
TypeError: int obj…
I came to this question while pondering about the ordering of set, frozenset and dict. Python doesnt guarantee any ordering, and any ordering is coupled to the hash value at some level. But is the hash…
This doesnt work for me.$ python django-admin.py startproject myprojectI am running a ubuntu virtual m/c on my windows system.
By default ubuntu 12.04 comes with python 2.7.3 so I am using that only
I …
This question already has answers here:Difference between multiple ifs and elifs?(9 answers)Why we use if, else if instead of multiple if block if the body is a return statement(13 answers)Closed 7 ye…
Im new with python. Could anybody help me on how I can create a regular expression given a list of strings like this:test_string = "pero pero CC tan tan RGantigua antiguo AQ0FS0que que CS segn se…
This question already has answers here:Reversing words in a Python string (including punctuation)(5 answers)Closed last month.I am trying to reverse words of a string, but having difficulty, any assist…
I have an input file (input.txt) with the following information:Number of students (first line)
Number of test scores (second line)
list of student names and scoresSo the text file looks something like…
This question already has answers here:Split string with multiple-character delimiter(3 answers)Closed 6 years ago.I want extract strings from this text with regex:~ZCC0ZAF~World~AAEef~RZgthAD~AAjaKNed…
Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…