Input: [(`for`, 24), (` Geeks`, 8), (`Geeks`, 30)] Output: [(`Geeks`, 8), (` for`, 24), (`Geeks`, 30)] Input: [(` 452 `, 10), (` 256`, 5), (`100`, 20), (` 135`, 15)] Output: [(`256`, 5), (` 452 `, 10), (` 135`, 15), (`100`, 20)]
Method # 1: Using Bubble Sort
Using the Bubble Sort technique, we can perform sorting. Note that each tuple is an element in the given list. Access the second element of each tuple using nested loops. This is done by the in-place sorting method. Time complexity is similar to bubble sort, i.e. O (n ^ 2).
[(`Geeksforgeeks`, 5), (` is`, 10), (`a`, 15), (` portal`, 20), (`for`, 24), (`Geeks`, 28)]
Method # 2: Using of the sort () method
Sorting with this method changes the actual content of the tuple and, as in the previous method, executes the sort in place method.
[(`akash`, 5), (` rishav`, 10) , (`gaurav`, 15), (` ram`, 20)]
Method # 3: Using sorted () method
Sorted () method sorts the list and that`s it where returns a list with sorted elements without changing the original sequence. It takes three parameters, of which two are optional, here we tried to use all three:
Iterable: sequence (list, tuple, string) or collection (dictionary, set, frozenset ) or any other iterator that needs to be sorted.
Key (optional): a function that will serve as a key or basis for sorting comparisons.
Reverse (optional): to sort this in ascending order, we could simply ignore the third parameter we did in this program. If set to true, the iteration will be sorted in reverse (descending) order, by default it is set to false.
[(`akash`, 5), (` rishav`, 10), (`gaurav`, 15), (` ram`, 20)]
As the title promises, this book will introduce you to one of the world’s most popular programming languages: Python. It’s aimed at beginning programmers as well as more experienced programmers wh...
Computer languages have so far been of the ‘interpreted’ or the ‘compiled’ type. Compiled languages (like ‘C’) have been more common. You prepare a program, save it (the debugged version),...
Big data is, admittedly, an overhyped buzzword used by software and hardware companies to boost their sales. Behind the hype, however, there is a real and extremely important technology trend with imp...
The role of adaptation, learning and optimization are becoming increasingly essen- tial and intertwined. The capability of a system to adapt either through modification of its physiological structure ...