0

Hey, let me try explaining what i need to do with an example
Example
Before sorting
arr1 = {15,12,18,11}
arr2 = {1,2,3,4}
arr2 is always in ascending order and its length is based on the number of elements in arr1
After sorting
arr2 = {2,3,1,4}
So basically i need arr2 to show which largest number the element with the same index in arr1 is(18 is the largest so its number 1 and 12 is the 3rd largest so its number 3}

Ive tried using bubble sort but i seem to be doing something wrong, either way here is the code.

for(i = 1; i <= k; i++)
	{
		for(j = 0; j < k - 1; j++)
		{
			if(arr1[j+1] > arr1[j])
			{
				temp = arr2[j];
				arr2[j] = arr2[j+1];
				arr2[j+1] = temp;
                        }
		}
	}

Edited by inzombiak: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by thekashyap
0
void sort(int arr1[], int arr2[], int k) {

    int i = 0, j=0, temp=0;

    for (i = 1; i <= k; i++) {
        for (j = 0; j < k - 1; j++) {

            if (arr1[j+1] > arr1[j]) {
                printf("swapping: %d and %d\n", arr2[j], arr2[j+1]);
                temp = arr2[j];
                arr2[j] = arr2[j+1];
                arr2[j+1] = temp;
            }

        }
    }
}



int main() {

    int arr1[] = {15,12,18,11};
    int arr2[] = {1,2,3,4};

    sort(arr1, arr2, 4);

    int i = 0;
    for(; i < 4; i++)
        printf("%d", arr2[i]);

    return 0;
}
Output is: 
swapping: 2 and 3
swapping: 3 and 2
swapping: 2 and 3
swapping: 3 and 2
1234

Does that tell you anything?

Edited by thekashyap: n/a

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.