0

I have two integer arrays, and I am trying to sort the first array based on the other array.

eg. a = {1,2,3,6,0,0,0};
and b = {1,2,2,0,0,0,0};

the values sorted in b is the real value for each integer in a

the expected result I am expecting after sorting is:

a = {2,3,1,6,0,0,0};
b = {2,2,1,0,0,0,0};

this is the code I used

int j,k,temp1,temp2;
for (j=0; j<N; j++){
    for (k=j+1; k<N; k++){
        if (b[j] < b[k]){
            temp1 = b[j];
            b[j] = b[k];
            b[k] = temp1;
            temp2 = a[j];
            a[j] = a[k];
            a[k] = temp2;
        }
    }
}

it gives me output: a = {2,3,1,0,0,0,6}; and b = {2,2,1,0,0,0,0};

I can't tell where the mistakes are, any help and suggestion is appreciated.

2
  • 2
    I don't think I understand what you want to archive. What do you mean by "the values sorted in b is the real value for each integer in a"? Commented Apr 23, 2017 at 13:43
  • Your code is fine and gives the expected output. For example you can add a puts("swap") inside the if condition to check when 2 values are swapped and it happens 2 times in your case. You might probably have compiled the wrong source code Commented Apr 23, 2017 at 15:48

2 Answers 2

0

Taking the main part of your code verbatim and converting it into an MCVE (Minimal, Complete, Verifiable Example), I get the code:

#include <stdio.h>

static void pr_data(const char *tag, int n, int *a)
{
    printf("%s = { ", tag);
    const char *pad = "";
    for (int i = 0; i < n; i++)
    {
        printf("%s %d", pad, a[i]);
        pad = ", ";
    }
    puts(" };");
}

int main(void)
{
    int a[] = { 1, 2, 3, 6, 0, 0, 0 };
    int b[] = { 1, 2, 2, 0, 0, 0, 0 };
    enum { N = sizeof(a) / sizeof(a[0]) };
    pr_data("a", N, a);
    pr_data("b", N, b);

    int j, k, temp1, temp2;
    for (j = 0; j < N; j++)
    {
        for (k = j + 1; k < N; k++)
        {
            if (b[j] < b[k])
            {
                temp1 = b[j];
                b[j] = b[k];
                b[k] = temp1;
                temp2 = a[j];
                a[j] = a[k];
                a[k] = temp2;
            }
        }
    }
    pr_data("a", N, a);
    pr_data("b", N, b);

    return 0;
}

Note the most basic debugging technique — print what you have before and after you operate on it. This assures that you can print the data correctly, that you have the right data, and gives you the result using the same printing code that's known to work.

The output from this is:

a = {  1,  2,  3,  6,  0,  0,  0 };
b = {  1,  2,  2,  0,  0,  0,  0 };
a = {  2,  3,  1,  6,  0,  0,  0 };
b = {  2,  2,  1,  0,  0,  0,  0 };

The output looks like what you wanted. Therefore, it appears that the problem is not in the code that you posted but in the code that you didn't post. It is not clear what form that trouble could take.

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

Comments

-1

What you are saying is that you want the sort to be stable. That means that items that are equal in the original array will keep the same order in the final sorted array.

In fact the code that you've posted here is indeed stable and produces the correct output. I suspect that the real code that you are running that you are using the following comparison:

b[j] <= b[k]

That is not a stable sort and will reorder entries that are equal to each other. I was able to get the "wrong" output that you are describing by making this change, but the code that you've posted here with:

b[j] < b[k]

is stable and produces the output you wanted.

Comments

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.