Partitioning in C

Asked

Viewed 123 times

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;
    }

2 answers

0

Something like that:

#include <stdio.h>

int main()
{
    int n, p;
    printf("Tamanho do vetor: ");
    scanf("%d",&n);
    printf("Posicao do pivo: ");
    scanf("%d",&p);
    int vetor[n];
    printf("Digite os %d valores do vetor\n",n);
    for (int a=0; a<n; a++)
    {
        if (a!=p) printf("%d- ",a+1);
        else printf("%d* ",a+1);
        scanf("%d",&vetor[a]);
    }
    int dir[n/2], esq[n/2], iD=0, iE=0;
    for (int a=0; a<n; a++)
    {
        if ((vetor[a] < vetor[p]) && (a != p))
        {
            esq[iE]=vetor[a];
            iE++;
        }
        else if (a != p)
        {
            dir[iD]=vetor[a];
            iD++;
        }
    }
    for (int a=0; a<iE; a++) printf("%d ", esq[a]);
    printf("|%d| ", vetor[p]);
    for (int a=0; a<iD; a++) printf("%d ", dir[a]);
}

Saída no terminal

0

Try positioning the partitioning() function before the main() function. The way you did it has to declare the function to use it. If you write the function before main() no need to declare.

#include <stdio.h>
#include <stdlib.h>
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;
}
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, arr[0], arr[n-1], p);
    printf("saida\n");
    for(i = 0; i < n; i++) printf("%d ", arr[i]);
    return 0;
}

Saída no terminal

  • hmm agr ran but still n ta printando right, the print is leaving equal to the input, :/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.