Suppose we are given a sequence S of n elements each of whic
Solution
 First Alalyze all sorting algorithm:
1. Counting Sort: it would take O(n^2) time as the given range is of size n^2.
 2. Using any comparison based sorting like Merge Sort, Heap Sort, .. etc would take O(nLogn) time.
 The idea is to use Radix Sort. Following is standard Radix Sort algorithm.
1) Do following for each digit i where i varies from least significant digit to the most significant digit.
    a) Sort input array using counting sort (or any stable sort) according to the i’th digit
Let there be d digits in input integers. Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers.
For example, for decimal system, b is 10. Since n^2-1 is the maximum possible value, the value of d would be O(logb(n)). So overall time complexity is O((n+b)*O(logb(n)). Which looks more than the time complexity of comparison based sorting algorithms for a large k. The idea is to change base b. If we set b as n, the value of O(logb(n)) becomes O(1) and overall time complexity becomes O(n).
arr[] = {0, 10, 13, 12, 7}
Let us consider the elements in base 5. For example 13 in base 5 is 23, and 7 in base 5 is 12.
 arr[] = {00(0), 20(10), 23(13), 22(12), 12(7)}
After first iteration (Sorting according to the last digit in base 5), we get.
 arr[] = {00(0), 20(10), 12(7), 22(12), 23(13)}
After second iteration, we get
 arr[] = {00(0), 12(7), 20(10), 22(12), 23(13)}
How to sort if range is from 1 to n2?
 If range is from 1 to n n^2, the above process can not be directly applied, it must be changed. Consider n = 100 and range from 1 to 10000. Since the base is 100, a digit must be from 0 to 99 and there should be 2 digits in the numbers. But the number 10000 has more than 2 digits. So to sort numbers in a range from 1 to n2, 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.
 // A function to do counting sort of arr[] according to
 // the digit represented by exp.
 void countSort(int arr[], int n, int exp)
 {
 int output[] = new int[n]; // output array
 int i, count[] = new int[n] ;
 for (i=0; i < n; i++)
 count[i] = 0;
 
 // Store count of occurrences in count[]
 for (i = 0; i < n; i++)
 count[ (arr[i]/exp)%n ]++;
 
 // Change count[i] so that count[i] now contains actual
 // position of this digit in output[]
 for (i = 1; i < n; i++)
 count[i] += count[i - 1];
 
 // Build the output array
 for (i = n - 1; i >= 0; i--)
 {
 output[count[ (arr[i]/exp)%n] - 1] = arr[i];
 count[(arr[i]/exp)%n]--;
 }
 
 // Copy the output array to arr[], so that arr[] now
 // contains sorted numbers according to curent digit
 for (i = 0; i < n; i++)
 arr[i] = output[i];
 }
 
 
 // The main function to that sorts arr[] of size n using Radix Sort
 void sort(int arr[], int n)
 {
 // Do counting sort for first digit in base n. Note that
 // instead of passing digit number, exp (n^0 = 0) is passed.
 countSort(arr, n, 1);
 
 // Do counting sort for second digit in base n. Note that
 // instead of passing digit number, exp (n^1 = n) is passed.
 countSort(arr, n, n);
 }
![Suppose we are given a sequence S of n elements, each of which is an integer in the range [0; n^2 - 1]. Describe a simple method for sorting S in O (n) time.So  Suppose we are given a sequence S of n elements, each of which is an integer in the range [0; n^2 - 1]. Describe a simple method for sorting S in O (n) time.So](/WebImages/46/suppose-we-are-given-a-sequence-s-of-n-elements-each-of-whic-1147082-1761616776-0.webp)
![Suppose we are given a sequence S of n elements, each of which is an integer in the range [0; n^2 - 1]. Describe a simple method for sorting S in O (n) time.So  Suppose we are given a sequence S of n elements, each of which is an integer in the range [0; n^2 - 1]. Describe a simple method for sorting S in O (n) time.So](/WebImages/46/suppose-we-are-given-a-sequence-s-of-n-elements-each-of-whic-1147082-1761616776-1.webp)
