Expt-12 - Implementation of Selection Sort
Expt-12 - Implementation of Selection Sort
Expt-12 - Implementation of Selection Sort
Explanation:
Lets consider the following array as an example: arr[] = {64, 25, 12, 22, 11}
First pass:
● For the first position in the sorted array, the whole array is
traversed from index 0 to 4 sequentially. The first position
where 64 is stored presently, after traversing whole array it is clear
that 11 is the lowest value.
Second Pass:
● For the second position, where 25 is present, again traverse the
rest of the array in a sequential manner.
11 25 12 22 64
Third Pass:
● Now, for third place, where 25 is present again traverse the rest of
the array and find the third least value present in the array.
11 12 25 22 64
Fourth pass:
● Similarly, for fourth position traverse the rest of the array and find
the fourth least element in the array
● As 25 is the 4th lowest value hence, it will place at the fourth
position.
11 12 22 25 64
Fifth Pass:
● At last the largest value present in the array automatically get
placed at the last position in the array
● The resulted array is the sorted array.
Example:
Program Code:
import java.util.Arrays;
}
void printArr(int a[])
{ // printing the element in sorted manner
int i;
int n = a.length;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
{
int a[] = { 91, 49, 4, 19, 10, 21 };
select harsh = new select();
System.out.println("\n\n\t before sorting array elements are - ");
harsh.printArr(a);
harsh.selection(a);
System.out.println("\n\n\t after sorting array elements are - ");
harsh.printArr(a);
}
}
Output Screenshots:
correct input
RESULT: Implementation of Selection Sort Thus, the programs for the given problem statements has been
executed and the results are verified successfully.