dimanche 31 mai 2015

Count Sort Algorithm, Unable to sort Perfectally

I been trying to Implement Count Sort Algorithm

Every time i run the Algorith It gives me wrong answer at "Highest & lowest Value" and At Index 1

It is been continuous 20 hours, & I am unable to track what i am doing wrong...

Generated_Array 90  27  58  111 105 39  24  144 19  91  38  109 39  70  177 70  122 80  75  115 
Sorted_Array    0   19  24  27  38  39  39  58  70  70  75  90  91  105 109 111 115 122 144 0   

Generated_Array 142 67  159 142 41  181 135 159 76  175 161 70  94  131 113 186 102 28  104 80  
Sorted_Array    0   186 41  67  70  76  80  94  102 104 113 131 142 142 159 159 161 175 181 0

Generated_Array 18  176 9   118 90  34  147 6   93  63  82  58  27  192 126 135 173 114 138 101 
Sorted_Array    0   6   18  27  34  58  63  82  90  93  101 114 118 126 135 138 147 173 176 0

Generated_Array 78  173 131 22  71  61  79  198 128 15  163 138 74  144 96  26  35  192 141 87  
Sorted_Array    0   198 22  26  35  61  71  78  79  87  96  128 131 138 141 144 163 173 192 0   

Generated_Array 29  139 81  81  65  12  164 76  119 95  164 41  125 184 144 59  179 143 89  33  
Sorted_Array    0   29  33  41  59  65  76  81  81  89  95  119 125 139 143 144 164 164 179 0   

Generated_Array 42  161 157 170 123 163 8   31  124 169 79  7   189 98  133 147 105 57  133 132 
Sorted_Array    0   7   8   42  57  79  98  105 123 124 132 133 133 147 157 161 163 169 170 0

Here is the algorithm I am using

int[] Counting_sort(int[] Array, int Max)
        {
            int No_Of_Elements = Array.Length;
            int[] Sorted_Array = new int[Array.Length];
            int[] C = new int[Max+1];

            for (int i = 0; i < Max; i++)
            {
                C[i] = 0; 
            }

            for (int j = 1; j <No_Of_Elements; j++)
            {
                C[Array[j]] = C[Array[j]] + 1;
            }

            for (int i = 1; i <Max; i++)
            {
                C[i] = C[i] + C[i - 1];
            }

            for (int j = No_Of_Elements-1; j >= 0; j--)
            {
                Sorted_Array[C[Array[j]]] = Array[j];
                C[Array[j]] = C[Array[j]] - 1;
            }
            return Sorted_Array;
        }

Aucun commentaire:

Enregistrer un commentaire