Variations in Different Sorting Techniques in Python

What 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 Python

There are various sorting techniques in Python. They are:

  • Selection Sort
  • Insertion Sort
  • Bubble Sort
  • Shell Sort
  • Merge Sort
  • Quick Sort

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( )
  2. sorted( )
  3. argsort( )
  4. lexsort( )

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,

  • inp_arr is the name of the input iterable, including array, list, tuples, etc., we want to sort.
  • sort( ) is the method in Python used to sort different elements

The sort method takes two optional arguments. These arguments are not required with the sort( ) method:

  • reverse is the parameter that specifies the order in which the input is to be sorted (ascending or descending). It takes a boolean value (True or False). By default, it is set as False, i.e., it will sort the input in ascending order by default. The true value of the reverse argument will sort the array in descending order.
  • key specifies the detailed criteria to sort the input array or list accordingly.

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,

  • sorted_list is the output iterable after sorting.
  • sorted( ) is the function in Python to sort various elements

The sorted( ) method takes 3 arguments in which two arguments are optional and one is required.

  • inp_list is the input iterable, including the list, tuple, dictionary, array, etc., which we want to sort. This is the required argument in the method.
  • reverse is the optional argument that specifies the order in which the input is to be sorted (ascending or descending). It takes a boolean value (True or False). By default, it is set as False, i.e., it will sort the input in ascending order by default. The true value of the reverse argument will sort the array in descending order.
  • key is another optional argument that specifies the order by which the input needs to be sorted. By default, it does not have any value.

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,

  • inp_arr is the input array that needs to be sorted

The argsort( ) takes a few optional arguments as follows:

  • axis is the axis along which the array is to be sorted. By default, it is set as -1. If the value is none, then it uses a flattened array.
  • Kind specifies the type of sorting algorithm according to which the array is to be sorted.
  • Order specifies the fields of the array that need to be sorted

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,

  • lexsort( ) is the function in Python used to sort more than one array
  • keys are the columns to be sorted.
  • axis is an optional argument that specifies the axis needs to be sorted indirectly.

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.






Latest Courses