{"id":7413,"date":"2021-05-29T10:49:53","date_gmt":"2021-05-29T05:19:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=7413"},"modified":"2021-11-22T18:38:40","modified_gmt":"2021-11-22T13:08:40","slug":"python-program-for-bubble-sort","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-bubble-sort\/","title":{"rendered":"Python Program for Bubble Sort"},"content":{"rendered":"
What exactly is sorting? What’s the big deal about it? In this part, we will attempt to answer these questions.<\/p>\n
We’ve sorted everything from books in a library to words in a dictionary to database entries and processor instructions on a number of occasions.
\nThis means that when we sort things, we must first determine the criteria by which we will organize the items in the sequence provided to us. For the purposes of this lesson, we’ll suppose that the criteria is a number’s value, and we’ll sort a set of numbers.<\/p>\n
The most significant goal of sorting in computer science is to create efficient algorithms. Binary Search is a lightning-fast search algorithm that would be impossible to use in an unsorted set of objects.<\/p>\n
On sorted data, almost all set operations are extremely fast.<\/p>\n
Apart from creating efficient algorithms, sorting is employed when a program’s sole purpose is to sort things, such as when working with a deck of cards. As a result, sorting algorithms are one of the most important things for a programmer to understand.<\/p>\n
Sorting algorithms are programs that reorganize a huge number of elements into a certain order, such as from highest to lowest, or vice versa, or even alphabetically.<\/p>\n
These algorithms take an input list, process it (that is, perform operations on it), and then return a sorted list.
\nThe importance of sorting comes from the idea that if data is kept in a sorted fashion, data searching may be greatly improved. Sorting can also be used to display data in a more legible fashion. The instances of sorting in real-life circumstances are as follows:<\/p>\n
Telephone Directory :<\/strong>The telephone directory keeps track of people’s phone numbers, which are classified by their names so that they may be quickly found.<\/p>\n Dictionary :<\/strong> The dictionary organizes terms alphabetically so that searching for a specific word is simple.<\/p>\n Examples:<\/strong><\/p>\n Sorting in Ascending order<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Sorting in descending order example<\/strong><\/p>\n Example 3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example4:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In their Computer Science course, Bubble Sort is most likely the first sorting algorithm they learned about.<\/p>\n It’s very natural and simple to “translate” into code, which is vital for new software engineers who are learning how to turn their thoughts into something that can be executed on a computer. Bubble Sort, on the other hand, is one of the worst-performing sorting algorithms in all cases except when testing whether the array has previously been sorted, where it frequently beats more efficient sorting algorithms like Quick Sort.<\/p>\n Consider how the bubbles in a glass of soda climb up. The greatest\/smallest element in a sequence is represented by the bubbles, and the increasing movements of the bubbles reflect how the greatest\/smallest element travels to the end\/beginning of the sequence. For the sake of this tutorial, we’ll use the given array and sort it in ascending order of the numbers’ values.<\/p>\n Below is the algorithm for Bubble Sort:<\/strong><\/p>\n Below is the implementation of BubbleSort:<\/strong><\/p>\n Sorting the given list in Ascending Order:<\/strong><\/p>\n Output:<\/strong><\/p>\n Sorting the given list in Descending order<\/strong><\/p>\n Below is the implementation of BubbleSort:<\/strong><\/p>\n Output:<\/strong><\/p>\n The bubble sort has an O time complexity (n^2)<\/p>\n The following are examples of time complexities:<\/p>\n Worst case scenario:<\/strong> the provided list is in descending order. The algorithm executes as many executions as possible, which is denoted as [Big-O] We learnt what sorting is and how it is used in this tutorial, then we learnt how Bubble Sort works, devised an algorithm, and implemented Bubble Sort in Python.<\/p>\n Bubble Sort is one of many sorting algorithms available, and while it is not the greatest, it is simple to construct. The difficulty of this approach is O(n^2), which implies that if the number of elements in the list is doubled, the time it takes to sort them with this approach will increase by four times.<\/p>\n As a result, this technique becomes inefficient when dealing with big amounts of data. As a coder, though, understanding Bubble Sort is critical. What exactly is sorting? What’s the big deal about it? In this part, we will attempt to answer these questions. We’ve sorted everything from books in a library to words in a dictionary to database entries and processor instructions on a number of occasions. This means that when we sort things, we must first determine …<\/p>\ngivenlist = [8, 132, 22, 34, 57, 2, 1, 9, 45, 87, 63, 80, 26, 65, 132]<\/pre>\n
printing the list before sorting :\r\n8 132 22 34 57 2 1 9 45 87 63 80 26 65 132 \r\nprinting the list after sorting :\r\n1 2 8 9 22 26 34 45 57 63 65 80 87 132 132<\/pre>\n
givenlist = [\"hello\", \"this\", \"is\", \"BTechGeeeks\", \"python\", \"new\", \"online\",\r\n \"platform\", \"for\", \"all\", \"students\", \"who\", \"are\", \"excited\", \"about\", \"coding\"]<\/pre>\n
printing the list before sorting :\r\nhello this is BTechGeeeks python new online platform for all students who are excited about coding \r\nprinting the list after sorting :\r\nBTechGeeeks about all are coding excited for hello is new online platform python students this who<\/pre>\n
givenlist = [8, 132, 22, 34, 57, 2, 1, 9, 45, 87, 63, 80, 26, 65, 132]<\/pre>\n
printing the list before sorting :\r\n8 132 22 34 57 2 1 9 45 87 63 80 26 65 132 \r\nprinting the list after sorting :\r\n132 132 87 80 65 63 57 45 34 26 22 9 8 2 1<\/pre>\n
givenlist = [\"hello\", \"this\", \"is\", \"BTechGeeeks\", \"python\", \"new\", \"online\",\r\n \"platform\", \"for\", \"all\", \"students\", \"who\", \"are\", \"excited\", \"about\", \"coding\"]<\/pre>\n
printing the list before sorting :\r\nhello this is BTechGeeeks python new online platform for all students who are excited about coding \r\nprinting the list after sorting :\r\nwho this students python platform online new is hello for excited coding are all about BTechGeeeks<\/pre>\n
Program for Bubble Sort in Python<\/h2>\n
\n
1)Bubble Sort Introduction<\/h3>\n
2)Working of Bubble Sort (Brief)<\/h3>\n
\nSimply explained, we repeat the sequence numerous times, swapping numerous pairs of elements each time so that the greatest\/smallest element in the sequence ends up at one of the sequence’s endpoints.<\/p>\n3)Algorithm<\/h3>\n
\n
4)Implementation<\/h3>\n
# function which implements the bubble_sort algorithm for givenlist\r\ndef bubbleSort(givenlist):\r\n # finding the length of given list\r\n length = len(givenlist)\r\n # using nested for loops\r\n for i in range(length-1):\r\n for j in range(length-i-1):\r\n # checking if the jth element is greater than j+1 th element\r\n if(givenlist[j] > givenlist[j+1]):\r\n # if it is greater then swap the elements\r\n givenlist[j], givenlist[j+1] = givenlist[j+1], givenlist[j]\r\n\r\n\r\n# given list\r\ngivenlist = [8, 132, 22, 34, 57, 2, 1, 9, 45, 87, 63, 80, 26, 65, 132]\r\n# printing the list before sorting\r\nprint(\"printing the list before sorting :\")\r\nfor i in givenlist:\r\n print(i, end=\" \")\r\nprint()\r\n# passing this given list to bubbleSort function which sorts the given list\r\nbubbleSort(givenlist)\r\n# printing the list after sorting\r\nprint(\"printing the list after sorting :\")\r\nfor i in givenlist:\r\n print(i, end=\" \")\r\n<\/pre>\n
printing the list before sorting :\r\n8 132 22 34 57 2 1 9 45 87 63 80 26 65 132 \r\nprinting the list after sorting :\r\n1 2 8 9 22 26 34 45 57 63 65 80 87 132 132<\/pre>\n
# function which implements the bubble_sort algorithm for givenlist\r\ndef bubbleSort(givenlist):\r\n # finding the length of given list\r\n length = len(givenlist)\r\n # using nested for loops\r\n for i in range(length-1):\r\n for j in range(length-i-1):\r\n # checking if the jth element is less than j+1 th element\r\n if(givenlist[j] < givenlist[j+1]):\r\n # if it is greater then swap the elements\r\n givenlist[j], givenlist[j+1] = givenlist[j+1], givenlist[j]\r\n\r\n\r\n# given list\r\ngivenlist = [\"hello\", \"this\", \"is\", \"BTechGeeeks\", \"python\", \"new\", \"online\",\r\n \"platform\", \"for\", \"all\", \"students\", \"who\", \"are\", \"excited\", \"about\", \"coding\"]\r\n# printing the list before sorting\r\nprint(\"printing the list before sorting :\")\r\nfor i in givenlist:\r\n print(i, end=\" \")\r\nprint()\r\n# passing this given list to bubbleSort function which sorts the given list\r\nbubbleSort(givenlist)\r\n# printing the list after sorting\r\nprint(\"printing the list after sorting :\")\r\nfor i in givenlist:\r\n print(i, end=\" \")\r\n<\/pre>\n
printing the list before sorting :\r\nhello this is BTechGeeeks python new online platform for all students who are excited about coding \r\nprinting the list after sorting :\r\nwho this students python platform online new is hello for excited coding are all about BTechGeeeks<\/pre>\n
5)Time Complexity<\/h3>\n
\nBest case :<\/strong> When the provided list is already sorted, this is the best case scenario. The algorithm executes the fewest number of times possible, which is denoted as [Big-Omega]? (N)
\nAverage Case :<\/strong>When the list is in random order, this is the average situation. [Big-theta](n^2) is a symbol for average complexity<\/p>\n6)Conclusion<\/h3>\n
\nRelated Programs<\/strong>:<\/p>\n\n