Fastest sorting method for k numbers and N elements, k <<< N


Puckz

Problem: There are n balls, marked as 0, 1, 2, in random order, I want to sort from small to large. ball:

1, 2, 0, 1, 1, 2, 2, 0, 1, 2, ...

It must be solved in the fastest way. The sort() function cannot be used. I have thought of many ways, such as bubble sorting, insertion sorting, etc., but the speed is not fast. Is there an algorithm that makes the time complexity O(logn) or O(n)?

Given a list of balls A[] and length n

void sortBalls(int A[], int n)
{
 //code here
}
Selby

Given the very limited number of item types ( 0, 1and 2) , you just need to count the number of occurrences of each type. Then to print the "sorted" array, you repeatedly print the number of occurrences of each label. running time isO(N)

int balls[N] = {...};  // array of balls: initialized to whatever
int sorted_balls[N];   // sorted array of balls (to be set below)
int counts[3] = {};    //   count of each label, zero initialized array.

// enumerate over the input array and count each label's occurance
for (int i = 0; i < N; i++)
{
    counts[balls[i]]++;
}

// sort the items by just printing each label the number of times it was counted above
int k = 0;
for (int j = 0; j < 3; j++)
{
    for (int x = 0; x < counts[j]; x++)
    {
        cout << j << ", ";   // print
        sorted_balls[k] = j; // store into the final sorted array
        k++;
    }
}

Related


Fastest way to extract k distinct elements from n pairs in R

Gaël Giordano Suppose I have n distinct pairs of unordered elements. I want to extract the smallest pair with k distinct elements from n pairs. I know I can use duplicated()this to extract all distinct elements from n pairs, but I don't know how to use it to g

Decompose a number n into the sum of k distinct numbers

XZ6H I have a number n and I have to split it into k numbers so that all k numbers are distinct and the sum of the k numbers is equal to n, k max. For example, if n is 9, the answer should be 1,2,6. If n is 15, the answer should be 1,2,3,4,5. Here is what I ha

Decompose a number n into the sum of k distinct numbers

XZ6H I have a number n and I have to split it into k numbers so that all k numbers are distinct and the sum of the k numbers is equal to n, k max. For example, if n is 9, the answer should be 1,2,6. If n is 15, the answer should be 1,2,3,4,5. Here is what I ha

Decompose a number n into the sum of k distinct numbers

XZ6H I have a number n and I have to split it into k numbers so that all k numbers are distinct and the sum of the k numbers is equal to n, k max. For example, if n is 9, the answer should be 1,2,6. If n is 15, the answer should be 1,2,3,4,5. Here is what I ha

Decompose a number n into the sum of k distinct numbers

XZ6H I have a number n and I have to split it into k numbers so that all k numbers are distinct and the sum of the k numbers is equal to n, k max. For example, if n is 9, the answer should be 1,2,6. If n is 15, the answer should be 1,2,3,4,5. Here is what I ha

Decompose a number n into the sum of k distinct numbers

XZ6H I have a number n and I have to split it into k numbers so that all k numbers are distinct and the sum of the k numbers is equal to n, k max. For example, if n is 9, the answer should be 1,2,6. If n is 15, the answer should be 1,2,3,4,5. Here is what I ha

Decompose a number n into the sum of k distinct numbers

XZ6H I have a number n and I have to split it into k numbers so that all k numbers are distinct and the sum of the k numbers is equal to n, k max. For example, if n is 9, the answer should be 1,2,6. If n is 15, the answer should be 1,2,3,4,5. Here is what I ha

list a sorted list of n elements into k categories

Mach I have n events {v1,...,vn} which will happen at some specific time {t1,...,tk} where k <= n (multiple events can happen at the same time), I need columns out every way this happens. For example, if we have 2 events, we might have: {v1 <v2},{v2 <v1}(2次) {

select k from n elements of mapreduce

mental illness Assume that the xrecords ' inputs nhave the desired properties (e.g. their values are positive) and that all records xhave unique keys. kWhat I want to do is, using a map-only job in MapReduce, emit these records exactly .n For example, let's sa

How to combine n elements in k different ways

Furkan Maidan For example 12 balls and 3 boxes, how can I add a list to all combinations using all balls. For example n is 12 and k is 3: - (12,0,0) - (0,12,0) - (0,0,12) - (0,1,11) - (0,11,1) - (11,1,0) - (11,0,1) - (10,1,1)... Python, C#, Java... on

list a sorted list of n elements into k categories

Mach I have n events {v1,...,vn} which will happen at some specific time {t1,...,tk} where k <= n (multiple events can happen at the same time), I need columns out every way this happens. For example, if we have 2 events, we might have: {v1 <v2},{v2 <v1}(2次) {

Method to automatically generate code for sorting n elements

Siddharth Chabra According to this article on Wikipedia, the theoretical minimum for sorting a list of n numbers is:log (n!) I managed to write a rather "big" code for sorting a list of up to 5 elements. The sorting tree code for sorting a list of 8 elements i

Method to automatically generate code for sorting n elements

Siddharth Chabra According to this article on Wikipedia, the theoretical minimum for sorting a list of n numbers is:log (n!) I managed to write a rather "big" code for sorting a list of up to 5 elements. The sorting tree code for sorting a list of 8 elements i