#11.Journey to DSA in C++
Hello, inquisitive brains! Welcome to a magical voyage utilizing C++ into the enthralling world of Data Structures and Algorithms (DSA).
Today I explored, Counting Sort and Radix Sort in the context of Data Structures and Algorithms (DSA), and understood their implementation>
Counting Sort ๐ง
Counting sort is the easiest and fastest algorithm that I have ever learned and now it's my favorite. Before that I didn't know about it. When I started to learn and search for this my mind said "I had wasted my life bro without knowing this sorting exits!"๐๐๐
The Art of Sorting by Numbers Imagine receiving a bag of vibrant marbles, each bearing a different number. It's up to you to put them in ascending order without making any comparisons. It sounds difficult, don't you think? That's what Counting Sort does, after all. It's like having a hassle-free, magical assistant who can sort numbers.
Imagine receiving a bag of vibrant marbles, each bearing a different number. It's up to you to put them in ascending order without making any comparisons. It sounds difficult, don't you think? That's what Counting Sort does, after all. It's like having a hassle-free, magical assistant who can sort numbers.
Counting Sort Implementation in C++
cppCopy code#include <iostream>
using namespace std;
void countingSort(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
int *count = new int[max + 1]();
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
int outputIndex = 0;
for (int i = 0; i <= max; i++) {
while (count[i] > 0) {
arr[outputIndex] = i;
outputIndex++;
count[i]--;
}
}
delete[] count;
}
Radix Sort ๐
Now, let's take a step into the enigmatic world of Radix Sort. Imagine you have a deck of cards, and you want to sort them not based on their face value but on the patterns on their backs. Radix Sort is your magical assistant, sorting data by examining digits one at a time. It's perfect for those situations where you need to sort numbers with multiple digits.
Radix Sort Implementation in C++
cppCopy code#include <iostream>
using namespace std;
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
void countingSort(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
void radixSort(int arr[], int n) {
int max = getMax(arr, n);
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(arr, n, exp);
}
}
Radix Sort breaks down the sorting process into multiple passes, each inspecting a different digit. It's like sorting the cards based on their backs' patterns, from the least significant digit to the most significant one.
The beauty of these sorting techniques lies in their simplicity and efficiency. Counting Sort excels when you have a limited range of values, while Radix Sort works wonders with multi-digit numbers. By understanding these sorceries, you'll have more tools in your coding arsenal to tackle diverse challenges.
So, why are these sorting spells essential in your coding journey? Here's the secret: they not only organize data but also enhance your problem-solving skills. These are like the training wheels that help you balance the complexities of coding, and as you master them, you'll find yourself pedaling smoothly through intricate algorithms and data structures.
Experiment and Embrace Curiosity! ๐ค๐ฌ
As we conclude this leg of our journey, I encourage you to experiment and dive deeper into the world of sorting algorithms. Try different scenarios, explore edge cases, and see how these sorting techniques perform. It's through experimentation that you'll truly grasp their power.
In our next session, we'll unravel more coding secrets, exploring data structures and algorithms that will leave you in awe. Get ready for more magic in the world of DSA!
Stay curious, keep coding, and remember that every line of code you write brings you one step closer to coding mastery. โจ๐ป๐
Subscribe to my newsletter
Read articles from Joy Shree directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Joy Shree
Joy Shree
<<I'm JOYSHREE >> With an insatiable curiosity for technology, a passion for learning new things, and an unwavering enthusiasm for mastering code, this girl is on a joyful journey of growth and discovery