Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to implement selection sort
- def selectionSort(myList):
- # loop i from 0 to n-1
- for i in range(0,len(myList)):
- # in each iteration we take i as index of the smallest element
- minIndex = i
- # loop j from i to n-1 (unsorted subarray from i to n-1)
- # compare jth element to minIndex element
- for j in range(i+1, len(myList)):
- if myList[minIndex] > myList[j]:
- minIndex = j
- # swap the smallest element to the minIndex position
- myList[i], myList[minIndex] = myList[minIndex], myList[i]
- # remove the following comment to view the sorted subarray
- # print(myList)
- return myList
- # main function
- print("Enter the array to be sorted : ")
- myList1 = list(map(int, input().strip().split(",")))
- selectionSort(myList1)
- print("The List after applying Selection Sort is : ")
- print(myList1)
Add Comment
Please, Sign In to add comment