go to previous page   go to home page   go to next page

Answer:

Time ≅ K*N2

Say N = 50 thousand and Time is 3. Then

One million is 20*N. We need K*(20*N)2 = K*400*N2 = 400*K*N2 = 400*3 = 1200 seconds

In fact, on my computer it took 1250 seconds, about 20 minutes to sort the array.


Simple Sorts

When you consider all the amazing things a web bowser can do in a fraction of a second, it seems inexplicable that merely sorting a list of numbers should take so long. But this is how many sorting algorithms perform.

Selection sort is regarded as a simple sort because it is fairly simple, but does not perform well with large arrays. Simple sorts typically have running time proportional to the square of the size of the data. As seen in the answer, this rapidly becomes a problem. A rule of thumb is to use a simple sort only when there are less than 30 items to sort.

There are advanced sorting algorithms that perform much better than simple sorts. But they are not discussed here. This is a topic for a book (or a course) on algorithms. The Java class Arrays has a sort method which should be used rather than the simple sorts in these chapters. This is discussed in chapter 112.


QUESTION 13:

Maybe the problem is Java. Would a sorting algorithm run much faster if written in some other language?


go to previous page   go to home page   go to next page