Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 14 additions & 32 deletions SortingAlgos/selectionSort.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
#include<iostream>

using namespace std;

int main()
{
int array[10]={10,30,20,100,80,70,40,60,50,90};
int index,swapVariable; // Index stores the index of lowest value of that round
int round,j;
int arraySize=(sizeof(array)/sizeof(array[0]));

for(round = 0; round < arraySize; round++)
{
// This loop starts from its index and goes till the end of the loop
index=round;
for(j=round+1; j < arraySize; j++)
// After the round index it searches for the index of the smallest element
{
if(array[j]<array[index])
index=j;
}
// This swaps the smallest value at index with the round index
swapVariable = array[round];
array[round] = array[index];
array[index] = swapVariable;
}

cout<<"Sorted Array is: "<<endl;
for(int t = 0; t < arraySize; t++)
cout<<array[t]<<" ";
return 0;
}
def selection_sort(alist):
for i in range(0, len(alist) - 1):
smallest = i
for j in range(i + 1, len(alist)):
if alist[j] < alist[smallest]:
smallest = j
alist[i], alist[smallest] = alist[smallest], alist[i]


alist = input('Enter the list of numbers: ').split()
alist = [int(x) for x in alist]
selection_sort(alist)
print('Sorted list: ', end='')
print(alist)