Counting Sort 1

Comparison Sorting
Quicksort usually has a running time of nxlog(n), but is there an algorithm that can sort even faster? In general, this is not possible. Most sorting algorithms are comparison sorts, i.e. they sort a list just by comparing the elements to one another. A comparison sort algorithm cannot beat nxlog(n) (worst-case) running time, since nxlog(n) represents the minimum number of comparisons needed to know where to place each element.

Alternative Sorting
Another sorting method, the counting sort, does not require comparison. Instead, 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. At the end, run through your counting array, printing the value of each non-zero valued index that number of times.


Example:

Input:

100
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

Output:

0 2 0 2 0 0 1 0 1 2 1 0 1 1 0 0 2 0 1 0
1 2 1 1 1 3 0 2 0 0 2 0 3 3 1 0 0 0 0 2
2 1 1 1 2 0 2 0 1 0 1 0 0 1 0 0 2 1 0 1 1 1 0 1 0 1 0 2 1 3 2 0 0 2 1 2 1 0 2 2
1 2 1 2 1 1 2 2 0 3 2 1 1 0 1 1 1 0 2 2

Approach:

Java


import java.util.Arrays;

public class CountingSort1 {
    public static void main(String[] args) {
        int[] arr = { 63257319873568486,
                 57168382581569539867,
                 9912838980,
                91398676857439259059,
                1094324438930277946
                962732182192,
                69814040346878248742,
                 6923417822690998950,
                 302014337095,
                334644969483360651682,
             6761322179757513877033 };

        int[] cnt = countingSort(arr);
        System.out.println(Arrays.toString(cnt));
    }

    private static int[] countingSort(int[] arr) {
        int n = arr.length;
        int[] cnt = new int[100];
        for (int i = 0; i < n; i++) {
            cnt[arr[i]]++;
        }
        return cnt;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;
vector<intcountingSort(vector<intarr)
{

    int n = arr.size();
    vector<intcnt(100);
    for (int i = 0i < ni++)
    {
        cnt[arr[i]]++;
    }
    return cnt;
}

int main()
{
    int n = 100;
    vector<intarr = {63257319873,
                       568486571683,
                       825815695398,
                       67991283898091,
                       39867685743925,
                       9059109432443,
                       89302779469627,
                       32182192698140,
                       40346878248742,
                       692341782269099,
                       89503020143370,
                       953346449694833,
                       6065168267613221,
                       79757513877033};

    vector<intcnt = countingSort(arr);
    for (int i = 0i < 100i++)
    {
        cout << cnt[i] << " ";
    }

    return 0;
}


No comments:

Post a Comment