Home  >  Article  >  Backend Development  >  Python program to sort array

Python program to sort array

PHPz
PHPzforward
2023-08-26 22:21:161279browse

Python program to sort array

Generally speaking, there are multiple ways to sort an array. The array can be sorted in ascending or descending order. An array sorted in ascending order will have smaller elements first, then larger elements. An array sorted in descending order will have larger elements first, then smaller elements.

Using the basic features of the Python language, arrays can generally be sorted. For this purpose, two loops are used.

Input and output scenarios

Consider an array arr = [ 5, 9, 1, 10, 3 ].

Now, let us sort the array by comparing one element to another.

  • Initially compares the first element to the remaining elements.

  • Repeat the same process until the last element so that the entire array is organized or sorted.

The array sorted in ascending order by "arr" is sorted_array = [1, 3, 5, 9, 10].

algorithm

  • Step 1 - Initially, an array must be created or declared with some elements. Please note that all elements present in the array should be of similar data type.

  • Step 2 - In order to sort the elements, the first step is to compare the elements. Compares an element to the remaining elements. If the element is smaller than other elements, it is placed at the first position of the array (when an array sorted in ascending order is required). Therefore, to do the comparison, two loops are required. The first loop is used to select an element from the array accordingly.

  • Step 3 - The second loop or inner loop is used to compare the elements selected in the first loop with other elements. Selection of elements is done by incrementing the index number. This step is one of the most important in the entire sorting process.

  • Step 4 - After comparing the elements, check if the element selected in the outer loop is smaller than the element selected in the inner loop. If the element is smaller than another element, the value of the element is swapped using the "temp" variable. The "temp" variable is used as a bridge in order to store the values ​​of other elements and exchange values ​​easily. (This is mainly to sort the array in ascending order).

  • Step 5 - Continue and repeat the entire process by increasing the index value in the loop (inner and outer loop) until all elements are compared and sorted. After the entire process is completed, the resulting array is the desired sorted array.

Example

In the following example, we will understand the process of sorting an array by placing elements in ascending order.

arr = [5, 9, 1, 10, 3, 8, 4, 2, 7, 6]  
temp = 0
max_size = len(arr)
print("The elements of the array before sorting: ");
for i in range(0, max_size):
   print(arr[i], end=" ")  

print()   
for i in range(0, max_size):
   for j in range(i+1, len(arr)):
      if(arr[i] > arr[j]):
         temp = arr[i]
         arr[i] = arr[j]
         arr[j] = temp
print("The elements of the array after sorting: ")

for i in range(0, max_size):
   print(arr[i], end=" ") 

Output

The output of the above program is as follows -

The elements of the array before sorting: 
5 9 1 10 3 8 4 2 7 6
The elements of the array after sorting:
1 2 3 4 5 6 7 8 9 10

in conclusion

In this way, the process of sorting an array in ascending or descending order works by using basic features of Python such as loops, conditional statements, etc. This is not considered an efficient technique as there are other effective techniques to reduce the algorithm by a large amount of running time, but with a basic knowledge of the programming language this algorithm can be easily constructed and understood. This technology is mainly used in smaller applications.

The above is the detailed content of Python program to sort array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete