ESTRUTURA DE DADOS - ALGORITMOS DE ORDENAÇÃO INTERNA
Autor: Marco Aurelio | Publicado: 2011-10-14T19:20:00-07:00


/*
&nbsp;========================================================================
&nbsp;Name &nbsp; &nbsp; &nbsp; &nbsp;: teste.c
&nbsp;Author &nbsp; &nbsp; &nbsp;: Marco Aurélio
&nbsp;Version &nbsp; &nbsp; :
&nbsp;Copyright &nbsp; : On Eclipse Indigo - Ubuntu 10.04
&nbsp;Description : Ordenação interna

Atualizado....

&nbsp;========================================================================
&nbsp;*/


#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;
#include &lt;time.h&gt;
#define N 20
typedef int tchave;
typedef char tinfo[20];
typedef struct Cel
{
&nbsp;&nbsp; &nbsp;tchave chave;
&nbsp;&nbsp; &nbsp;tinfo info;
&nbsp;&nbsp; &nbsp;}celula;
typedef celula vetor[N];

void inicializar()
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;int i;
&nbsp;&nbsp; &nbsp;vetor v;
&nbsp;&nbsp; &nbsp;srand(time(NULL));
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;for(i=0;i&lt;N;i++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v[i].chave = (rand()%100);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;}
void troca(int *a, int *b)
{
 int temp;
 temp=*a;
 *a=*b;
 *b=temp;
}
//==============================================================================
void booble(vetor lista)
{
int passada, i, trocou;
&nbsp;&nbsp; &nbsp;for(passada = 1; passada &lt; N; passada++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;trocou = 0;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(i = 0; i &lt; N-passada; i++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(lista[i].chave &gt; lista[i+1].chave)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;troca(&amp;lista[i], &amp;lista[i+1]);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;trocou = 1;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(trocou == 0) break;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}
}

//=============================================================================
void selects(vetor lista)
{
&nbsp;&nbsp; int i, j, min;
&nbsp;&nbsp; for (i = 0; i &lt; N; i++)
&nbsp;&nbsp; {
&nbsp;&nbsp; &nbsp; &nbsp;min = i;
&nbsp;&nbsp; &nbsp; &nbsp;for (j = i+1; j &lt; N; j++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (lista[j].chave &lt; lista[min].chave)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;min = j;
&nbsp;&nbsp; &nbsp; &nbsp;troca(&amp;lista[min], &amp;lista[i]);
&nbsp;&nbsp; }
}

//==============================================================================
void quick(vetor lista, int min, int max)
{
&nbsp;&nbsp; int i = min;
&nbsp;&nbsp; int j = max;
&nbsp;&nbsp; tchave pivo = lista[min].chave;
&nbsp;&nbsp; do
&nbsp;&nbsp; {
&nbsp;&nbsp; &nbsp; &nbsp;while(lista[i].chave &lt; pivo &amp;&amp; i &lt; max) i++;//verifica se o item na posição "i" é menor que o "pivo" e se "i" é menor que a ultima posição "max"
&nbsp;&nbsp; &nbsp; &nbsp;while(lista[j].chave &gt; pivo &amp;&amp; j &gt; min) j--;//verifica se o item na posição "j" é maior que o "pivo" e se "j" é maior que a primeira posição "min"
&nbsp;&nbsp; &nbsp; &nbsp;if(i &lt;= j) // se "i" for menor ou igual a "j"
&nbsp;&nbsp; &nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; troca(&amp;lista[i], &amp;lista[j]); i++; j--;//efetua a troca do item na posição "i"
&nbsp;&nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; } while(i &lt;= j);
&nbsp;&nbsp; if(min &lt; j) quick(lista, min, j);
&nbsp;&nbsp; if(max &gt; i) quick(lista, i, max);
}

//=============================================================================


void shell(vetor v, int unit)// unit é um parametro que informa em quantas partes o vetor sera dividido
{
&nbsp;&nbsp; &nbsp;long int i, j; celula temp;
&nbsp;&nbsp; &nbsp;int k; k=1;
&nbsp;&nbsp; &nbsp;do
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;k = unit * k + 1;
&nbsp;&nbsp; &nbsp;} while(k &lt; N);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;do
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k /= unit;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(i = k; i &lt; N; i++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;temp = v[i]; j = i-k;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while(j &gt;= 0 &amp;&amp; temp.chave &lt; v[j].chave)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v[j+k] = v[j]; j -= k;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v[j+k] = temp;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}while(k &gt; 1);
}


//=============================================================================

void merge(vetor v, int tam)//tam é um parametro que informa o tamanho do vetor
{
&nbsp;&nbsp; &nbsp;int meio;
&nbsp;&nbsp; &nbsp;if(tam &gt; 1)
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;meio = tam/2;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;merge(v, meio);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;merge(v+meio, tam-meio);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;pass(v, tam);
&nbsp;&nbsp; &nbsp;}
}

