c++ 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 (arr[j] < arr[small])
{
small = j;
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
}
}
Example 2: how to make a selection sort C++
void selectionSort(int array[], int size)
{
int minIndex, minValue;
for (int start = 0; start < (size - 1); start++)
{
minIndex = start;
minValue = array[start];
for (int index = start + 1; index < size; index++)
{
if (array[index] < minValue)
{
minValue = array[index];
minIndex = index;
}
}
swap(array[minIndex], array[start]);
}
}
Example 3: selection sort
def ssort(lst):
for i in range(len(lst)):
for j in range(i+1,len(lst)):
if lst[i]>lst[j]:lst[j],lst[i]=lst[i],lst[j]
return lst
if __name__=='__main__':
lst=[int(i) for i in input('Enter the Numbers: ').split()]
print(ssort(lst))