4
There is some more efficient way to do that same function and do not know why on my computer is not working the code.
#include <stdlib.h>
#include <stdio.h>
void ordena(int A[],int n);
int buscaBinaria( int chave, int inicial, int v[]);
int main ()
{
    /*declaracao das variaveis*/
    int i = 0;
    int A[5];
    /*populaciona o vetor*/
   A[0]= 5;
   A[1]= 4;
   A[2]= 3;
   A[3]= 2;
   A[4]= 1;
    /*imprime o vetor*/
    for (i = 0; i <5; i++)
    {
        printf( " O VETOR DESORDENADO EH DESTA FORMA: %d \n \n", A[i]);
    }
    /*chama a funcao que ordena*/
    ordena(A,5);
    /*imprime o vetor ordenado*/
    for (i = 0; i <5; i++)
    {
        printf( " O VETOR ORDENADO EH DESTA FORMA: %d \n \n", A[i]);
    }
}
void ordena ( int A[],int n)
{
    int j, i;
    /*percorreo vetor*/
    for(j = 0; j < n; j++)
    {
      int chave = A[j];
      int m = buscaBinaria(n, chave, A);
      A[j] = A[m];
      A[m] = chave;
    }
}
/* A função abaixo recebe um número x e um vetor
 crescente v[0..n-1]. Ela devolve um índice m
 tal que v[m] == x ou devolve -1 se tal m não
 existe.
 */
int buscaBinaria( int tam, int chave, int v[])
{
   int ini, fim, m, i;
   ini = 0;
   fim = tam - 1;
   while (ini <= fim)
   {
     m = (ini + fim)/2;
     if (v[m] >= chave){
         fim = m - 1;
     }else
         ini = m + 1;
   }
   i = v[m];
   printf("i vale: %d\n", i);
   return m;
}
Binary search only works if the array is already sorted. So you can’t use it to sort the array, at least not the way you’re trying to do it.
– Victor Stafusa
there I am using the binary search split and fetch the smallest value, ai .
– Matheus Francisco
Are you trying to implement the quicksort algorithm? If not, then what sort algorithm are you trying to implement?
– Victor Stafusa