{"id":7471,"date":"2021-05-29T10:48:29","date_gmt":"2021-05-29T05:18:29","guid":{"rendered":"https:\/\/python-programs.com\/?p=7471"},"modified":"2021-11-22T18:38:40","modified_gmt":"2021-11-22T13:08:40","slug":"python-program-for-selection-sort","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-selection-sort\/","title":{"rendered":"Python Program for Selection 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 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 Selection Sort is a comparison sorting algorithm that uses a random list of items to sort them in ascending order. The comparison does not necessitate a lot of extra room. It just need one more memory space for the time variable.<\/p>\n This is referred to as in-place sorting. The temporal complexity of the selection sort is O(n^2), where n is the total number of items in the list. The number of iterations necessary to sort the list is measured by the time complexity. The list is separated into two sections: the first contains sorted items, and the second contains unsorted items.<\/p>\n The sorted list is empty by default, but the unsorted list contains all of the entries. The minimal value is then found in the unsorted list and entered into the sorted list. This procedure is repeated until all of the data have been sorted and compared.<\/p>\n To determine if the first item in the unsorted partition is the minimum value, it is compared to all of the values on the right-hand side. Its position is swapped with the minimal value if it is not the minimal value.<\/p>\n Below is the implementation of Selection Sort:<\/strong><\/p>\n Sorting the given list in Ascending Order:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation :<\/strong> Here we can see that the given list of strings is sorted in Ascending order<\/p>\n Below is the implementation of Selection Sort:<\/strong><\/p>\n Sorting the given list in Descending Order:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation :<\/strong> Here we can see that the given list of strings is sorted in Descending order<\/p>\n The sort complexity expresses the number of execution times required to sort the list. There are two loops in the implementation.<\/p>\n The outer loop, which selects values from the list one by one, is executed n times, where n is the total number of elements in the list.<\/p>\n The inner loop, which compares the value from the outer loop to the remaining values, is also executed n times, where n is the total number of elements in the list.<\/p>\n As a result, the number of executions is (n * n), which can also be written as O. (n^2).<\/p>\n The difficulty of the selection sort is divided into three categories:<\/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] O(n^2) In this tutorial, we learned how Selection Sort works, how to implement it in Python.<\/p>\n Selection Sort, like Bubble and Insertion Sort, has an O complexity (n^2). This indicates that doubling the input size increases the time it takes to execute the method by four times, making it an inefficient sorting method.<\/p>\n It is less efficient than Insertion Sort in general, but it is considerably easier to understand and apply. 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 Selection Sort in Python<\/h2>\n
\n
1)Selection Sort Introduction<\/h3>\n
2)Working of Selection Sort<\/h3>\n
3)Algorithm<\/h3>\n
\n
4)Implementation<\/h3>\n
\n
# function which implements the selection_sort algorithm for givenlist\r\ndef selectionSort(givenlist):\r\n length = len(givenlist)\r\n for i in range(length):\r\n # To begin, consider the first element in the unsorted section to be the smallest.\r\n minValue = i\r\n\r\n for j in range(i+1, length):\r\n if (givenlist[j] < givenlist[minValue]):\r\n # If a smaller element is identified, update the position\r\n # of the minimum element.\r\n minValue = j\r\n\r\n # Replace the smallest(minValue) element with the first element\r\n # of the unsorted portion.\r\n givenlist[i], givenlist[minValue] = givenlist[minValue], givenlist[i]\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 selectionSort function which sorts the given list\r\nselectionSort(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\nBTechGeeeks about all are coding excited for hello is new online platform python students this who<\/pre>\n
# function which implements the selection_sort algorithm for givenlist\r\ndef selectionSort(givenlist):\r\n length = len(givenlist)\r\n for i in range(length):\r\n # To begin, consider the first element in the unsorted section to be the largest.\r\n maxValue = i\r\n\r\n for j in range(i+1, length):\r\n if (givenlist[j] > givenlist[maxValue]):\r\n # If a larger element is identified, update the position\r\n # of the larger element.\r\n maxValue = j\r\n\r\n # Replace the largest(maxValue) element with the first element\r\n # of the unsorted portion.\r\n givenlist[i], givenlist[maxValue] = givenlist[maxValue], givenlist[i]\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 selectionSort function which sorts the given list\r\nselectionSort(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^2)
\nAverage case:<\/strong> When the list is in random order, this is the average situation. [Big-theta]?O(n^2) is the average level of complexity
\nBecause it only uses one temporal variable to exchange values, the selection sort has an O(1) space complexity.<\/p>\n6)Conclusion<\/h3>\n
\nRelated Programs<\/strong>:<\/p>\n\n