From 890a7fe2313c8208a8f768d3444f90dedc1017ac Mon Sep 17 00:00:00 2001 From: hasan356 Date: Sat, 27 Oct 2018 13:41:40 +0530 Subject: [PATCH] Added selection sort --- README.md | 2 ++ sorting/bubble.pyc | Bin 433 -> 0 bytes sorting/heap.pyc | Bin 772 -> 0 bytes sorting/selection.py | 16 ++++++++++++++++ tests.py | 9 +++++++++ 5 files changed, 27 insertions(+) delete mode 100644 sorting/bubble.pyc delete mode 100644 sorting/heap.pyc create mode 100644 sorting/selection.py diff --git a/README.md b/README.md index cda093b..0e605c1 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,8 @@ Go ahead and add a one line intro about you and add your favorite emoji (you can - Hi, my name is Brandon and my favorite emoji is 🍔 +- Hi, my name is Hasan and my favorite emoji is 😅 + ## Conclusion Thank you for the overwhelming amount of contributions! I hope that everybody made their 4 pull requests for Hacktoberfest, and that your journey to open source doesn't end here. I am *slowly* getting through the pull requests. Check back if you don't see your changes in this repo! Best of luck :) diff --git a/sorting/bubble.pyc b/sorting/bubble.pyc deleted file mode 100644 index 449664ea79de658d542b19fcb756a7675c803556..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 433 zcmaJ-yH3ME5S%-khaw{JD2N+sS5T(05E67qDM%54M1f-~GT2VyJ4->>RjBzyz5yve z!0b5)Dy-Yt-I=|;k-bg7_xZKCpJMwd<|~Hj6B6JZcwnJ04mFvn=sS#E#x=uy5G>FD zYlT)}^#-fZC~0pV3$z-mL@4ec+h`0D##&RoMeE7U#zW90N~gh#k81eCxBj+rQa2Sf zf5~V$1kYp{U_yNydbBjk2*|{)Gj4^<)6$Ch>ZzQip;UH{P}W79R)!u~JIowDTVF=f zVi7XKfjMGj=9SIL*+pE(ah{$pUakC&u9GF^kN|a{Pt=(j=$<-OfgXh--N}Q$>;Buh Q&&jBmCv~1)x~=T-4f>Ku3jhEB diff --git a/sorting/heap.pyc b/sorting/heap.pyc deleted file mode 100644 index d4b7f7782c3545ee1e1dbf7fe867a18be6596620..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 772 zcmaJBb5S`gQR19K+AO+F#VxfgCF(%rP5OWkn6AJ<-UcdwI)u|&?Ax7vZ_h5aHecQsmt9&ujPVqamH|DYC!#APtO--kP|LTN>I$qO zJj59yeFIDl&8)DIl)$abyst0DSB|D0O_>HnNrR@kpeln$)n(0-9b%Ng&4^BkmXw&w zh^XaOL@V6}mXg<>L_<}RHK@yAsqQe~#o;W?EisxvU?!&-85}G6Qj3Q}M-UZ^?0F4N4AGb@coHQw)0`?0Yi8`|yZ z_2`0QSpY{+Qfq%ZhH8Den-PjQc7H)_ENJ<1>CNZ^k6IStlk~Sxn}%WRt<8 diff --git a/sorting/selection.py b/sorting/selection.py new file mode 100644 index 0000000..359e04a --- /dev/null +++ b/sorting/selection.py @@ -0,0 +1,16 @@ +def selection(numbers): + + # Traverse through the array + for i in range(len(numbers)): + + # Find the index of the minimum element in remaining sorted array + min_index = i + for j in range(i+1, len(numbers)): + if numbers[min_index] > numbers[j]: + min_index = j + + # Swap the found minimum element with this index + numbers[i], numbers[min_index] = numbers[min_index], numbers[i] + + #return the sorted array + return numbers \ No newline at end of file diff --git a/tests.py b/tests.py index 270c350..c1d5389 100644 --- a/tests.py +++ b/tests.py @@ -55,3 +55,12 @@ print "Bucket Sort incorrect" except: print "Bucketsort function errored or is incomplete" + +try: + from selection import selection + if(selection(list(nums)) == sortedNums): + print "selectionsort success!" + else: + print "selectionsort incorrect." +except: + print "selectionsort function errored or is incomplete." \ No newline at end of file