How many comparisons are needed in worst case if we have to sort 7 numbers each of 4 digit? - radix-sort

How many comparisons are needed in worst case if we have to sort 7 numbers each of 4 digit ?(Radix sort)
Options are- 40,38,47,280 .
My solution-I have taken 10 buckets( 0 to 9)(linked list) . Then for every number for ith digit I have put it into Bucket corresponding to its digit's value. Then I collected those numbers into array back. This process is repeated for all the digits and thus my original array got sorted. Total number of comparisons= 10*4=40 (10 because I iterated through all buckets to look for corresponding bucket).
Now the problem is in the book by Timothy J Williams its given no of comparisons= no of digits* no of numbers * no of buckets= 4*7*10=280. I am not able to comprehend up. Can someone please explain how this came.

It is true that radix sort is not a comparison based algorithm. Here comparisons account to the comparisons involved in iterations. First, we need to traverse the array of 7 elements and keep the digit of each number in the appropriate bucket. Here in order to traverse the array we need 7 comparisons. After placing all the elements in the buckets from 0-9 we need to traverse all the buckets from 0-9 and arrange the elements in order according to their bucket number, this takes 10 comparisons to traverse the bucket from 0-9. Now we have to repeat this process for all the 4 digits of 7 elements. So, the total number of comparisons would be 7*10*4 = 280

Related

algorithm interview: mth frequent element in n sorted arrays

