[edit] Characteristics of counting sortCounting sort is a stable sort and has a running time of Θ(n+k), where n and k are the lengths of the arrays A (the input array) and C (the counting array), respectively. In order for this algorithm to be efficient, k must not be much larger than n. The indices of C must run from the minimum to the maximum value in A to be able to index C directly with the values of A. Otherwise, the values of A will need to be translated (shifted), so that the minimum value of A matches the smallest index of C. (Translation by subtracting the minimum value of A from each element to get an index into C therefore gives a counting sort. If a more complex function is used to relate values in A to indices into C, it is a bucket sort.) If the minimum and maximum values of A are not known, an initial pass of the data will be necessary to find these (this pass will take time Θ(n); see selection algorithm). The length of the counting array C must be at least equal to the range of the numbers to be sorted (that is, the maximum value minus the minimum value plus 1). This makes counting sort impractical for large ranges in terms of time and memory needed. Counting sort may for example be the best algorithm for sorting numbers whose range is between 0 and 100, but it is probably unsuitable for sorting a list of names alphabetically. However counting sort can be used in radix sort to sort a list of numbers whose range is too large for counting sort to be suitable alone. Because counting sort uses key values as indexes into an array, it is not a comparison sort, and the Ω(n log n) lower-bound for sorting is inapplicable. [edit] Tally sort
A well-known variant of counting sort is tally sort, where the input is known to contain no duplicate elements, or where we wish to eliminate duplicates during sorting. In this case the count array can be represented as a bit array; a bit is set if that key value was observed in the input array. Tally sort is widely familiar because of its use in the book Programming Pearls as an example of an unconventional solution to a particular set of limitations.[1] [edit] The algorithm[edit] Informal
[edit] C++ implementation/// countingSort - sort an array of values. /// /// For best results the range of values to be sorted /// should not be significantly larger than the number of /// elements in the array. /// /// param nums - input - array of values to be sorted /// param size - input - number of elements in the array /// void counting_sort(int *nums, int size) { // search for the minimum and maximum values in the input int i, min = nums[0], max = min; for(i = 1; i < size; ++i) { if (nums[i] < min) min = nums[i]; else if (nums[i] > max) max = nums[i]; } // create a counting array, counts, with a member for // each possible discrete value in the input. // request compiler to value-initialize all counts to 0. int distinct_element_count = max - min + 1; int *counts = new int[distinct_element_count](); // accumulate the counts - // each index in the counts array represents the value // of an element in the input nums array, so the result // of incrementing the sum at the index in the // counts array reflects the number of times the element // appears in the input array and therefore // must be copied to the sorted output. for(i=0; i<size; ++i) ++counts[ nums[i] - min ]; // store back into the input array the sorted value as // represented by the respective index in the counts array. // repeat for each additional occurrence of the value // found in the original array, as recorded by the // counts array. int j=0; for(i=min; i<=max; i++) for(int z=0; z<counts[i-min]; z++) nums[j++] = i; delete[] counts; } [edit] References[edit] Notes
[edit] Bibliography
[edit] External links
offerte voli | hoteles | precios | voli | die verzeichnis | annuarie web | stop smoking london | ||||||||||||||||||||||||||||||||||||||||||||||||