- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Sort an Array
In general, there are multiple ways to sort an array. The array can be sorted either in ascending order or in descending order. The array sorted in ascending order will have the smaller elements first and then the larger elements. The array sorted in descending order will have the larger elements first and then the smaller elements.
The array can be generally sorted by using basic characteristics of Python language. In order to do so, there are two loops that are used.
Input Output Scenario
Consider an array arr = [ 5, 9, 1, 10, 3 ].
Now, let us sort the array by comparing one element to another.
The first element is compared to the rest of the elements initially.
The same process repeats until the last element such that the entire array is organized or sorted.
The sorted array of “ arr ” in ascending order is sorted_array = [ 1, 3, 5, 9, 10 ].
Algorithm
Step 1 − Initially, an array must be created or declared with some elements. Note that all the elements present in the array should belong to the similar data type.
Step 2 − In order to sort the elements, the first step followed is comparing the elements. One element is compared to the rest of the elements. If the element is smaller than the other elements, then it is placed in the first position of the array ( when the sorted array is required in ascending order ). So, to proceed with comparison, two loops are required. First loop is used to select an element from the array accordingly.
Step 3 − The second loop or the inner loop is used to compare the element that is selected in the first loop with the other elements. The elements’ selection is done by incrementing the index number. This step is one of the most important steps throughout the process of sorting.
Step 4 − After comparing the element, check whether the element selected in the outer loop is less than the element selected in the inner loop. If the element is less than the other element, then swap the values of the elements using a “ temp ” variable. The “ temp ” variable is used as a bridge in order to store the values of other elements and swap the values easily. ( This is mainly meant for sorting an array in ascending order ).
Step 5 − By incrementing the index values in the loops ( both inner loop and outer loop ), continue and repeat the entire process until all the elements are compared and sorted. After the entire process is completed, the resulting array is the required sorted array.
Example
In the following example, we are going to learn about the process of sorting an array by placing the 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 for 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
Conclusion
In this way, the procedure for sorting an array either in ascending order or in descending order works by using basic characteristics of Python such as loops, conditional statements, etc. This is not considered as an efficient technique as there are other efficient algorithms that decrease lots of running time, but with the basic knowledge on the programming language, this algorithm can be constructed and understood easily. This technique is mostly used in smaller applications.