Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest strategy to form and sort an array of positive integers

In Java, what is faster: to create, fill in and then sort an array of ints like below

int[] a = int[1000];
for (int i = 0; i < a.length; i++) {
    // not sure about the syntax
    a[i] = Maths.rand(1, 500) // generate some random positive number less than 500
}
a.sort(); // (which algorithm is best?)

or insert-sort on the fly

int[] a = int[1000];
for (int i = 0; i < a.length; i++) {
    // not sure about the syntax
    int v = Maths.rand(1, 500) // generate some random positive number less than 500
    int p = findPosition(a, v); // where to insert
    if (a[p] == 0) a[p] = v;
    else {
        shift a by 1 to the right
        a[p] = v;
    }
}
like image 478
Sophie Sperner Avatar asked Aug 21 '12 22:08

Sophie Sperner


People also ask

What is the fastest way to sort an array?

But since it has the upper hand in the average cases for most inputs, Quicksort is generally considered the “fastest” sorting algorithm.

Which sorting algorithm is best for integers?

Integer sorting algorithms including pigeonhole sort, counting sort, and radix sort are widely used and practical.

How do you sort only positive numbers in an array?

Using brute force approach to sort only positive numbers in the array. We will first filter out the positive numbers from the given array and sort them in required order. After that we will use a temp array to store the sorted array and extra variable to track the elements of the filtered and sorted positive array.

What is the best way to sort an array in Java?

Using the sort() Method In Java, Arrays is the class defined in the java.util package that provides sort() method to sort an array in ascending order. It uses Dual-Pivot Quicksort algorithm for sorting. Its complexity is O(n log(n)).


1 Answers

There are many ways that you could do this:

  1. Build the array and sort as you go. This is likely to be very slow, since the time required to move array elements over to make space for the new element will almost certainly dominate the sorting time. You should expect this to take at best Ω(n2) time, where n is the number of elements that you want to put into the array, regardless of the algorithm used. Doing insertion sort on-the-fly will take expected O(n2) time here.

  2. Build the array unsorted, then sort it. This is probably going to be extremely fast if you use a good sorting algorithm, such as quicksort or radix sort. You should expect this to take O(n log n) time (for quicksort) or O(n lg U) time (for radix sort), where n is the number of values and U is the largest value.

  3. Add the numbers incrementally to a priority queue, then dequeue all elements from the priority queue. Depending on how you implement the priority queue, this could be very fast. For example, using a binary heap here would cause this process to take O(n log n) time, and using a van Emde Boas tree would take O(n lg lg U) time, where U is the largest number that you are storing. That said, the constant factors here would likely make this approach slower than just sorting the values.

Hope this helps!

like image 145
templatetypedef Avatar answered Sep 21 '22 06:09

templatetypedef