From 83c55b8e46222d408868f11d3e99113e505ad489 Mon Sep 17 00:00:00 2001 From: Sunny Pate Date: Sat, 13 Oct 2018 13:07:15 +1100 Subject: [PATCH 1/2] SelectionSort The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray which is unsorted. In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray. --- sorting/selectionSort.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 sorting/selectionSort.py diff --git a/sorting/selectionSort.py b/sorting/selectionSort.py new file mode 100644 index 0000000..78b4e36 --- /dev/null +++ b/sorting/selectionSort.py @@ -0,0 +1,21 @@ + +# coding: utf-8 + +# In[50]: + + +alist = [54,26,93,17,77,31,44,55,20] +def selectionSort(alist): + for fillslot in range(len(alist)-1,0,-1): + positionOfMax=0 + for location in range(1,fillslot+1): + if alist[location]>alist[positionOfMax]: + positionOfMax = location + + temp = alist[fillslot] + alist[fillslot] = alist[positionOfMax] + alist[positionOfMax] = temp + return alist + +print(shortBubbleSort(alist)) + From 10ebf884613524018eac709a9175f8ec74c5c130 Mon Sep 17 00:00:00 2001 From: Sunny Patel Date: Sat, 13 Oct 2018 13:19:27 +1100 Subject: [PATCH 2/2] UpdateselectionSort.py minor changes --- sorting/selectionSort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorting/selectionSort.py b/sorting/selectionSort.py index 78b4e36..2de5d91 100644 --- a/sorting/selectionSort.py +++ b/sorting/selectionSort.py @@ -17,5 +17,5 @@ def selectionSort(alist): alist[positionOfMax] = temp return alist -print(shortBubbleSort(alist)) +print(selectionSort(alist))