# Selection sort

## Implementations

 Related content:

### C

```void selectionSort(int numbers[], int array_size)
{
int i, j;
int min, temp;

for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}
```

Original source: [1]

### Java

```public  void selectionSort(int data[],int n)
// pre: 0<=n <= data.length
// post: values in data[0..n-1] in ascending order
{
int numUnsorted=n; // number of values not in order
int index;       // general index
int max;
int temp;
while (numUnsorted > 0)
{
//determine a maximum value in Array
max=0;
for (index=1; index < numUnsorted; index++)
if (data[max] < data[index])
max=index;
// swap data[max] and data[numUnsorted-1]
temp                = data[max];
data[max]           = data[numUnsorted-1];
data[numUnsorted-1] = temp;
numUnsorted--;
}
}
```

### Python

``` def selection_sort(list):
l=list[:]                  # create a copy of the list
sorted=[]                  # this new list will hold the results
while len(l):              # while there are elements to sort...
lowest=l[0]            # create a variable to identify lowest
for x in l:            # and check every item in the list...
if x<lowest:       # to see if it might be lower.
lowest=x
sorted.append(lowest)  # add the lowest one to the new list
l.remove(lowest)       # and delete it from the old one
return sorted
```

### Ruby

```def selectionsort(list) 0.upto(list.size-1) do |start| min = start start.upto(list.size-1) do |i| min = i if list[i] < list[min] end list[start], list[min] = list[min], list[start] end list end ```