//=============================================================================


void insert(vetor v)
{
 int i, j; celula temp;
 for(i = 1; i &lt; N; i++)
 {
  temp = v[i];
  j = i-1;
  while(j &gt;= 0 &amp;&amp; temp.chave &lt; v[j].chave)
  {
   v[j+1] = v[j];
   j--;
  }
   v[j+1] = temp;
 }
}


//=============================================================================


void heap(vetor v, int n)//n é um parametro que informa o tamanho do vetor
{
&nbsp;&nbsp; int i;
&nbsp;&nbsp; // construindo o heap
&nbsp;&nbsp; for( i = (n-1 / 2); i &gt;=1 ; i--){
&nbsp;&nbsp; &nbsp; &nbsp;ajusta(v, i, n);
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; // excluindo a raiz e atualizando o heap
&nbsp;&nbsp; for( i = n-1; i &gt;=1; i--){
&nbsp;&nbsp; &nbsp; &nbsp;troca(&amp;v[i+1], &amp;v[1]);
&nbsp;&nbsp; &nbsp; &nbsp;ajusta(v, 1, i);
&nbsp;&nbsp; &nbsp;}
}

//=============================================================================




void ajusta(vetor v, int i,int n)
{
&nbsp;&nbsp; long int j;
&nbsp;&nbsp; tchave chave;
&nbsp;&nbsp; celula raiz;
&nbsp;&nbsp; int fim;
&nbsp;&nbsp; fim =0; raiz = v[i]; chave = v[i].chave; j = 2 * i;
&nbsp;&nbsp; //raiz recebe o registro na posição "i" e a chave recebe o valor da chave no registro "i"
&nbsp;&nbsp; // j recebe duas vezes o valor de "i"
&nbsp;&nbsp; while ((j &lt;= n) &amp;&amp; (fim==0)) {
&nbsp;&nbsp; &nbsp; &nbsp; if (j &lt; n) { //valor de j sendo menor que o tamanho do vetor
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (v[j].chave &lt; v[j+1].chave) {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = j + 1;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; }
&nbsp;&nbsp; &nbsp; &nbsp; if (chave &gt;= v[j].chave) {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fim = 1;
&nbsp;&nbsp; &nbsp; &nbsp; }else {
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v[j / 2] = v[j];
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j = 2 * j;
&nbsp;&nbsp; &nbsp; &nbsp; }
&nbsp;&nbsp; }
&nbsp;&nbsp; v[j / 2] = raiz;
}

//=============================================================================




int main()
{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;int op;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;do{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("Menu inicial, Digite \n1 para BOOBLESORT,\n2 para SELECTSORT,\n3 para QUICKSORT,\n4 para Sair\n\n&gt;&gt;&gt; ");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; scanf ("%d", &amp;op);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ((op&lt;1)||(op&gt;4))// Verifica se a opção digitada é valida
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; printf("Opcao invalida\n\n\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (op==3)break;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}while((op&lt;1)||(op&gt;3));
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;int i;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;vetor v;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;srand(time(NULL));
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;for(i=0;i&lt;N;i++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;v[i].chave = (rand()%100);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("Vetor antes de ordenar\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(i=0;i&lt;N;i++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("%d ",v[i].chave);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("\n\n\n");

&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;switch (op){
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;case 1:
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//clock_t inicio = clock();
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;booble(v);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//clock_t fim=clock();
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("Vetor depois de ordenado\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int x;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(x=0;x&lt;N;x++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("%d ",v[x].chave);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("\n\n\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//printf("tempo total %lu segundos\n\n",(fim-inicio)/CLOCKS_PER_SEC);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;case 2:
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;selects(v);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("Vetor depois de ordenado\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int j;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(j=0;j&lt;N;j++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("%d ",v[j].chave);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("\n\n\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;case 3:
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;quick(v,N-N,N-1);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("Vetor depois de ordenado\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int k;
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for(k=0;k&lt;N;k++)
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("%d ",v[k].chave);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("\n\n\n");
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;}

}