A small variation form Counting Sort Algo to sort values within a range
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}; int len = sizeof(inputArray) / sizeof(int); int count[RANGE]; memset(count, 0, sizeof(count)); for (int index = 0; index < len; ++index) { ++count[inputArray[index]]; } cout << "Unsorted Values " << endl; for (int index = 0; index < len; ++index) { cout<<inputArray[index]<<" "; } /* cout << "\nCount Array Values " << endl; for (int index = 0; index < RANGE; ++index) { cout << count[index] << " "; }*/ cout << "\nSorted Values " << endl; for (int index = 0; index < RANGE; ++index) { if (count[index] != 0) { for (int inner = 0; inner < count[index]; ++inner) { cout << index << " "; } } } _getch(); }
Thanks
Comments
Post a Comment