Updated April 13, 2023
Introduction to Python Sort Array
The following article provides an outline on Python Sort Array. Array in a python is a group of elements similar to list and tuples, but the only difference is that array can have values of the same type while lists and tuples can have values of other types also. The size of the array in python is not fixed; it can be increased and decreased at any time. If we want to sort the array elements by ascending or descending, we have to make a function sorting function. We have to import the array package to make an array first.
How does Sort Array work in Python?
There is no direct method available on the array to sort in python as compared to the list. In this case, we first have to import the array package, create our array type. Then we have to make a custom sorting function to sort the array. There are many data structure algorithms available that can be used for sorting.
For example, Bubble sort, Selection sort and Insertion sort.
We will use three sorting algorithms for our arrays.
Let us create an array:
- import array as arr
- sample_array = arr.array(’I’,[4,2,5,9,1,8,6])
Examples of Python Sort Array
Given below are the examples of Python Sort Array:
Example #1 – Bubble Sort
Code:
import array as arr
sample_array = arr.array('i',[1,2,4,5,6,8,9])
def bubblesort(sample_array):
for j in range(len(sample_array)-1,0,-1):
for i in range(j):
if sample_array[i]> sample_array[i+1]:
temp = sample_array[i]
sample_array[i] = sample_array[i+1]
sample_array[i+1] = temp
bubblesort(sample_array)
print(sample_array)
Output:
Explanation:
- The above program is bubble sorting. We have created a sample_array array of integer type. As we know, every value inside the array is indexed, and indexing starts from 0. Swapping is used while sorting; we have created a temporary variable temp that will be storing intermediate value for every loop iteration.
- In bubble sorting, index i is compared with index i+1; if index i > index i+1, we swap these two values using the temp variable. The comparison was carried out for index + 1 with an index +2 position until the last index. After this inner loop is completed and the array’s highest value, i.e. 9 is now the last element of the array.
- Now outer loop runs again, and inner loops for all values till all the values are sorted. ‘len’ function calculates the length of the array, a number of times array will run, len – 1 used as array started from 0. 0 denotes going from (len – 1) to 0 values, and -1 denotes going in decreasing order.
Example #2 – Selection Sort
Code:
import array as arr
sample_array = arr.array('i',[4,2,5,9,1,8,6])
def selectionsort(sample_array):
for i in range(len(sample_array)):
min_val = i
for j in range( i +1, len(sample_array)):
if sample_array[min_val] > sample_array[j]:
min_val = j
sample_array[i], sample_array[min_val] = sample_array[min_val], sample_array[i]
sample_array = arr.array('i',[4,2,5,9,1,8,6])
selectionsort(sample_array)
print(sample_array)
Output:
Explanation:
- In selection sort, we sort the array by finding the minimum value. At first, we find the minimum value from the whole array and swap this value with the array’s first element. Then the first value is ignored, and minimum values are found from the rest of the array; in this way, we find the second minimum value, and these values will be swap this second value of the array.
- Now we find the minimum from the third value of the array and keep swapping; in this whole array is sorted. In this case, we also have to use a temp variable that will be storing the min value for every iteration.
- In the above program, we are running two loops. For inner loops, we are finding the min value and assigning it to the ‘i’ variable. Inner loops run for 1 value, and outer loops run for each and every value. ‘min_val’ stores the min value of the array, and it is ignored for the iteration.
Example #3 – Insertion Sort
Code:
import array as arr
sample_array = arr.array('i',[1,2,4,5,6,8,9])
def insertion_sort(sample_array):
for i in range(1, len(sample_array)):
j = i-1
temp = sample_array[i]
while (sample_array[j] > temp) and (j >= 0):
sample_array[j+1] = sample_array[j]
j=j-1
sample_array[j+1] = temp
sample_array = arr.array('i',[4,2,5,9,1,8,6])
insertion_sort(sample_array )
print(sample_array)
Output:
Explanation:
- In Insertion sort, we consider the first element of the array to be sorted. Now we pick the second element of the array and compare it with the first; if the second value is greater, then it is placed after the first element. Now the first and second elements are sort. Now we pick the third element to compare it with the first element; if a third element is smaller than the first, then it is placed at the first position, and the element at the first and second position is shifted forward.
- If a third element is greater than the first, then it is compared with the second element.If small then placed the second position, the second position element moves to third. If the element is greater, then it is placed in the third position. Now the first three elements are sorted.
- In this way, iteration continues until the last element. First loop ‘1, len’ indicates element runs from index position to the length of the array. The temp variable is storing the current element. J is storing an index position. Now we are comparing the first element with index + 1; if true, then we are place index + 1 element to the second position.
Conclusion
Python array is of single data type, and we don’t have any specific sort method, so we write custom sorting method. There are many algorithms available. You can pick any of them, which is comparatively faster. Out of that bubble, sorting is the easiest and faster and most widely used algorithm.
Recommended Articles
We hope that this EDUCBA information on “Python Sort Array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.