Sort the values using Counting Sort Algo in C++, C
Here is the code : Debug, Run and Learn
Thanks
#include <iostream>
#include <conio.h>
#define RANGE 100
using namespace std;
void main()
{
int inputArray[] = {23, 1, 45, 13, 19, 16, 90, 23, 35, 12, 28, 67, 56, 23, 99, 23, 67, 45, 67, 34, 23};
const int len = sizeof(inputArray) / sizeof(int);
int count[RANGE + 1];
int outputArray[len];
memset(count, 0, sizeof(count));
for (int index = 0; index < len; ++index)
{
++count[inputArray[index]];
}
for (int index = 1; index <= RANGE; ++index)
{
count[index] += count[index - 1];
}
for (int index = 0; index < len; ++index)
{
outputArray[count[inputArray[index]] - 1] = inputArray[index];
--count[inputArray[index]];
}
cout << "Unsorted Values " << endl;
for (int index = 0; index < len; ++index)
{
cout << inputArray[index] << " ";
}
cout << "\n\nSorted Values " << endl;
for (int index = 0; index < len; ++index)
{
cout << outputArray[index] << " ";
}
/* cout << "\n\nCount Array Values " << endl;
for (int index = 0; index < RANGE; ++index)
{
cout << count[index] << " ";
}*/
_getch();
}
Thanks

Comments
Post a Comment