Skip to main content
deleted 104 characters in body
Source Link

basically you are trying to create a space the size of max value and fill it with values(count of the array values in their position in v3) >=0, then refill our array with the values from the space v3

the only change I'd suggest is to completely remove the v2 array

void sort(int *v, int d) {

    int max = 0;
    for (int i = 0; i < d; i++) {
        if (v[i] > max)max = v[i];
    }

    int v3[max + 1];
    for (int i = 0; i < max + 1; i++) {
        v3[i] = 0;
    }

    for (int i = 0; i < d; i++) {
        if (v3[v[i]] == 0) {
            v3[v[i]]++;
        } else {
            v3[v[i]]++;
        }
    }

    int j = 0;
    for (int i = 0; i < max + 1; i++) {
        if (v3[i] != 0) {
            for (int k = 0; k < v3[i]; k++) {
                v[j] = i;
                j++;
            }
        }
    }
}

Note:

  1. this sort mechanism takes up too much memory in case of numbers with greatly varying differences
  2. will not support negative values

basically you are trying to create a space the size of max value and fill it with values(count of the array values in their position in v3) >=0, then refill our array with the values from the space v3

the only change I'd suggest is to completely remove the v2 array

void sort(int *v, int d) {

    int max = 0;
    for (int i = 0; i < d; i++) {
        if (v[i] > max)max = v[i];
    }

    int v3[max + 1];
    for (int i = 0; i < max + 1; i++) {
        v3[i] = 0;
    }

    for (int i = 0; i < d; i++) {
        if (v3[v[i]] == 0) {
            v3[v[i]]++;
        } else {
            v3[v[i]]++;
        }
    }

    int j = 0;
    for (int i = 0; i < max + 1; i++) {
        if (v3[i] != 0) {
            for (int k = 0; k < v3[i]; k++) {
                v[j] = i;
                j++;
            }
        }
    }
}

Note:

  1. this sort mechanism takes up too much memory in case of numbers with greatly varying differences
  2. will not support negative values

basically you are trying to create a space the size of max value and fill it with values(count of the array values in their position in v3) >=0, then refill our array with the values from the space v3

the only change I'd suggest is to completely remove the v2 array

void sort(int *v, int d) {

    int max = 0;
    for (int i = 0; i < d; i++) {
        if (v[i] > max)max = v[i];
    }

    int v3[max + 1];
    for (int i = 0; i < max + 1; i++) {
        v3[i] = 0;
    }

    for (int i = 0; i < d; i++) {
        v3[v[i]]++;
    }

    int j = 0;
    for (int i = 0; i < max + 1; i++) {
        if (v3[i] != 0) {
            for (int k = 0; k < v3[i]; k++) {
                v[j] = i;
                j++;
            }
        }
    }
}

Note:

  1. this sort mechanism takes up too much memory in case of numbers with greatly varying differences
  2. will not support negative values
Source Link

basically you are trying to create a space the size of max value and fill it with values(count of the array values in their position in v3) >=0, then refill our array with the values from the space v3

the only change I'd suggest is to completely remove the v2 array

void sort(int *v, int d) {

    int max = 0;
    for (int i = 0; i < d; i++) {
        if (v[i] > max)max = v[i];
    }

    int v3[max + 1];
    for (int i = 0; i < max + 1; i++) {
        v3[i] = 0;
    }

    for (int i = 0; i < d; i++) {
        if (v3[v[i]] == 0) {
            v3[v[i]]++;
        } else {
            v3[v[i]]++;
        }
    }

    int j = 0;
    for (int i = 0; i < max + 1; i++) {
        if (v3[i] != 0) {
            for (int k = 0; k < v3[i]; k++) {
                v[j] = i;
                j++;
            }
        }
    }
}

Note:

  1. this sort mechanism takes up too much memory in case of numbers with greatly varying differences
  2. will not support negative values