Sorting arrays and keeping them in sync

I need to know if I'm writing this sort function correctly. that sort's an array of #'s into descending order while maintaining the link up of another array of numbers.

For example, a set of data for array will look like this:
Arr[]={ 4, 2, 1, 5, 3}

And a set of data for array2 will look like this:
Arr2[]={ 780, 600, 1900, 690, 1200}

All of the numbers are coming from a data file.

I need it too look something like this:

Arr1: Arr2:
5 690
4 780
3 1200
2 600
1 1900

Can someone show me please? Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void sort1array(int arr[],int arr2[],int arr3 [],int arr4 [],int k){

    int temp,pass,cand;

    for (int i = 0;i<k-1;i++){
        for(cand =0;cand<k-1;cand++){
        if(arr[cand]>arr[cand+1]){
            int temp = arr [cand+1];
            arr [cand+1] = arr [cand];
            arr [cand ] = temp;
            
            }
        }
    }


    return;

}
Last edited on
You're pretty close.

The inner loop isn't quite right though, cand should be initialized to i + 1, and the loop continues while cand is less than k.

Then remove the "cand+1"s and replace them with i.

I believe lines 8-10 are not needed, as you seem to have a swap function which you are calling at line 11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void sort1array(int arr[],int arr2[],int arr3 [],int arr4 [],int k){

    int temp,pass,cand;

    for (int i = 0;i<k-1;i++){
        for(cand = i+1;cand<k;cand++){
        if(arr[cand]>arr[i]){

            swap(arr[cand],arr[i]);
             swap(arr2[cand],arr2[i]);
             swap(arr3[cand],arr3[i]);
             swap(arr4[cand],arr[i]);
            }
        }
    }


I did that and now it's not sorting
What is your swap function?
closed account (D80DSL3A)
There is an error on line 12.
Surely you meant: swap(arr4[cand],arr4[i]);
1
2
3
4
5
6
7
8
9
10
11
12
for (int i = 0;i<k-1;i++){
        for(cand =0;cand<k;cand++){
        if(arr[cand]>arr[i]){
            int temp = arr [cand+1];
            arr [cand+1] 
            swap(arr[cand],arr[i]);
             swap(arr2[cand],arr2[i]);
             swap(arr3[cand],arr3[i]);
             swap(arr4[cand],arr4[i]);
            }
        }
    }


I fixed it now. Now I'm getting a weird output. at line 17, i got some random numbers

This is my output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
9    69   86   221  
10   82   69   160  
20   22   20   342  
22   33   95   138  
36   97   73   230  
42   59   58   172  
42   155  101  344  
50   80   80   180  
60   28   100  207  
66   299  41   390  
79   101  86   223  
88   56   108  206  
88   16   44   82   
90   12   43   97   
100  66   300  466  
300  100  23   213  
268663279   88   373  
55   56   72   216  

 


Here is my input data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
18 // this is the size of the array

50	299	41
79 	28	100
42	56	108
100	66	300
300	22	20
20	80	80
60	97	73
42	12	43
90	100	23
88	155	101
36	101	86
66	69	86
9	82	69
10	33	95
22	16	44
88	56	72
206	79	88
55	59	58
97	82	63
43	77	33
Last edited on
closed account (D80DSL3A)
Maybe because you are swapping arr twice.
One time arr[cand] with arr[cand+1].
Again with arr[cand] and arr[i]
Dude!! Look at your code!
Last edited on
I tried removing one of them but once I remove them, I get really weird outputs but swapping them twice gets me the output I want. it's just line 17 that gets weird
Last edited on
If you look at the code, cand+1 is not going to always be a valid index into arr. You're modifying memory you have no business modifying.

I get really weird outputs but swapping them twice gets me the output I want. it's just line 17 that gets weird


Which says to me, "this random thing I tried is closer than the other random thing I tried, but neither one actually do what I want." Reason it out.

Yes thank you, i finally figured it out! Thank you all so much for the help
Topic archived. No new replies allowed.