3
Hello, I’m having trouble solving my workout and would like a help. I thought to do with max heap but it didn’t work out.
That is the statement:
"Indirect sorting problem". ) Write an algorithm that takes an integer number
n
and a constant vector of integersA[1..n]
and returns a vectorB[1..n]
vector indexA
in such a way thatA[B[1]] ≤ A[B[2]] ≤ . . . ≤ A[B[n]]
. Values stored in a constant vector cannot be changed. IfA
is a constant vector andA[1] = 5
, thenA[1] = 5
forever.Ex.: Suppose an entry
A = [3, 6, 1, 8]
. Your algorithm must returnB = [3, 1, 2, 4]
, for,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]);
}
}