📌 Information & Algorithm

Given Input:

Original array: [64, 25, 12, 22, 11]


Expected Output:

Sorted array: [11, 12, 22, 25, 64]

💻 Program Code

# Selection Sort in Python

def selection_sort(arr):
    # Traverse through all array elements
    for i in range(len(arr)):
        # Find the minimum element in the unsorted portion of the array
        min_idx = i
        for j in range(i+1, len(arr)):
            if arr[j] < arr[min_idx]:
                min_idx = j

        # Swap the found minimum element with the first element of the unsorted portion
        arr[i], arr[min_idx] = arr[min_idx], arr[i]

# Example usage
arr = [64, 25, 12, 22, 11]
print("Original array:", arr)

selection_sort(arr)

print("Sorted array:", arr)

                        

🖥 Program Output

Original array: [64, 25, 12, 22, 11]
Sorted array: [11, 12, 22, 25, 64]

                            

📘 Explanation

  • Outer Loop: Iterates over each element in the array.
  • Inner Loop: Finds the minimum element from the unsorted part of the array.
  • Swap: Swaps the minimum element with the first unsorted element.
  • Sorted Output: After the loops complete, the array is sorted in ascending order.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.