Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort n^2 numbers only with the function that can sort n numbers

There is a function F() that can sort any n numbers, now there are n^2 numbers need to sort, how many times of calling F() you need at least ? (you can only call F() ). I thought out a method like bubble sort, about O(n^2) times calling. Have any better way?

like image 674
newSolar Avatar asked Jun 30 '15 09:06

newSolar


People also ask

Which sorting technique is given in options having n number of elements make n 1?

n numbers in range from 0 to n2 – 1? The idea is to use Radix Sort.

What is DNF sort?

For DNF (Dutch National Flag), we sort an array of 0, 1, and 2's in linear time that does not consume any extra space. We have to keep in mind that this algorithm can be implemented only on an array that has three unique elements.

How do you sort an array with two elements?

Step 1 : Here we can take two pointers type0 (for element 0) starting from beginning (index = 0) and type1 (for element 1) starting from end index. Step 2: We intend to put 1 to the right side of the array. Once we have done this then 0 will definitely towards left side of array to achieve this we do following.

How to sort numbers in a range from 1 to N2?

So to sort numbers in a range from 1 to n 2, we can use following process. 1) Subtract all numbers by 1. 2) Since the range is now 0 to n 2, do counting sort twice as done in the above implementation. 3) After the elements are sorted, add 1 to all numbers to obtain the original numbers.

What is the time to sort n numbers in linear time?

Sort n numbers in range from 0 to n^2 – 1 in linear time - Searching and sorting - If we use Counting Sort, it would take O(n^2) time as the given range is of size n^2. Using any comparison based sorting like Merge Sort, Heap Sort, .. etc would take O(nLogn) time.

How do you sort numbers in an array of numbers?

Consider sorting n n numbers stored in array A A by first finding the smallest element of A A and exchanging it with the element in A [1] A[1]. Then find the second smallest element of A A, and exchange it with A [2] A[2].

How to do counting sort for second digit in base n?

# Do counting sort for second digit in base n. # exp (n^1 = n) is passed. // in range from 0 to n^2 – 1. // digit in base n.


1 Answers

You'll need n(2n-1) steps (worst case). Here is an intuitive explanation:

Suppose there are 4 sorted groups of size (n/2) each. Let's call them A,B,C,D. Also suppose that each of these groups is sorted, that the initial input vector is DCBA and that the final sorted vector should be ABCD.

In each single operation we can change the order of 2 groups (e.g. change BA to AB).

sorting DCBA requires the following steps:

DCBA --> CDAB (2 steps) --> CADB (1 step) --> ACBD (2 steps) --> ABCD (1 step)

Total steps: 6 = 4*3/2

Now support that you need to sort FEDCBA:

FEDCBA --> EFCDAB (3 steps) --> ECFADB (2 steps) --> CEAFBD (3 steps) --> CAEBFD (2 steps) --> ACBEDF (3 steps) --> ABCDEF (2 steps)

Total steps: 15 = 6*5/2

And so on....

To sort x blocks of size (n/2) each you'll need x(x-1)/2 steps (each step sorts n consecutive elements).

n² elements are 2n * (n/2) blocks, so you'll need (2n)(2n-1)/2 = n(2n-1) steps.


Edit:

What if a single n-sorter (F) can sort arbitrary elements (not necessarily consecutive)?

This turns out to a research-level problem related to sorting networks. See also here.

Take a look at this recent paper by Shi, Yan, and Wagh:

In this work, we propose an n-way merging algorithm, which generalizes the odd-even merge by using n-sorters as basic building blocks, where n (≥ 2) is prime. Based on this merging algorithm, we also propose a sorting algorithm. For N = n^p input values, p + ⌈n/2⌉ × p(p−1)/2 stages are needed. The complexity of the sorting network is evaluated by the total number of n-sorters. The closed form expression for the number of sorters is also derived.

like image 99
Lior Kogan Avatar answered Nov 15 '22 06:11

Lior Kogan