Selection Sort Algorithm
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts;
- Sorted part at the left end and the unsorted part at the right end.
- Initially, the sorted part is empty and the unsorted part is the entire list.
- The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array.
- This process continues moving unsorted array boundary by one element to the right.
http://localhost:3000
- Java
class selectionSort{
public static void main(String [] args){
int i,j,mini_index,size,temp;
int a[] = {12,13,67,10,5,4}; // Declare a Variable
size = a.length; // Calculate Size of Array
for(i = 0; i<size; i++){
mini_index = i;
for(j = i+1; j<size; j++){
if(a[j] <a[mini_index]){
temp = a[j]; //Swapping Values
a[j] = a[mini_index];
a[mini_index] = temp;
}
}
}
for(i = 0; i < size; i++){
System.out.println(a[i]); // Print Selection Sorted Array
}
}
}