selection sort algorithm c++ code 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
procedure selection sort
list : array of items
n : size of list
for i = 1 to n - 1
min = i
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
if indexMin != i then
swap list[min] and list[i]
end if
end for
end procedure