void selectionsort code example
Example 1: selection sort in java
public static void SelectionSort(int[] arr)
{
int small;
for (int i = 0; i <arr.length - 1; i++)
{
small = i;
for (int j = i + 1; j < arr.length; j++)
{
if (arr[j] < arr[small])
{
small = j;
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
}
}
Example 2: selection sort
void selectionSort(int arr[], int n)
{
for(int i = 0; i < n-1; i++)
{
int min = i;
for(int j = i+1; j < n; j++)
{
if(arr[j] < arr[min])
min = j;
}
if(min != i)
{
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}