Selection-Sort 7
Selection-Sort 7
Topperworld.in
Selection Sort
The array is passed through (n-1) times and the smallest element
is placed in its respective position in the array as detailed below:
Pass 1: Find the location j of the smallest element in the array x [0], x[1],
. . . . x[n-1],
Pass 2: Leave the first element and find the location j of the smallest
element in the
sub-array x[1], x[2], . . . . x[n-1], and then interchange x[1] with x[j].
Then
Pass 3: Leave the first two elements and find the location j of the smallest
element in
the sub-array x[2], x[3], . . . . x[n-1], and then interchange x[2] with x[j].
Pass (n-1): Find the location j of the smaller of the elements x[n-2] and
x[n-1], and then interchange x[j] and x[n-2]. Then x[0], x[1], . . . . x[n-
2] are sorted. Of course, during this pass x[n-1] will be the biggest element
and so the entire array is sorted.
Subject Name
1 2 3 4 5 6 7 8 9 Remarks
Algorithm:
2. Iterate through the unsorted portion of the array (starting from the
second element), and find the smallest element.
3. Swap the smallest element found with the first element of the
unsorted portion.
4. Move the boundary between the sorted and unsorted portions one
position to the right.
Java Implementation:
System.out.println("Sorted array:");
for (int num : arr) {
Subject Name
➔Complexity Analysis:
Overall, while Selection Sort is easy to understand, it's not efficient for large
arrays due to its quadratic time complexity. More advanced sorting
algorithms like Merge Sort or Quick Sort are generally preferred for larger
datasets