0

Dear all, I need the code for counting sort algorithm but I try this code but is not working

#include <iostream>
#include <conio.h>
#include <time.h>
#include <stdlib.h> //srand and rand functions
using namespace std;
void add_random_numbers(long arr[], long b);
void countingSort( long left, long right, long vector[], int k, long length);
long const MADH_MAX_VEKTORIT = 110000;
int main()
{
    long random_vector[MADH_MAX_VEKTORIT];
    long vector[] = { 1000, 10000, 15000, 25000, 30000, 45000, 50000, 60000, 75000, 90000, 100000 };
    long num_elem;
    clock_t tn, tp;
    srand(time(NULL));
    cout << "AlgorithmttNumbers_of_emelmentstttTime(s)n";
    cout << "--------tt-------------------tt-------n";
    for (long i = 0; i < 11; i++)
    {
        num_elem = vector[i];
        add_random_numbers(random_vector, num_elem);
        tn = clock();
        countingSort(random_vector, 0, num_elem);
        tp = clock();
        cout << "countingSort  ttt" << num_elem << "ttt" << (tp - tn) / (double)CLOCKS_PER_SEC << endl;
    }
    getchar();
    return 0;
}
void add_random_numbers(long arr[], long b)
{
    for (long i = 0; i < b; i++)
        arr[i] = rand();
}
void countingSort(long vector[], int k, long length)
{
    int c[] = new int[k];
    for (int i = 0; i < vector.length; i++)
        c[vector[i]]++;
    for (int i = 1; i < k; i++)
        c[i] += c[i - 1];
    int b[] = new int[vector.length];
    for (int i = vector.length - 1; i >= 0; i--)
        b[--c[vector[i]]] = vector[i];
    return b;
}
2
Contributors
1
Reply
17
Views
2 Years
Discussion Span
Last Post by deceptikon
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.