There is an algorithm interview question:
We have n sorted arrays, how to find the m-th frequent element in the aggregated array of n arrays? Moreover, how to save space? Even compromise on some time complexity.
What I can think is that enumerate all the elements of n arrays and use a hashmap to record their frequency, then sort hashmap respect to the value (frequency). But then there is no difference with the one array case.
Walk over all arrays in parallel using n pointers
1 4 7 12 34
2 6 9 12 25
The walk would look like this
1 1 4 7 7 12 12 34
* 2 2 2 9 12 25 34
You do need a hash map in order to count the number of occurrences of elements in the cut. E.g. at the second step in the example, your cut contains 1 and 2.
Also, you need two min-heaps, one for every cut to be able to choose the array to advance along and another one to store m most repetitive elements.
The complexity would be expected O(#elements * (log(n) + log(m))). The space requirement is O(n + m).
But if you really need to save space you can consider all these n sorted arrays as one big unsorted, sort it with something like heapsort and choose the longest subarray of duplicates. This would require O(#elements * log(#elements)) time but only O(1) space.
You do an n-way merge, but instead of writing out the merged array, you just count the length of each run of duplicate values and remember the longest m in a min-heap.
This takes O(total_length * (log n + log m)) time, and O(n) space.
It's a combination of common SO questions. Search above on "merge k sorted lists" and "kth largest"

Stuck on complexity analysis of a tricky program

Really stuck the complexity analysis of this problem .
Given digits 0–9 , we need to find all the numbers of max length k whose digits will be in increasing order .
for example if k = 3 , numbers can be 0,00,000,01,02,03,04,.... 1,11,111,12,...
So the question basically that if repetitions allowed for digits,
How many such combinations are possible to find all the numbers less than size k (less than digit length k) such that digits from left to right will be non-decreasing order.
Numbers with at most k digits that are weakly increasing are in 1-1 correspondence with binary strings of length k+10, with exactly ten 1's. The number of consecutive 0s just before the ith one and one in the binary string is the number of i digits in the original number. For example, if k=7, then 001119 maps to 00100011111111010 (2 zeros, 3 ones, 0 twos, 0 threes, ..., 0 eights, 1 nine, 1 digit left over to make the number of digits up to 7).
These binary strings are easy to count: there's choose(k+10, 10)-1 of them (missing one because the empty number is disallowed). This can be computed in O(1) arithmetic operations (actually 10 additions, 18 multiplications and one division).
I don't have enough reputation neither, so I cannot answer Paul's or Globe's answer.
Globe's answer choose(k+9,9) is not perfect, because it only counts the solutions where the numbers have exactly k digits. But the original problems allows numbers with less digits too.
Paul's answer choose(k+10,10) counts these shorter numbers too, but it also allows numbers with zero digits. Let's say k=7 then the following binary string describes a number with no digits: 11111111110000000. We have to exclude this one.
So the solution is: choose(k+10,10)-1
I don't have enough reputation to comment on Paul's answer, so I'm adding another answer. The formula isn't choose(k+10, 10) as specified by Paul, it's choose(k+9, 9).
For instance if we have k=2, choose(2+10, 10) gives us 66, when there are only 55 numbers that satisfy the property.
We pick stars and separators, where the separators divide our digits into buckets from 0 to 9, and stars tell us how many digits to pick from a bucket. (E.g. **|**||*||||||* corresponding to 001139)
The reasoning behind it being k+9 and not k+10 is as follows:
we have to pick 9 separators between 10 digits, so while we have k choices for the stars, we only have 9 choices for the separators.

Which algorithm should be used to solve this sorting exercise?

I am trying to solve the following problem,but I am stuck. The problem is as follows-
Suppose you are given an array of integers.You have to remove certain elements from the array so that the entire array is sorted in ascending order. Elements can't be repeated, so if any element is repeated in the array, it also should be removed. You have to find the minimum number of integers that needs to be removed from the array in order to sort it in ascending order.
I am posting a few test cases so that the question becomes clear:
Input
1 1 2
Output-1(since 1 is repeated and if removed, the array is sorted)
2.Input - 1 8 9 3 4 5
Output-2(since 8,9 if removed the array is sorted and it is also the minimum number required)
Input- 1 7 8 9 3
Output - 1(only 3 should be removed)
My approach was to move through the array and if the previous number is bigger than the current number, then the previous number should be removed. By this approach, 1 and 2 will get solved, but case 3 will output 3 for this approach.
How should I solve this problem? Is there any specific algorithm that might be helpful?
Let's assume that we have thrown away some numbers so that the remaining array is sorted. What does the remaining numbers form in the initial array? An increasing subsequence. We want to throw away as few numbers as we can, or, put it another way, keep as many numbers as possible. Thus, we need to find the longest increasing subsequence in the given array.

minimum number of comparisons needed

what is the minimum number of comparisons needed to find the largest element from 4 distinct elements? I know for 5 distinct numbers it is 6, floor(5/2) * 3; this is from clrs book. but I know there is no one general formula for finding this, or is there?
edit clarification
these 4 elements could be in any different order(for all permutations of these 4 elements) im not interested in a counting technique to keep track of the largest element as you traverse the elements, but comparisons like > or <.
for 4 elements the min. number of comparisons is 3.
In general, to find largest of N elements you need N-1 comparisons. This gives you 4 for 5 numbers, not 6.
Proof:
there is always a solution with N-1 comparisons: just compare first two and then select the larger and compare with next one, select the larger and compare with next one etc....
there cannot be shorter solution because this solution would not compare all the elements.
QED.
I know it does not answer the original question, but I enjoyed reading this not-so-intuitive post on the minimum number of comparisons needed to find the smallest AND the largest number from an unsorted array (with proof).
Think of it as a competition. By comparing two elements you have a looser and a winner.
So if you have n elements and need 1 final winner you need n-1 comparisons to rule out the other ones.
for elements a,b,c,d
if a>b+c+d, then it only required one comparison to know that a is the biggest.
You do have to get lucky though.

Measuring how "out-of-order" an array is

Given an array of values, I want to find the total "score", where the score of each element is the number of elements with a smaller value that occur before it in the array.
e.g.
values: 4 1 3 2 5
scores: 0 0 1 1 4
total score: 6
An O(n^2) algorithm is trivial, but I suspect it may be possible to do it in O(nlgn), by sorting the array. Does anyone have any ideas how to do that, or if it's not possible?
Looks like what you are doing is essentially counting the number of pairs of elements that are in the incorrect relative order (i.e. number of inversions). This can be done in O(n*log(n)) by using the same idea as merge sort. As you merge, you just count the number of elements that are in the left list but should have been on the right list (and vice versa).
If the range of your numbers is small enough, the fastest algorithm I can think of is one that uses Fenwick Trees. Essentially just iterate through the list and query the Fenwick Tree for how many elements are before it, then insert the number into the tree. This will answer your question in O(nlogm), where n is the size of your list and m is your largest integer.
If you don't have a reasonable range on your integers (or you want to conserve space) MAK's solution is pretty damn elegant, so use that :)

Resources