Monday, April 29, 2024
HomeJavaHow Counting Kind Work in Java? Instance Tutorial

How Counting Kind Work in Java? Instance Tutorial


Howdy guys, in our final article, we appeared on the Radix Kind in Java and on this article, we’ll take a look at the counting type in Java. In case you are pondering how they’re associated then let me inform you that each are O(n) sorting algorithms. Sure, its doable to type in O(n) or linear time. In case you are questioning that you’ve got thus far solely discovered that finest sorting algorithms are Fast Kind and Merge Kind who kinds an array in O(nLogN) time then you might be in for shock. Sure, there present O(n) sorting algorithms that are sooner than each Quicksort and Merge type like Radix Kind, Counting Kind, and Bucket Kind however they’ve their limitation. They aren’t normal function sorting algorithm and you should use this to type solely integers and it additionally relies upon upon how massive is the info set and what number of completely different numbers are within the knowledge set. 

This was truly requested to me a few years again after I was interviewing for an enormous Funding financial institution, even after a lot studying I wasn’t aware of Counting Kind and Bucket Kind and I felt embarrassed that I did not even heard the identify of those sorting algorithm, overlook about explaining how they work and when to make use of them. 

At the moment, I let it go by merely accepting the truth that I did not learn about them however as quickly as interview was over I checked out each Google and Fb to seek out out what are these liner time sorting algorithms, how they work and when to make use of them? I used to be truly fairly excited that I discovered one thing new from Interview. 

I’ve already defined how Bucket type works and how Radix type works in my earlier algorithms and on this one I’ll simply clarify how Counting Kind works after which we’ll see code instance of  the right way to implement Counting type in Java. 
One of many key factor to recollect about these linear sorting algorithms is that they do not type by evaluating factors and that is why the are in a position to give efficiency which is best than O(NLogN) as a result of the second you’ll begin evaluating components, you’ll lean in direction of logN time.  They’re also referred to as non-comparison sorting algorithms
Counting sort in Java with Example

How Counting Kind Algorithms Work?

Because the identify suggests Counting Kind algorithm kinds an array of integer by counting how variety of instances a singular factor seem within the array. It woks finest when your array accommodates small optimistic integers, also referred to as keys on this case.   

Counting type can be not a normal function sorting algorithms and works solely when the vary of factor is small however you may as well use it to type unfavorable quantity.  The most effective case, common case  and worst case efficiency of Counting type algorithms all identical and it’s O(n +ok) the place ok is the vary of non unfavorable key worth.

Since sorting algorithms are finest understood by seeing sorting in motion,  here’s a YouTube tutorial which explains how Counting Kind works with instance, you’ll be able to watch this video a few instances to grasp how Counting Kind kinds components as its not a straightforward one which you’ll be able to grasp in only one minute, chances are you’ll be nevertheless it took me a few instances to essentially get this into my head.

You may watch this YouTube tutorial proper right here, its from CS Dojo or you’ll be able to watch it on CS Dojo channel on YouTube, one in every of my favourite channels. 

Java Program to Implement Counting Kind Algorithm

Right here is an instance of doing counting type in Java. 

import java.util.Arrays;

/**
 * Java Program to type an array of integers with Counting Kind algorithm.
 * Counting Kind is a linear time sorting algorithm, which reap the benefits of
 * favorable situation to type an inventory of integers in O(n) time.
 *
 * @creator WINDOWS 8
 */
public class CountingSort {

    public static void major(String args[]) {

        // sorting integer array utilizing Counting Kind
        int[] random = {4, 8, 3, 2, 9, 3, 11};
        System.out.println("Unsorted random integer array : "
                               + Arrays.toString(random));

        countingsort(random, 11);
        System.out.println("Sorted integer array : "
                             + Arrays.toString(random));

        // yet another instance
        int[] massive = {332, 44, 32, 99, 11, 119, 434, 33};
        System.out.println("Unsorted array of integers : " 
                             + Arrays.toString(massive));

        countingsort(massive, 434);
        System.out.println("Sorted integer array : "
                             + Arrays.toString(massive));
    }

    /*
     * Types integer array utilizing Counting Kind Algorithm
     * time complexity : Greatest Case O(n+ok); Common Case O(n+ok); 
     * Worst Case O(n+ok),
     * the place n is the scale of the enter array and ok means the
     * values vary from 0 to ok.
     */
    public static void countingsort(int[] enter, int ok) {
        // create buckets
        int counter[] = new int[k + 1];

        // fill buckets
        for (int i : enter) {
            counter[i]++;
        }

        // type array
        int ndx = 0;
        for (int i = 0; i < counter.size; i++) {
            whereas (0 < counter[i]) {
                enter[ndx++] = i;
                counter[i]--;
            }
        }
    }
}

Output :
Unsorted random integer array : [4, 8, 3, 2, 9, 3, 11]
Sorted integer array : [2, 3, 3, 4, 8, 9, 11]
Unsorted array of integers : [332, 44, 32, 99, 11, 119, 434, 33]
Sorted integer array : [11, 32, 33, 44, 99, 119, 332, 434]

That is all about the right way to implement counting type in Java. It is one of many O(n) sorting algorithms also referred to as linear time sorting algorithm and fall into class of non-comparison sorting algorithm. This implies you type numbers with out evaluating them. 

The most effective case and worst case efficiency for this algorithm is identical and they’re O(n +ok ) the place ok is the vary of non-negative numbers you might be sorting. Whereas counting type is just not a normal function algorithm like Quicksort or Mergesort, and even heap type, its nonetheless a pleasant algorithm to know as you may be requested throughout interviews as properly. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments