diff --git a/Python/Sorting-Techniques/selectionSort.py b/Python/Sorting-Techniques/selectionSort.py index 57fe3ad6..f5182219 100644 --- a/Python/Sorting-Techniques/selectionSort.py +++ b/Python/Sorting-Techniques/selectionSort.py @@ -1,3 +1,26 @@ + +# program for selection sort +#selections sort takes O(n^2) time complexity + + + +#program starts from here +def selectionSort( itemsList ): + n = len( itemsList ) + for i in range( n - 1 ): + minValueIndex = i + + for j in range( i + 1, n ): + if itemsList[j] < itemsList[minValueIndex] : + minValueIndex = j + + if minValueIndex != i : + temp = itemsList[i] + itemsList[i] = itemsList[minValueIndex] + itemsList[minValueIndex] = temp + + return itemsList + # Selection sort in Python # time complexity O(n^2) @@ -21,3 +44,4 @@ def selectionSort(array, size): selectionSort(arr, size) print('The array after sorting in Ascending Order by selection sort is:') print(arr) +