0
I have to do an activity in which I must type in the input the size of the vector, the position of its pivot, and then type the elements that make up this vector
input example:
- 7 - vector size
- 1 - pivot of it
- 78 - here already begins the elements
- 43- in this case it would be the pivot
- 23
- 98
- 32
- 13
77
Exit:
23
- 32
- 13
- 43
- 98
- 78
- 77
The exit has to contain the numbers smaller than the pivot on top of it and the larger ones below it, without it being necessary to be ordered, but my code is giving "Segmentation Fault" n sei pq, someone could help me?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, p;
printf("Tamanho do vetor\n");
scanf("%d", &n);
int arr[n];
printf("Pivô do vetor\n");
scanf("%d",&p);
printf("elementos do vetor\n");
for(i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
particiona(arr, 0, n-1, p);
printf("saida\n");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
int particiona(int *V, int inicio, int final, int p ){
int esq, dir, pivo, aux;
esq = inicio;
dir = final;
pivo = V[p];
while(esq < dir){
while(V[dir] >= pivo && esq<dir)
dir--;
if(esq<dir){
V[esq] = V[dir];
}
while(V[esq] <= pivo && esq<dir)
esq++;
if(esq<dir){
V[dir] = V[esq];
}
}
V[esq] = pivo;
return esq;
}
hmm agr ran but still n ta printando right, the print is leaving equal to the input, :/
– Signatuz