📖 Scenario: You have a list of numbers that are not in order. You want to arrange them from smallest to largest, like sorting books by their height on a shelf.
🎯 Goal: Build a program that uses the Selection Sort Algorithm to sort a list of numbers in ascending order.
📋 What You'll Learn
Create an integer array called numbers with the exact values: 64, 25, 12, 22, 11
Create an integer variable called n that stores the length of the numbers array
Use nested for loops with variables i and j to implement the selection sort logic
Swap elements in the array to sort it in ascending order
Print the sorted array elements separated by spaces
💡 Why This Matters
🌍 Real World
Sorting is used in many real-life situations like organizing files, arranging products by price, or sorting names alphabetically.
💼 Career
Understanding sorting algorithms like selection sort is fundamental for software development and technical interviews.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called numbers with these exact values: 64, 25, 12, 22, 11
DSA Java
Hint
Use int[] numbers = {64, 25, 12, 22, 11}; to create the array.
2
Add array length variable
Create an integer variable called n that stores the length of the numbers array
DSA Java
Hint
Use int n = numbers.length; to get the array size.
3
Implement selection sort logic
Use nested for loops with variables i and j to find the minimum element and swap it with the element at index i in the numbers array
DSA Java
Hint
Use two loops: outer loop with i, inner loop with j. Find the smallest element and swap it with numbers[i].
4
Print the sorted array
Use a for loop with variable i to print each element of the numbers array separated by spaces
DSA Java
Hint
Use System.out.print(numbers[i] + " "); inside a loop to print all elements.
Practice
(1/5)
1. What is the main idea behind the Selection Sort algorithm?
easy
A. Repeatedly find the smallest element and move it to the front
B. Divide the list into halves and sort each half recursively
C. Swap adjacent elements if they are in wrong order repeatedly
D. Use a heap data structure to sort elements efficiently
Solution
Step 1: Understand Selection Sort process
Selection Sort works by finding the smallest element in the unsorted part and swapping it with the first unsorted element.
Step 2: Compare with other sorting methods
Unlike divide and conquer or adjacent swaps, Selection Sort directly selects the minimum each time.
Final Answer:
Repeatedly find the smallest element and move it to the front -> Option A
Quick Check:
Selection Sort = Find min and swap [OK]
Hint: Selection Sort picks smallest element each pass [OK]
Common Mistakes:
Confusing Selection Sort with Bubble Sort
Thinking it divides the list recursively
Assuming it uses extra data structures
2. Which of the following is the correct syntax to swap two elements arr[i] and arr[minIndex] in Java during Selection Sort?
easy
A. arr[i] = arr[minIndex]; arr[minIndex] = arr[i];
B. swap(arr[i], arr[minIndex]);
C. temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp;
D. arr[i], arr[minIndex] = arr[minIndex], arr[i];
Solution
Step 1: Understand Java swap syntax
Java does not have a built-in swap function; we use a temporary variable to swap values.
Step 2: Check each option
temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; correctly uses a temporary variable. arr[i] = arr[minIndex]; arr[minIndex] = arr[i]; overwrites arr[i] before saving it. swap(arr[i], arr[minIndex]); is invalid syntax. arr[i], arr[minIndex] = arr[minIndex], arr[i]; is Python style, not Java.