PLEASE EXPLAIN IN DETAIL HOW THIS CODE WORKS
package selectionsortexample;
public class SelectionSortExample {
public static void selectionSort(int[] arr){
for (int i = 0; i < arr.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < arr.length; j++){
if (arr[j] < arr[index]){
index = j;//searching for lowest index
}
}
int smallerNumber = arr[index];
arr[index] = arr[i];
arr[i] = smallerNumber;
}
}
this method iterating through input array arr and searching the smallest element then placing this element to first position in array then looking for smallest element in the rest of array and placing to 2nd position and so on. In the whole this algoritm sorting array in ascending order
Comments
Leave a comment