Hello, I have a hard time resolving my workout and would like some help. I thought about doing with max heap, but it did not work.
Here's the statement:
"Indirect sort problem".) Write an algorithm that receives an integer
n
and a constant vector of integersA[1..n]
and returns a vectorB[1..n]
of indexes of vectorA
in such a way thatA[B[1]] ≤ A[B[2]] ≤ . . . ≤ A[B[n]]
. Values stored in a constant vector can not be changed. IfA
is a constant vector andA[1] = 5
, thenA[1] = 5
forever.Eg: Suppose a
A = [3, 6, 1, 8]
entry. Your algorithm should returnB = [3, 1, 2, 4]
, therefore,A[B[1]] = A[3] = 1 ≤ A[B[2]] = A[1] = 3 ≤ A[B[3]] = A[2] = 6 ≤ A[B[4]] = A[4] = 8
.
Here's what I've tried:
#include <stdio.h>
#include <stdlib.h>
void ordena(int v1[], int v2[], int v3[]);
void imprime(int v[]);
int main()
{
int v1[4];
int v2[4];
int v3[4];
/*
printf("Digite 4 valores)");
scanf("%d", v[0]);
scanf("%d", v[1]);
scanf("%d", v[2]);
scanf("%d", v[3]);
*/
v1[0] = 3;
v1[1] = 6;
v1[2] = 1;
v1[3] = 8;
v2[0] = 3;
v2[1] = 1;
v2[2] = 2;
v2[3] = 4;
ordena(v1, v2, v3);
imprime(v3);
return 0;
}
void ordena(int v1[], int v2[] , int v3[]){
int aux, i, j;
int k;
k=0;
for(i=0;i<4;i++){
for(j=0;j<4;j++){
if(v1[v2[i]] > v1[j]){
v3[k] = v1[v2[i]];
k++;
}
}
}
}
void imprime(int v[])
{
int i;
for(i = 0; i < 4; i++)
{
printf("\nElemento %d.... %d\n", i, v[i]);
}
}