{"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

givenlist = [8, 132, 22, 34, 57, 2, 1, 9, 45, 87, 63, 80, 26, 65, 132]<\/pre>\n

Output:<\/strong><\/p>\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

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

givenlist = [\"hello\", \"this\", \"is\", \"BTechGeeeks\", \"python\", \"new\", \"online\",\r\n                   \"platform\", \"for\", \"all\", \"students\", \"who\", \"are\", \"excited\", \"about\", \"coding\"]<\/pre>\n

Output:<\/strong><\/p>\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

Sorting in descending order example<\/strong><\/p>\n

Example 3:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

givenlist = [8, 132, 22, 34, 57, 2, 1, 9, 45, 87, 63, 80, 26, 65, 132]<\/pre>\n

Output:<\/strong><\/p>\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

Example4:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

givenlist = [\"hello\", \"this\", \"is\", \"BTechGeeeks\", \"python\", \"new\", \"online\",\r\n                   \"platform\", \"for\", \"all\", \"students\", \"who\", \"are\", \"excited\", \"about\", \"coding\"]<\/pre>\n

Output:<\/strong><\/p>\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