Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
Skills:
Algorithm Basics90%
Key Takeaways
Explains the Counting Sort algorithm in less than 6 minutes with a visual example
Full Transcript
here's a quick introduction to Counting sort let's just say as an example we are trying to sort this array into an ascending order and Counting St works the best when the range of numbers each value could have is very small in the array so let's just say the range of values that we could have for each item is 0 1 2 and three the first part of applying counting sort is going to be finding the starting index for each number and it's going to be clear what I mean by that in a second and the first step for doing that is going to be counting the number of occurrences for each number in the array so for this example there's only one occurrence of zero and three occurrences of one so I'm going to write down one and three and then two occurrences of three and zero occurrences of two and we can store these numbers in an array of length four in this particular case once we have these numbers the number of occurrences for each number the second step is going to be adding each number to the right of it accumulatively what I mean by that is the first number at index zero stays the same and the second number will be 1 + 3 = 4 and the third number will be 4 + 0 which is equal to 4 and the force number at the index three will be 4 + 2 which is equal to 6 after that step we have the numbers 1 4 4 6 for the numbers 0 1 2 and three the next step is going to be shifting this whole array to the right by one cell and we're going to do that by looking at this array from right to left we're going to first start with the last index which is index three in this particular case and the value there should be the value that's currently in index 2 so we're going to put four at index 3 and we're going to do the same thing for index 2 the value here should be what's in index one currently so it should also be four and at index one we're going to put one at index zero we're going to put zero and these numbers 01 44 are actually the starting indexes for the range of numbers we have in this particular case0 1 two and three and here's one way to think about it if you look at the number three or the index three the corresponding number the corresponding starting index is four and that's because when we sort this array the number three will start at the index four another way to think about it is that there are four items that should appear before the number three in the SED array and that should be obvious if you just look at the first array that we constructed there is one occurrence of zero three occurrences of one and Zer occurrences of two so adding them up there are four items that should appear before the number three and that's why the starting index for the number three is four once we have the starting index for each number the rest is relatively easy we're going to first initialize a new array that's the same length as the original array so length six in this particular case and then we're going to iterate it through the original array 1 by one when we are looking at the first element of this array which is one we're going to check what the starting index should be for this number from the array that we constructed we see that the starting index is one so we're going to put this number into index one of the new array and then we're going to increment our starting index by one and it becomes two and this is so that when we look at the second instance of the number one we'll know that it should go to the index two instead of one and then we're going to increment it again we'll do the same thing for each number of this array from left to right until we get to the last number in the original array now you might have noticed that this process makes counting sort a stable sorting algorithm what that means is that of course if you look at different instances of the same value for example the number one the order in which they appear in the new sorted array is exactly the same as the order in which they appear in the original array this wouldn't matter if they were just number one but if they represented underlying meanings to them for example if there were ages of people three different people let's say Emily Tom and George then we might care if they appear in the same order or in a different order in the sorted array as the original unsorted array now let's quickly go through a few important properties of counting sort first of all as I mentioned it's a stable sorting algorithm and the time complexity for this algorithm is Big of n plus K where n is the number of items that we have in the original array and K is the range of numbers that we could have for each item in the array and finally the auxiliary space complexity is bigger of n plus k for counting sort because we need to create an entirely new array with length n the same size as the original array as well as an array of length K to keep track of the starting index for each number
Original Description
Step-by-step guide to counting sort with a visual example. Counting sort is useful when the range of values each item can take is very small.
For example, you can use if if you want to sort 10,000 people according to their age. We can safely assume (for now) that no human is older than 199 years old, so the range of values is very small in this case.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 8 of 60
1
2
3
4
5
6
7
▶
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Building a Power Grid Inside Minecraft with BFS Algorithms
Dev.to · Carlos Cortez 🇵🇪 [AWS Hero]
The Run-Length Encoding Trick: How Simple Strings Get Compressed
Medium · Programming
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Medium · AI
I implemented the algorithm that broke the sorting barrier. Dijkstra still wins.
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI