Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

Python - Sort Arrays (Complete Guide for Beginners)

 In Python, arrays are used to store multiple values of the same data type. When working with data, you often need to sort elements in ascending or descending order for analysis, searching, or display purposes.

Python provides several ways to sort arrays such as using sort(), sorted(), and manual sorting techniques.

In this tutorial, you will learn all methods to sort arrays in Python with clear examples.


Creating an Array

We use Python’s array module.

import array as arr

numbers = arr.array('i', [40, 10, 50, 20, 30])
print(numbers)

1. Sort Array Using sort() Method

The sort() method sorts the array in ascending order.

Example

numbers = arr.array('i', [40, 10, 50, 20, 30])

numbers = arr.array('i', sorted(numbers))

print(numbers)

Output

array('i', [10, 20, 30, 40, 50])

Explanation

  • sorted() returns a sorted list
  • We convert it back to array

2. Sort Array in Descending Order

You can sort in reverse order using reverse=True.

Example

numbers = arr.array('i', [40, 10, 50, 20, 30])

numbers = arr.array('i', sorted(numbers, reverse=True))

print(numbers)

Output

array('i', [50, 40, 30, 20, 10])

3. Sorting Using Loop (Bubble Sort)

You can manually sort arrays using bubble sort.

Example

numbers = arr.array('i', [40, 10, 50, 20, 30])

for i in range(len(numbers)):
    for j in range(len(numbers) - i - 1):
        if numbers[j] > numbers[j + 1]:
            numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]

print(numbers)

Output

array('i', [10, 20, 30, 40, 50])

4. Sorting in Descending Order (Bubble Sort)

Example

numbers = arr.array('i', [40, 10, 50, 20, 30])

for i in range(len(numbers)):
    for j in range(len(numbers) - i - 1):
        if numbers[j] < numbers[j + 1]:
            numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]

print(numbers)

Output

array('i', [50, 40, 30, 20, 10])

5. Sort Array Using sorted() Function (Recommended)

The sorted() function is the easiest way.

Example

numbers = arr.array('i', [40, 10, 50, 20, 30])

sorted_array = arr.array('i', sorted(numbers))

print(sorted_array)

6. Sorting After Converting to List

Arrays can be converted to lists for sorting.

Example

numbers = arr.array('i', [40, 10, 50, 20, 30])

temp_list = list(numbers)
temp_list.sort()

numbers = arr.array('i', temp_list)

print(numbers)

Real-World Example: Student Marks Sorting

import array as arr

marks = arr.array('i', [85, 90, 78, 88, 92])

sorted_marks = arr.array('i', sorted(marks))

print("Sorted Marks:", sorted_marks)

Real-World Example: Product Prices Sorting

import array as arr

prices = arr.array('i', [500, 200, 800, 100, 300])

prices = arr.array('i', sorted(prices, reverse=True))

print("High to Low Prices:", prices)

Sorting Methods Comparison

MethodTypeDescription
sorted()Built-inEasy and recommended
bubble sortManualEducational use
list.sort()List-basedFast but not array method
reverse sortDescendingUses reverse=True

Common Mistakes

Mistake 1: Using sort() directly on array module

❌ Wrong:

numbers.sort()

✔ Arrays do not support .sort() directly


Mistake 2: Forgetting conversion

arr.array('i', sorted(numbers))

✔ Always convert properly


Mistake 3: Bubble sort inefficiency

✔ Works but slow for large data


Safe Sorting Example

numbers = arr.array('i', list(numbers))
numbers = arr.array('i', sorted(numbers))

Array Sorting Methods Summary

MethodUse
sorted()Best simple method
bubble sortLearning purpose
list.sort()List conversion method
reverse=TrueDescending order

Practice Exercise 1

Sort an array in ascending order.


Practice Exercise 2

Sort an array in descending order.


Conclusion

Sorting arrays in Python is an essential operation for organizing and analyzing data.

You learned:

  • How to sort arrays using built-in functions
  • Manual sorting using loops
  • Ascending and descending order sorting
  • Real-world applications

Mastering sorting techniques helps in data analysis, searching algorithms, and real-world programming tasks.




Post a Comment

0 Comments