Question

In: Computer Science

Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . ,...

Present an O(n) algorithm that sorts n positive integer numbers a1, a2, . . . , an which are known to be bounded by n 2 (so ai ≤ n 2 , for every i = 1, . . . , n. Use the idea of Radix Sort (discussed in class and presented in Section 8.3 in the textbook). Illustrate your algorithm by showing on paper similar to Fig. 8.3, page 198 in the textbook (make sure you indicate clearly the columns) how the algorithm sorts the following sequence of 12 positive integers: 45, 98, 3, 82, 132, 71, 72, 143, 91, 28, 7, 45. In this example n = 12, because there are 12 positive numbers in the sequence bounded by 144 = 12^2 .

Note that in order to obtain O(n) you have to adapt the Radix Sort algorithm (explain what are the “digits”, how can the “digits” of each number be calculated, and so on. Note that you cannot use the base 10 representation, because n^2 requires log10 n^2 digits, which is obviously not constant and therefore you would not obtain an O(n)-time algorithm.).

Solutions

Expert Solution

The time complexity of radix sort is, O(d*(n+b)) where,

d is the number of digits,

n is the number of integers,

b is the base example 10 for decimal numbers,

The total number of digit for n^2 integer will be, log10 n^2 and for base b. It will be logb n^2

Hence the total time complexity for using radiz sort for n integer (where number of integers are less than n^2) is, O((n+b)*O(logb(n^2)) ie O((n+b)*O(logb(n))

If we set the base b to n, then the number of digits become, logn n^2 ie 2 so the total time complexity becomes, O((n + n) * O(2)) ie O(n)

Hence, if we change the base b to n then the time complexity of radix sort becomes linear.

Let's consider the example given in the question, for the following12 integers: 45, 98, 3, 82, 132, 71, 72, 143, 91, 28, 7, 45

Let's change the base to 12, the integers becomes: 39, 82, 03, 6a, b0, 5b, 60, bb, 77, 24, 07, 39.

Now let's stably sort them on the basis of the least significant digit; after first iteration we get:

b0, 60, 82, 03, 24, 77, 07, 39, 39, 6a, 5b, bb

Stably sorting them based on the next significant digit, we get:

03, 07, 24, 39, 39, 5b, 60, 6a, 77, 82, b0, bb

Let's convert the numbers back to base 10:

03, 07, 28, 45, 45, 71, 72, 82, 91, 98, 132, 143

The above example illustrates the proposed algorithm to sort positive integers, n in number such that they are less than n^2 in linear time using radix sort.


Related Solutions

Let a1 ≥ a2, . . . , an be a sequence of positive integers whose...
Let a1 ≥ a2, . . . , an be a sequence of positive integers whose sum is 2n − 2. Prove that there exists a tree T on n vertices whose vertices have degrees a1, a2, . . . , an. Sketch of solution: Prove that there exist i and j such that ai = 1 and aj ≥ 2. Remove ai, subtract 1 from aj and induct on n.
Prove: If a1 = b1 mod n and a2 = b2 mod n then (1) a1...
Prove: If a1 = b1 mod n and a2 = b2 mod n then (1) a1 + a2 = b1 + b2 mod n, (2) a1 − a2 = b1 − b2 mod n, and (3) a1a2 = b1b2 mod n.
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns,...
Write, in Python, a recursive algorithm that takes, as input, a positive integer n, and returns, as output, the sum of the first n positive odd integers. Your solution should be recursive, and it should not contain any "for" loops or "while" loops.
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces...
Consider the following algorithm, which takes as input a sequence of ?n integers ?1,?2,…,??a1,a2,…,an and produces as output a matrix ?={???}M={mij} where ???mij is the minim term in the sequence of integers ??,??+1,…,??ai,ai+1,…,aj for ?≥?j≥i and ???=0mij=0 otherwise. for i := 1 to n for j := 1+1 to n for k:= i+1 to j m[i][j] := min(m[i][j], a[k]) end for end for end for return m a.) Show that this algorithm uses ?(?3)O(n3) comparisons to compute the matrix M....
The Lucas numbers are very similar to the Fibonacci numbers and are defined by a1=2, a2=1,...
The Lucas numbers are very similar to the Fibonacci numbers and are defined by a1=2, a2=1, and an+2=an+1+an. So the first five are 2, 1, 3, 4, 7 and it continues in that fashion. Give the next 4 Lucas numbers
2. Write the hexadecimal numbers in the registers of $a0, $a1, $a2, $a3 after the following...
2. Write the hexadecimal numbers in the registers of $a0, $a1, $a2, $a3 after the following codes running: ori $a0, $0, 11 ori $a1, $0, 19 addi $a1, $a1, -7 slt $t2, $a1, $a0 beq $t2, $0, label addi $a2, $a1, 0 sub $a3, $a1,$a0 j end_1 label: ori $a2, $a0, 0 add $a3, $a1, $a0 end_1: xor $t2, $a1, $a0 *Values in $a0, $a1, $a2, $a3 after the above instructions are executed.
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
Let n be a positive integer. Prove that if n is composite, then n has a...
Let n be a positive integer. Prove that if n is composite, then n has a prime factor less than or equal to sqrt(n) . (Hint: first show that n has a factor less than or equal to sqrt(n) )
Prove or disprove that 3|(n 3 − n) for every positive integer n.
Prove or disprove that 3|(n 3 − n) for every positive integer n.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT