Java implements counting sorting algorithm

Counting sorting algorithm

Counting sort is a sorting algorithm that sorts the elements of an array by counting the number of occurrences of each unique element in the array. Store the count in an auxiliary array, and complete the sorting by mapping the count to the index of the auxiliary array.


How does counting sorting work?

  1. Find the largest element ( max) from the given array .Calculation and sorting steps

    Given array

  2. Initialize an max+1array of all elements with a length of 0. This array is used to store the number of elements in the array.Counting and sorting steps

    Count array

  3. Store the count of each element at counttheir respective index in the array.

    For example: if the count of element 3 is 2, then store 2 in the third position of the element with a large number of counts. If the element "5" does not exist in the array, 0 is stored in the fifth position.Counting and sorting steps

    The count of each element stored

  4. Stores the cumulative sum of count array elements. It helps to put the elements in the correct index of the sorted array.Counting and sorting steps

    Cumulative number

  5. Find the index of each element of the original array in the count array. This gives the cumulative count. Place the element at the calculated index as shown in the figure below.Calculation and sorting steps

    Count sort

  6. After placing each element in the correct position, reduce its number by one.

Counting sorting algorithm

countSort(array,size)
  max <-查找数组中的最大元素
  用全零初始化计数数组
  对于j <-0到大小
    找到每个唯一元素的总数,然后 
    将计数存储在count数组中的第j个索引处
  对于我<-1到最大
    找到累积和并将其存储在count数组本身中
  对于j <-减小到1
    恢复元素到数组
    将还原的每个元素的数量减少1

Java example

 
import java.util.Arrays;

public class CountingSortDemo {
	public static void main(String[] args) {
		int[] array = { 4, 2, 2, 8, 3, 3, 1 };
		// Find the maximum element in the input array
		int max = findMaxElement(array);
		System.out.println("Max Value in input array-" + max);
		System.out.println("Original Array- " + Arrays.toString(array));
		int[] sortedArr = countingSort(array, max + 1);
		System.out.println("Sorted array after counting sort- " + Arrays.toString(sortedArr));
	}

	private static int findMaxElement(int[] array) {
		int max = array[0];
		for (int val : array) {
			if (val > max)
				max = val;
		}
		return max;
	}

	private static int[] countingSort(int[] array, int range) {
		int[] output = new int[array.length];
		int[] count = new int[range];
		// Calculate frequency of each element, put it in count array
		for (int i = 0; i < array.length; i++) {
			count[array[i]]++;
		}
		System.out.println("Count array- " + Arrays.toString(count));

		// Modify count array to get the final position of elements
		for (int i = 1; i < range; i++) {
			count[i] = count[i] + count[i - 1];
		}
		System.out.println("Modified count array- " + Arrays.toString(count));

		// Add elements to output sorted array
		for (int i = 0; i < array.length; i++) {
			output[count[array[i]] - 1] = array[i];
			count[array[i]]--;
		}
		return output;
	}
}

 

Output:

Max Value in input array-8
Original Array- [4, 2, 2, 8, 3, 3, 1]
Count array- [0, 1, 2, 2, 1, 0, 0, 0, 1]
Modified count array- [0, 1, 3, 5, 6, 6, 6, 6, 7]
Sorted array after counting sort- [1, 2, 2, 3, 3, 4, 8]
 


complex

time complexity:

There are four main cycles. (The maximum value can be found outside the function.)

cycle Counting time
the first O (maximum)
Second place O (size)
Third place O (maximum)
fourth place O (size)

Overall complexity =  O(max)+O(size)+O(max)+O(size)=O(max+size)

  • Worst-case complexity: O(n+k)
  • Best case complexity: O(n+k)
  • Average case complexity: O(n+k)

In all the above cases, the complexity is the same, because no matter how the elements are placed in the array, the algorithm will go through n+ktime.

 

 

There is no comparison between any elements, so it is better than comparison-based sorting techniques. However, if the integer is large, it is not good, because an array of that size should be made.

Space complexity:

The space complexity of counting and sorting is O(max). The larger the element range, the greater the space complexity.


Computing Sorting Application

Use count sorting in the following situations:

  • There are multiple smaller integers.
  • Linear complexity is necessary.

Guess you like

Origin blog.csdn.net/allway2/article/details/114003894