Python: Program hangs up with large integer count

Hi,
I am trying to sort with one million integers but my program hangs up.
I am using following following command:

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

In the above code, I am trying to run my selection sort program with 1000000 integers
How to solve this problem in Python?
Is there any size limitation with integers in python?
Zulfi.

No Python is actually great with large numbers.
I think that’s the reason why you where giving this task.

I’m not sure what the rest of your code is or what it is what you want to do it excatly. Could you provide us with more information?

What do you mean by ‘hangs up’? Your performance can vary based on a large number of factors and I can’t say much without seeing your code.

Hi Kittykora & BobSmit,
I would provide you the code but please provide me practical running solution not just suggestions.

Zulfi.

I’m not going to write the code for you. I can tell you where there are bugs in your code, but me rewriting your code for you does nether of us any good.

Hi,
My program hangs in sorting loop. I thought its a integer size limit but other post says that Python is good at large numbers. It works fine for random values but random values generation (in my program) actually don’t use large numbers (i.e. seed). Even the sorting is not using large numbers, so it might be program logic but also a memory problem which python is not complaining.
Any way somebody please provide me a practical solution.
Program works fine with small values.

``````import random
import psutil
import os

class SSSort:
arr = []  # class var access by className.arr

def __init__(self, n):
self.n = n

def generate1000000_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
return min
#print("min {}".format(min))

def selectionSort(self):
for i in range(self.n):
min = self.find_the_min_element(i)
#swap min and ith element of array
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.generate1000000_5digit_RandomNum()
self.selectionSort()

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

Output: shows the random values but hangs up in the sorting loop

Hi Bob,
Thanks for your response. Actually this is not my hw. I had to do upto 1000 values. This is just I did on a suggestion on another post. It works fine with 10000 values. But it hangs up with n=1000000. I did not try intermediate values. I don’t need solution in using another algorithm. I want to solve my python error in the above program.
Zulfi.

The problem is your algorithm. You need a faster algorithm. With this approach, you are doing n*(n-1) + (n-1)*(n-2) + … comparisons. In order to compare 100x as many values, it will take 10000x as long.

You probably want a merge sort or a quick sort.

Hi,

You mean it would work after 10 minutes?

Zulfi.

if it takes 1 minute to sort 1000 values, then it would take 100 minutes to sort 10000 values. This is not good scaling. You really do want a better algorithm.

Sorry this is not my interest. I am interested in learning python. Tell me why it hangs up? If you thing this is not a hang up problem why the computer is not going beyond the generation of random numbers. You can’t go beyond the scope of question. So please don’t suggest merge sort/quick sort. Tell me how to correct the hang up problem or accept that it is the inherent issue of python.
Zulfi.

You cannot make the algorithm go faster. If you increase the number of items being sorted by a factor of 10, then the algorithm will take 100x longer. If you increase the number of items being sorted by a factor of 100, then the algorithm will take 10,000x longer. If you increase the number of items being sorted by a factor of 1,000, then the algorithm will take 1,000,000x longer. The algorithm is going as fast as it can go.

If you want to go faster, then you have to replace your sorting with a better algorithm. If you want to keep this sorting algorithm, then you cannot go faster.

Choosing the correct way to solve a problem for your given data and problem size is part of learning code.

There is nothing wrong with Python. Python is correctly executing the selection sort. Selection sort is just painfully slow for large lists. This is the case for any language.