Search code examples
sortingtime-complexitycomplexity-theoryspace-complexity

What is the time complexity of this sorting problem?


The question was given an array of n size with values ranging from [1-100]. Create a sorting algorithm and discuss it's time, space, and optimality. I don't have a decent understanding of asymptotic analysis and wasn't sure how to answer this.

Algorithm:

void sort(int a[], int n) {
    int temp[100] = {0};
    for(int i=0; i<n; i++)
        temp[a[i]-1]++;
    int c = 0;
    for(int i=0; i<100; i++)
        for(int j=0; j<temp[i]; j++) {
            a[c] = i+1;
            c++;
        }

  }

Solution

  • It's O(n), please refer Count Sort