Part of the series

Several example codes

~2 min read • Updated Sep 17, 2025

Program Overview

This Python program reads the number of students from the user.
It then reads each student’s ID number and stores them in an array.
The array is sorted using the Selection Sort algorithm:
In each step, the smallest remaining element is found and swapped with the current position.
After sorting, the final result is printed.


Python Code:


# Read number of students
n = int(input("Enter the number of students: "))

# Read student ID numbers
student_ids = []
for i in range(n):
    sid = int(input(f"Enter student ID {i+1}: "))
    student_ids.append(sid)

# Selection Sort
for i in range(n):
    min_index = i
    for j in range(i + 1, n):
        if student_ids[j] < student_ids[min_index]:
            min_index = j
    # Swap current element with the smallest found
    student_ids[i], student_ids[min_index] = student_ids[min_index], student_ids[i]

# Print sorted result
print("Sorted student ID numbers:")
print(student_ids)

Sample Output:


Enter the number of students: 5  
Enter student ID 1: 98231  
Enter student ID 2: 97112  
Enter student ID 3: 99345  
Enter student ID 4: 96500  
Enter student ID 5: 97890  
Sorted student ID numbers:  
[96500, 97112, 97890, 98231, 99345]

Explanation:

Here’s how the program works:
- It reads n student IDs into a list
- It uses Selection Sort to find the smallest element and swap it forward
- The process continues until the entire list is sorted
- The final sorted list is printed


Written & researched by Dr. Shahin Siami