code for selection sort 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 current position is less than previous smallest
if (arr[j] < arr[small])
{
small = j;
//swap values
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
}
}
Example 2: selection sort python
def selection_sort(lst):
empty_lst = []
x = len(lst) - 1
while x>=0:
for i in range(len(lst)):
if lst[i] <= lst[0]:
lst[0],lst[i] = lst[i],lst[0]
g = lst.pop(0)
empty_lst.append(g)
x -= 1
return empty_lst
print(selection_sort([2,3,4,2,1,1,1,2]))