Why is counting sort stable




















It means that it sorts the input without comparing the values of the elements. This is because non-comparison sorts are generally implemented with few restrictions like counting sort has a restriction on its input which we are going to study further. So, the restriction of the counting sort is that the input should only contain integers and they should lie in the range of 0 to k, for some integer k.

For example, if the value of k is 10, then all the inputs must be between 0 to Also, we don't want the value of k to be too high because that will increase the running time of the counting sort. Suppose, there are 10 boxes and 10 stones. Now, you have to keep all these 10 stones in 10 boxes but in a sorted order i.

Of course, there are many different ways to do this but let's talk about one particular way which is related to the counting sort. Suppose, you picked up a stone and you know that there are exactly 5 stones which are smaller than this stone, then without giving a single thought, you would place it in the 6 th box.

This is the exact idea behind the counting sort. Thus in the counting sort, we need an extra array to store the output like the boxes in the previous example.

This array is going to store all the numbers which are in the input, so the size of this array should be n. As the name suggests, we start by counting the number of times a number is in the input array.

For example, if 2 is appearing 4 times in the input array then we count this number and store it in a different temporary array. And the way we are going to store this number in this temporary array is by changing the value of that element of the array whose index is equal to the number itself to a value number of times it is appearing. For example, if 2 is appearing 4 times, then the element with index 2 of the temporary array will have a value of 4.

This is also illustrated in the picture given below. So, if the range of the number in the input is large i. For example, if there are numbers from 0 to 10, in the input array, then we are going to need a temporary array at least of size 10, range is starting from 0 to store how many times 10, is appearing in the input array. This is also one of the reasons why we want the value of k to be small because a large value of k would require a temporary array of large size.

Copy the output array to arr, so that arr now. Driver program to test above function. This code is contributed by Nikhil Kumar Singh. Write "Sorted character array is " ;. Write arr[i] ;. The function that sorts the given arr[]. Max ;. Min ;. WriteLine "" ;. Previous Radix Sort. Next Bucket Sort. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. That's how I understood it too. Actually it's very confusing. The OP asked about counting sort, but your explanation is for a simplified bucket sort.

The counts alone are enough, you don't need any linked list Your solution is not a full counting sort, and discards the associated values. Here's the full counting sort algorithm. After you calculated the histogram: 0 1 1 1 3 1 4 1 6 3 8 1 you have to calculate the accumulated sums - each cell will contain how many elements are less than or equal to that value: 0 1 1 2 3 3 4 4 6 7 8 8 Now you start from the end of your original list and go backwards.

Karoly Horvath Karoly Horvath Steve Jessop Steve Jessop k 34 34 gold badges silver badges bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete?

Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Linked 6. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. Still, suppose we can gather information about the sorted order of the input by means other than comparing elements. In that case, we can improve the time complexity of the sorting algorithm from O nlogn to O n.

The counting sort algorithm, for example, assumes that each of the n input elements is an integer in the range 0 to k. In other words, counting sort is one of the popular linear time sorting algorithms that works in O n time complexity if input elements are an integer in the range 0 to k. The basic idea of counting sort is to determine, for each input element x, the number of elements less than x.

This information can be used to place element x directly into its position in the output array. For example, if there are 17 elements less than x, then x belongs in output position We initialize all values of the count array C[] to zeros using a loop. Now we scan the input array A[] from start to end and store the count of each value in the count array C[].

Here we store the count of value A[i] at index A[i] in the C[]. In other words, we inspect each input element using a loop. If the value of an input element is i, we increment the value at C[i]. In other words, we modify the count array C[] such that each element at each index stores the sum of previous counts. The modified count array indicates the position of each element in the sorted output.

Now using a loop, we place each element A[i] into its correct sorted position in the output array B[]. If all n elements are distinct, then for each A[i], the value C[A[i]] - 1 is the correct final index of A[i] in the output array since there are C[A[i]] elements less than or equal to A[i]. Because the elements might not be distinct, we decrement C[A[i]] each time we place a value A[i] into the B[] array. Decrementing C[A[i]] causes the next input element with a value equal to A[i], if one exists, to go to the position immediately before A[i] in the output array.

Author: Shubham Gautam.



0コメント

  • 1000 / 1000