This week's book giveaway is in the Agile and Other Processes forum. We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line! See this thread for details.
if __name__ == "__main__":
objSSSort = SSSort(20)
I am getting the following output:
I think there is problem with swapping.
Somebody please guide me how to correct the above program;
First a few comments:
* Why is arr a class variable instead of an instance variable? Having it as a class variable will cause conflicts when you have m,ultiple instances of SSSSort. It would be better to use an instance variable (initialized with self.arr =  in the __init__ method)
* A better name for generate1000_5digit_RandomNum would be generateN_5digit_RandomNum
* In generate1000_5digit_RandomNum, a simpler way to iterate through the array would be to use a simple for elt in arr loop
* The parenthesis in if (SSSort.arr[j] < SSSort.arr[min]): are unnecessary
* Using slightly longer variable names would make it clearer to understand if a variable contains an index or an element.
* You are doing a pretty good following PEP8 (The Python coding guidelines) except in a couple of places (selectionSort instead of selection_sort and objSSSort instead of obj_SSSort)
The problem lies with lines 3 and 6 of the selectionSort method:
At line 3, find_the_min_element returns the value of the minimum element from i. What you need instead is the index of the minimum element from i.
At line 6, min is overwritten but it not used anymore in the current iteration. This line can be deleted without changing the behaviour of your program.
In general the pattern for swapping elements i and j in an array is:
There is also a fence post error in find_the_min_element because the last element is never taken into consideration. This means that it will never stand a chance to be swapped.
Here is the version I ended up with:
Hope this helps.
posted 1 week ago
Thanks for providing me this explanation.
By now I have corrected my code but your comments are good.
I have not tried to execute your code but I have one question for line#25 of your code:
I actually realized my mistake. Argument of "range" starts from 0 and goes up to "n-1", so there is no need of explicitly doing (self.n-1), so I changed it to (self.n).
You might have better reasons for doing this.
Your help is appreciable.
God bless you.
posted 1 week ago
range(N) goes from 0 to N-1. A list of length N has indices between 0 and N-1.
The code works correctly with both my range(self.n - 1) and your range(self.n) at line #25.
When using range(self.n - 1), the loop in selection_sort will stop at idx = n-2 (ie the penultimate element) but find_the_min_element_index will go up to n-1 (ie the last element).
When using range(self.n), the loop in selection_sort will stop at idx = n-1 (ie the last element) and the loop in find_the_min_element_index will exit immediately. The end result will be correct but at the (negligible) cost of one unnecessary iteration in the main loop and on unnecessary call to find_the_min_element_index in selection_sort.
Hi, I tried to work on given solution it helpful to resolve and learn about python.
Selection Sort is not stable because it swaps non-adjacent elements. Given [2, 2, 1], the '2' values will not retain their initial order. Selection sort first swaps the (1, 0) to the front: And now the (4, 0) and (4, 1) are out of order from where they started in the sort.The selection sort algorithm sorts an array by repeatedly finding the minimum element . order) from the unsorted subarray is picked and moved to the sorted subarray.Python program for implementation of Selection. # Sort. import sys .You want to share more then share now I start to learn online from Cetpa Infotech.
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you: