Jump to content

Python: Selection Sort Program

Featured Replies

Hi,

I am trying to write a program for selection sort. I got an example program:

 

https://www.geeksforgeeks.org/selection-sort/

import random

class SSSort:
   arr = [] # class var access by className.arr
   def __init__(self, n):
      self.n = n

   def generate1000_5digit_RandomNum(self):
      for i in range(self.n):
         num = random.randint(10000, 99999)
         SSSort.arr.append(num)

      for j in range(self.n):
         print("random {0}".format(SSSort.arr[j]))

   def find_the_min_element(self, i):
      min = i
      for j in range(i+1, self.n-1):
         if (SSSort.arr[j] < SSSort.arr[min]):
            min = j

      return SSSort.arr[min]

   def selectionSort(self):
      for i in range(self.n-1):
         min = self.find_the_min_element(i)
         #swap min and ith element of array
         temp = min
         min = SSSort.arr[i]
         SSSort.arr[i] = temp
   
      for i in enumerate(SSSort.arr):
         print(i)


   def main(self):
      self.generate1000_5digit_RandomNum()
      self.selectionSort()

if __name__ == "__main__":
   objSSSort = SSSort(20)
   objSSSort.main()

The output of the program is:


 

Quote

 

random 98046
random 21192
random 50964
random 81034
random 87750
random 50619
random 32092
random 52373
random 70434
random 61962
random 67618
random 87543
random 91959
random 25470
random 13246
random 37841
random 87932
random 40889
random 64008
random 94949
(0, 13246)
(1, 13246)
(2, 13246)
(3, 13246)
(4, 13246)
(5, 13246)
(6, 13246)
(7, 13246)
(8, 13246)
(9, 13246)
(10, 13246)
(11, 13246)
(12, 13246)
(13, 13246)
(14, 13246)
(15, 37841)
(16, 40889)
(17, 40889)
(18, 64008)
(19, 94949)

 

 

 

 

 

 

I think I am facing problem in the swapping. Somebody please solve my problem.

 

Zulfi.

It looks like you are confusing the index of the minimum value and the actual value when you do the swap.

I think you need something more like:

temp = SSSort.arr[min]
SSSort.arr[min] = SSSort.arr[i]
SSSort.arr[i] = temp

(There may be other problems!)

2 hours ago, Strange said:

(There may be other problems!)

Found three:
The finder method returns the value of the min element, not the index:

find_the_min_element(self, i):
      ...
      return SSSort.arr[min]

Change that to "return min".

The indexes self.n-1should be self.n in two places, otherwise the last element is not sorted.

Complete code below with my and Strange's suggestions, look for "# Changed by Ghideon" and  "#Strange's fix" 

 

import random

class SSSort:
   arr = [] # class var access by className.arr
   def __init__(self, n):
      self.n = n

   def generate1000_5digit_RandomNum(self):
      for i in range(self.n):
         num = random.randint(10000, 99999)
         SSSort.arr.append(num)

      for j in range(self.n):
         print("random {0}".format(SSSort.arr[j]))

   def find_the_min_element(self, i):
      min = i
      for j in range(i+1, self.n):
         if (SSSort.arr[j] < SSSort.arr[min]):
            min = j
            
	  # Changed by Ghideon:
      return min

   def selectionSort(self):
      for i in range(self.n):
         min = self.find_the_min_element(i)
         #swap min and ith element of array
    
    	 #Strange's fix	
         temp = SSSort.arr[min]
         SSSort.arr[min] = SSSort.arr[i]
         SSSort.arr[i] = temp
   
      for i in enumerate(SSSort.arr):
         print(i)


   def main(self):
      self.generate1000_5digit_RandomNum()
      self.selectionSort()

if __name__ == "__main__":
   objSSSort = SSSort(20)
   objSSSort.main()

 

  • Author

Hi,

 

Thanks a lot for solving my problem.

 

God bless you.

 

Zulfi.

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.