0
People have to develop a code that reads the size of 2 vectors dps the elements of it (these vectors are increasing), transform into a single vector (it doesn’t have to be ordered), and then using the interlink function, transform into an ordered vector and print it. The question is, is it possible to do this without using Mergesort? only using the " Merge" function? I tried a lot of things here but there’s always two elements that should be at the beginning that are getting at the end.
Edit: In short, the doubt is even if it is possible to do what the exercise asks without using any sort algorithm, only the "Interlayer" function that in this case would interlink the vector from position 0 to half-1 with the vector half to size-1 Follows the code.
#include <stdio.h>
#include <stdlib.h>
int intercala(int *v, int inicio, int n, int t){
int v1 = inicio;
int fim = (n+t)-1;
int meio = (n+t)/2;
int v2 = meio+1;
int i,j;
int tamanho = fim-inicio+1;
int vet[tamanho];
for(i=0; i<tamanho; i++){
if(v[v1] <= v[v2]){
vet[i]=v[v1++];
}
else{
vet[i]=v[v2++];
}
}
while(v1<=meio){
vet[i++] = v[v1++];
}
while(v2<=fim){
vet[i++] = v[v2++];
}
for(i=0; i<tamanho; i++){
printf("%d ", vet[i]);
}
return 0;
}
int main (){
int n, t, i;
printf("Tamanho do primeiro vetor\n");
scanf("%d", &n);
printf("Tamanho do segundo vetor\n");
scanf("%d", &t);
int v[t+n];
printf("elementos do primeiro vetor\n");
for(i=0; i <n; i++){
scanf("%d", &v[i]);
}
printf("Elementos do segundo vetor\n");
for(i=n; i <(n+t); i++){
scanf("%d", &v[i]);
}
intercala(v, 0, n, t);
return 0;
}
Opa, then I got to take a look at the sorting algorithms, but the doubt is even if it is possible to do what the exercise asks without using any sorting algorithm, only the "Intermediate" function that in the case would be intermediate the vector from position 0 to middle-1 with half vector up to size-1"
– Signatuz