From 257be99f0d5465f93b03c2a65c3c4bb115a4a477 Mon Sep 17 00:00:00 2001 From: Nikita pande <30722238+Nikitapande@users.noreply.github.com> Date: Sun, 31 Oct 2021 21:52:47 +0530 Subject: [PATCH] Update SelectionSort.py --- Sorting/SelectionSort.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/Sorting/SelectionSort.py b/Sorting/SelectionSort.py index ba1b5d37..379e66d8 100644 --- a/Sorting/SelectionSort.py +++ b/Sorting/SelectionSort.py @@ -5,23 +5,29 @@ # Average Time Complexity: O(n^2) # Best Time Complexity: O(n^2) -print("Number Sequence Before Selection Sort") -seq=[5,2,1,8,3,5,3] -print(seq) +# Python program for implementation of Selection +# Sort +import sys +A = [64, 25, 12, 22, 11] -def selectionSort(nums): - n=len(nums) - for i in range(n-1): - iMin=i - for j in range(i+1,n): - if(nums[iMin]>nums[j]): - iMin=j - temp=nums[i] - nums[i]=nums[iMin] - nums[iMin]=temp - return nums +# Traverse through all array elements +for i in range(len(A)): + + # Find the minimum element in remaining + # unsorted array + min_idx = i + for j in range(i+1, len(A)): + if A[min_idx] > A[j]: + min_idx = j + + # Swap the found minimum element with + # the first element + A[i], A[min_idx] = A[min_idx], A[i] + +# Driver code to test above +print ("Sorted array") +for i in range(len(A)): + print("%d" %A[i]), -print("Number Sequence After Selection Sort") -print(selectionSort(seq))