Often, when a list is sorted, the elements being sorted are just keys to other values. For example, if you are sorting files by their size, the sizes need to stay connected to their respective files. You cannot just take the size numbers and output them in order, you need to output all the required file information.
The counting sort is used if you just need to sort a list of integers. Rather than using a comparison, you create an integer array whose index range covers the entire range of values in your array to sort. Each time a value occurs in the original array, you increment the counter at that index. In the end, run through your counting array, printing the value of each non-zero valued index that the number of times.
Example:
Input:
10063 25 73 1 98 73 56 84 86 57 16 83 8 2581 56 9 53 98 67 99 12 83 89 80 91 39 8676 85 74 39 25 90 59 10 94 32 44 3 89 3027 79 46 96 27 32 18 21 92 69 81 40 40 3468 78 24 87 42 69 23 41 78 22 6 90 99 89 5030 20 1 43 3 70 95 33 46 44 9 69 48 33 6065 16 82 67 61 32 21 79 75 75 13 87 70 33
Output:
1 1 3 3 6 8 9 9 10 12 13 16 16 18 20 21 21 22 2324 25 25 25 27 27 30 30 32 32 32 33 33 33 34 3939 40 40 41 42 43 44 44 46 46 48 50 53 56 56 5759 60 61 63 65 67 67 68 69 69 69 70 70 73 73 7475 75 76 78 78 79 79 80 81 81 82 83 83 8485 86 86 87 87 89 89 89 90 90 91 92 94 95 96 9898 99 99
Approach:
Java
import java.util.ArrayList;import java.util.Arrays;import java.util.List;public class CountingSort2 {public static void main(String[] args) {int[] arr = { 63, 25, 73, 1, 98, 73, 56, 84, 86, 57, 16, 83,8, 25, 81, 56, 9, 53, 98, 67, 99, 12, 83, 89, 80,91, 39, 86, 76, 85, 74, 39, 25, 90, 59, 10, 94, 32, 44,3, 89, 30, 27, 79, 46, 96, 27, 32, 18, 21, 92,69, 81, 40, 40, 34, 68, 78, 24, 87, 42, 69, 23, 41, 78,22, 6, 90, 99, 89, 50, 30, 20, 1, 43, 3, 70, 95,33, 46, 44, 9, 69, 48, 33, 60, 65, 16, 82, 67, 61, 32,21, 79, 75, 75, 13, 87, 70, 33 };int[] cnt = countingSort(arr);System.out.println(Arrays.toString(cnt));}private static int[] countingSort(int[] arr) {int f[] = new int[100];int n = arr.length;List<Integer> res = new ArrayList<Integer>();for (int i = 0; i < n; i++)f[arr[i]]++;for (int i = 0; i < 100; i++) {while (f[i] > 0) {res.add(i);f[i]--;}}return res.stream().mapToInt(Integer::intValue).toArray();}}
C++
#include <bits/stdc++.h>using namespace std;vector<int> countingSort(vector<int> arr){vector<int> f(100);int n = arr.size();vector<int> res;for (int i = 0; i < n; i++)f[arr[i]]++;for (int i = 0; i < 100; i++){while (f[i]){res.push_back(i);f[i]--;}}return res;}int main(){int n = 100;vector<int> arr = {63, 25, 73, 1, 98, 73,56, 84, 86, 57, 16, 83,8, 25, 81, 56, 9, 53, 98,67, 99, 12, 83, 89, 80, 91,39, 86, 76, 85, 74, 39, 25,90, 59, 10, 94, 32, 44, 3,89, 30, 27, 79, 46, 96, 27,32, 18, 21, 92, 69, 81, 40,40, 34, 68, 78, 24, 87, 42,69, 23, 41, 78, 22, 6, 90, 99,89, 50, 30, 20, 1, 43, 3, 70,95, 33, 46, 44, 9, 69, 48, 33,60, 65, 16, 82, 67, 61, 32, 21,79, 75, 75, 13, 87, 70, 33};vector<int> res = countingSort(arr);for (int i = 0; i < res.size(); i++){cout << res[i] << " ";}return 0;}
No comments:
Post a Comment