Variations in Different Sorting Techniques in PythonWhat is Sorting?Sorting is the technique used to arrange the data in a specific order. It arranges the numerical data in ascending or descending order. It is used to represent the data in a simpler way, which is much more understandable. Various algorithms are made based on the sorting techniques. Sorting Techniques and their Variations in PythonThere are various sorting techniques in Python. They are:
Besides these Sorting Algorithms, different variations are used for sorting in Python. These techniques include different sorting functions that behave differently in sorting. Let's understand these variations in depth using different examples.
1) sort( )This function is used to sort the array in place. It means it modifies the array or list directly. It alters the original order of the elements. It does not make any copy of the input array or list. Its return type is None, which means it does not return anything. It is a faster method to sort an array. It occupies less space as it sorts the input directly. The basic syntax of the sort( ) function in Python Here,
The sort method takes two optional arguments. These arguments are not required with the sort( ) method:
Let's understand the sort( ) by implementing it in Python. Ascending Order Code: Output: Unsorted Array: [99, 3, 56, 22, 15, 0] Sorted Array: [0, 3, 15, 22, 56, 99] Explanation: We sorted the array using the sort( ) method in ascending order. This method does not make a copy of the array; it just sorts the original input array. Descending Order Code: Output: Unsorted Array: [90, 13, 46, 82, 5, 1000] Sorted Array: [1000, 90, 82, 46, 13, 5] Explanation: We sorted the array using the sort( ) method in descending order. We have set the reverse parameter as false, which sorts the array in descending order. This method does not make a copy of the array; it just sorts the original input array. 2) sorted( )This is another method to sort the array. Unlike the sort( ) method, it makes a copy of the input list, then sorts it and returns the new sorted array or list. It takes up more space as it makes a copy of the input array. It returns the list after sorting it. It is slower than the sort( ) method. The basic syntax of the sorted() method in Python: here,
The sorted( ) method takes 3 arguments in which two arguments are optional and one is required.
Let's understand the sorted( ) method by implementing it in Python. Ascending Order Code: Output: Unsorted Array: [199, 1223, 145, 256, 67, 89, 0] Sorted Array [0, 67, 89, 145, 199, 256, 1223] Explanation: Using the sorted( ) method, we have sorted the array in ascending order. Firstly, we will define an input array and then make an object that will copy the original array, sort it, and return the copied sorted elements. Descending Order Code: Output: Unsorted Array: ('a', 'c', 'z', 'abccd', 'p', 'pillow', 'i') Sorted Array: ['pillow', 'abccd', 'a', 'c', 'z', 'p', 'i'] Explanation: Using the sorted( ) method, we have sorted a string array in descending order. Firstly, we will define an input array and then make an object that will copy the original array. We have set the reverse value as False, which will sort the array in descending order according to the length of the elements in the array as the key argument is set as len (length). Then, it will return the output array after sorting. 3) argsort( )This method is another sorting technique used in Python. The argsort( ) method sorts the input array indirectly and returns the indices of the sorted array in the same shape as the input array. It occupies more space as a new array is returned containing the indices of the sorted array. The basic syntax of the argsort( ) method is: here,
The argsort( ) takes a few optional arguments as follows:
Let's understand the argsort( ) by implementing it in Python: Code: Output: Unsorted Array: [19, 129, 153, 326, 41, 93, 10] Sorted Indices [6 0 4 5 1 2 3] Sorted Array: [ 10 19 41 93 129 153 326] Explanation: Using the argsort( ) method, we have sorted the array in ascending order. Firstly, we will define an input array and then make an object that will copy the original array, sort it, and return the indices of the sorted array. Then, we define another array that will copy the sorted array from the indices and then print the sorted array. lexsort( )This method is also used to sort elements indirectly using a sequence of keys. It can sort more than one array at a time. We need two input iterables for the sorting with lexsort( ). Firstly, it will sort by the first array and then by the second one. As a result, it returns an array containing the indices of the sorted keys. The basic syntax of the lexsort( ) function is: here,
Let's understand the lexsort( ) by implementing it in Python: Code: Output: Unsorted Array: [10 23 4 67] [ 3 56 78 2] Sorted Indices: [2 0 1 3] Sorted Array: [(4, 78), (10, 3), (23, 56), (67, 2)] Explanation: Using the lexsort( ) method, we sorted two columns in ascending order. Firstly, it will sort the col1, then sort the col2, and print the indices accordingly. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India