Introduction to Python Sort Array
This article will provide you a brief summary of Python’s Sort Array function. Arrays in Python are collections of items that are very similar to lists and tuples, with the key distinction being that arrays may only include values of the same type, while lists and tuples can contain values of any type. In Python, the size of an array is a variable that may be changed at any moment. It is necessary to create a sorting function in order to sort the array items in an ascending or descending order. To create an array, we must first load the array package.
first.
How does Sort Array work in Python?
When comparing lists and arrays in Python, it is clear that the latter lacks a straightforward sorting option. As such, we need to bring in the array package and then define our array type. Finally, to sort the array, we must develop our own sorting code. Many methods for sorting data structures are now at your disposal.
Some common examples are Bubble sort, Selection sort, and Insertion sort.
We’re going to utilise three different algorithms to sort our arrays. Let’s make an array:
- import array as arr
- sample_array = arr.array(’I’,[4,2,5,9,1,8,6])
Examples of Python Sort Array
Python Sort examples are shown below.
Array:
Code:
Example #1 – Bubble Sort
Its Output is import array as arr
sample_array = arr.array('i',[4,2,5,9,1,8,6])
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)
:
Clarification:
- 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.
Number-Code
Example #2 – Selection Sort
Product 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)
Reasoning:
- 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.
Indicator
Example #3 – Insertion Sort
:
Resulting import array as arr
:
sample_array = arr.array('i',[4,2,5,9,1,8,6])
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 )
Clarification:
- 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.