2

I wrote a program that sorts the values of an array. It has 5 arrays,

    int arr1[] = { 3, 9, 6, 7 };
    int arr2[] = { 2, 5, 5 };
    int arr3[] = { 0 };
    int arr4[] = { 1, 6 };
    int arr5[] = { 4, 5, 6, 2, 1 };

and an array of pointers that holds those 5 arrays,

int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };

I want to sort the values of each array, but when I pass the arrays to the sorting function

sortArrValues(&pArr[i]);

it sees the first index of each array (arr1,arr2...) as the element of that array (pArr[i]), so pArr[i] is (3,2,0,1,4).
But I want pArr[i] to be the full array it meant to be, so in the first iteration pArr[i] will be (3,9,6,7).

Notes: the first index of each array indicates the length of that array (not including that index).
The sorting will skip the first index.
There are two additional functions,they're not used (you can skip them).

Here's the full code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEN 5

void sortArrValues(int** arr);
void sortArrAdress(int* pArr);
void printArr(int * pArr);

int main()
{
    int arr1[] = { 3, 9, 6, 7 };
    int arr2[] = { 2, 5, 5 };
    int arr3[] = { 0 };
    int arr4[] = { 1, 6 };
    int arr5[] = { 4, 5, 6, 2, 1 };

    int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };

    int  i = 0;

    for (i = 0; i < LEN; i++)
    {
        sortArrValues(&pArr[i]);
    }

    //sortArrAdress(pArr);
    //printArr(pArr);

    return 0;
}
/*
this function will sort the given arrays' values.
in: array
out: none
*/
void sortArrValues(int** arr)
{
    int tmp = 0;
    int i = 0;

    for (i = 1; i < *arr; i++)
    {
        if (*arr[i] > *arr[i+1])
        {
            tmp = *arr[i];
            *arr[i] = *arr[i + 1];
            *arr[i + 1] = tmp;
        }
    }
}
8
  • I assume it crashes? Commented Mar 31, 2017 at 20:30
  • @StoryTeller No, it just iterates through the false array that is composed by the first index of each array (3,2,0,1,4). Commented Mar 31, 2017 at 20:31
  • 6
    Perhaps a review of operator precedence would help? *arr[i] -> (*arr)[i]? Commented Mar 31, 2017 at 20:31
  • 3
    i < *arr should be i < **arr Commented Mar 31, 2017 at 20:32
  • 1
    Sure, in operator precedence in C, [] has a higher priority than *. So in order to force the derefence to occur first, you must wrap the dereference in parenthesis. Look up the operator precedence table, it will save your bacon when things are not making sense. Commented Mar 31, 2017 at 20:40

2 Answers 2

1

There is some errors in your code.

First, you don't need to send &pArr[i], pArr[i] is enough to order your arrays. then your void sortArrValues(int** arr) become void sortArrValues(int* arr) which is clearer to read.

Second, in your sortValues, you when you change a value, you should restart (I know that's not very optimized but if you would want to make something really faster, you should use a quick sort. with your code, { 4, 5, 6, 2, 1 } will become { 4 5 2 1 6 }.

So that's your code with the fixes:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEN 5

void sortArrValues(int* arr);
void sortArrAdress(int* pArr);
void printArr(int * pArr);

int main()
{
    int arr1[] = { 3, 9, 6, 7 };
    int arr2[] = { 2, 5, 5 };
    int arr3[] = { 0 };
    int arr4[] = { 1, 6 };
    int arr5[] = { 4, 5, 6, 2, 1 };

    int* pArr[LEN] = { arr1, arr2, arr3, arr4, arr5 };

    int  i = 0;

    for (i = 0; i < LEN; i++)
    {
        sortArrValues(pArr[i]);
    }

    //sortArrAdress(pArr);
    //printArr(pArr);

    for (int i = 0 ; i < LEN ; i++) {
        for (int j = 0 ; j < pArr[i][0] + 1 ; j++)
            printf("%d ", pArr[i][j]);
        printf("\n");
    }

    return 0;
}
/*
this function will sort the given arrays' values.
in: array
out: none
*/
void sortArrValues(int* arr)
{
    int tmp = 0;
    int i = 0;

    for (i = 1; i < *arr; i++)
    {
        if (arr[i] > arr[i+1])
        {
            tmp = arr[i];
            arr[i] = arr[i + 1];
            arr[i + 1] = tmp;
            i = 0;

        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You should qualify the need for any other sort routine with the fact that until you have a few hundred more elements, it isn't going to make any measurable difference. Using qsort from the C library is difficult to beat. It uses a blended merge and quick sort which handles both large and small arrays efficiently.
@DavidC.Rankin is already my answer that for a better performance he should use a quick sort algorithm.
That wasn't my point. The point is it does not matter what sort you use until you have several hundred elements. You are correct that quicksort can provide many advantages, but it also has many drawbacks for newcomers trying to write there own. Much better to steer them to qsort which is part of the standard library and has years of optimization and validation in it.
0

I tried to modify your program to fix issues. Some of these issues are already highlighted by others. There were some there were not. Here is the working snippet. Let me know if you need more explanation on any modification. Note that I have only modified your sort function. The idea is not to give you the fastest sorting algorithm, but a working one.

int main()
{
    int arr1[] = { 3, 9, 6, 7 };
    int arr2[] = { 2, 5, 5 };
    int arr3[] = { 0 };
    int arr4[] = { 1, 6 };
    int arr5[] = { 4, 5, 6, 2, 1 };

    int* pArr[] = { arr1, arr2, arr3, arr4, arr5 };

    int  i = 0;

    for (i = 0; i < LEN; i++)
    {
        sortArrValues(&pArr[i]);
    }
}


void sortArrValues(int** arr)
{
    int tmp = 0;
    int i = 0, j = 0;

    for (i = 0; i < (**arr) - 1; i++)
    {
        for(j=i+1; j < (**arr) - 1; j++) {
           if ((*arr)[i+1] > (*arr)[j+1])
           {
               tmp = (*arr)[i+1];
               (*arr)[i+1] = (*arr)[j + 1];
               (*arr)[j+1] = tmp;
           }
        }
        printf("%d ", (*arr)[i+1]);
    }
    printf("%d\n", (*arr)[i+1]);
}

2 Comments

Just a bit of a pointer for answers (not a criticism). When answering, please try to explain where the error in logic and reasoning lies, and inform the OP how to properly approach problems of the type. Saying "Here is a working snippet.", without analysis, while helpful, misses an opportunity to help educate future C programmers. Who knows, the person your helping may be the next Linus -- give them a good foundation.
@DavidC.Rankin, I agree with your suggestion. I will try be more descriptive next time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.