free hosting   image hosting   hosting reseller   online album   e-shop   famous people 
Free Website Templates
Free Installer

Algoritmos de Arranjo (Sorting Algorithms)

Faculdade Estácio de Sá, 2008:

Princípios de Programação I - Prof. B. Bazzanella.
Autor: Sidnei M. Barrientos Jr.


Arranjos de Troca: Bubble Sort, Cocktail Sort, Comb Sort, Gnome Sort, Quicksort
Arranjos de Seleção: Selection Sort, Heap Sort, Smooth Sort, Strand Sort
Arranjos de Inserção: Insertion Sort, Shell Sort, Tree Sort, Library Sort, Patience Sort
Arranjos de União: Merge Sort
Arranjos Sem Comparação: Radix Sort, Bucket Sort, Counting Sort, Pigeonhole Sort, Tally Sort

Arranjos de Troca

Arranjo de Bolhas (Bubble Sort)

C:

#include <stdio.h>

int main(void){
    int lista[8] = {4,7,4,5,8,3,2,0};
    int aux = 0;
    int x = 0;
    int y = 0;
    int tamanhoArray = sizeof(lista)/sizeof(*lista);
    for (x = 0; x < tamanhoArray ; x++){
        for (y = tamanhoArray ; y >= x + 1; y--){
                if(lista[y - 1] > lista[y]){
                      aux = lista[y-1];
                      lista[y-1] = lista[y];
                      lista[y]=aux;
                }
        }
    }
    
    for (x=0; x < tamanhoArray; x++)
        printf("%d",lista[x]);
        
    scanf("%d",&x);
}

 Exemplo Java:


Arranjo Coquitel (CocktailSort)

C: (Não funciona)

#include <stdio.h>

int main(void){
    int lista[8] = {4,7,4,5,8,3,2,0};
    int inicio = -1;
    int aux; //auxiliar para troca de elementos
    int fim = sizeof(lista)/sizeof(*lista) - 1;
    bool trocado;
    do {
        trocado = false;
        /*incrementa inicio pois os elementos anteriores a ele já estão arrumados*/
        inicio++;
        for(int x=inicio; x <= fim; x++){
                if(lista[x] > lista[x + 1]){
                        aux = lista[x + 1];
                        lista[x + 1] = lista[x];
                        lista[x] = aux;
                        trocado = true;
                }
        }
        if (!trocado){ //se nenhum dos elementos foram trocados acima
                break;
        }
        trocado = false;
        fim--; //decresce fim pois os elementos após fim estão na ordem correta
        for (int x=fim; x>=inicio; x--){
                if(lista[x]> lista[x + 1]){
                        aux = lista[x];
                        lista[x] = lista[x+1];
                        lista[x+1] = aux;
                }
        }
    } while(trocado);
    
    for (int x=0; x < fim; x++)
        printf("%d",lista[x]);
        
    scanf("%d",&aux);        
}


Exemplo Java:




Arranjos de Seleção


Arranjo de Seleção (Selection Sort)


C: (Não Funciona)
#include <stdio.h>

int main(void){
    int lista[8] ={4,7,4,5,8,3,2,0};
    int menor = 0;
    int aux = 0;
    int tamanhoArray = sizeof(lista)/sizeof(*lista);
    for (int x=0; x < tamanhoArray-1 ;x++){
        menor = x;
        for (int y=x+1; x < tamanhoArray; y++){
            if (lista[y] < lista[x]){
                        menor = y;
            }
        }
        //troca os elementos
        aux = lista[menor];
        lista[menor] = lista[x];
        lista[x] = lista[menor];
    }
    for (int x=0; x < tamanhoArray; x++)
        printf("%d",lista[x]);
      
    scanf("%d",&aux);
}
Exemplo Java:


Arranjos de Inserção

Inserção (Insertion Sort)

C: (Não funciona)

#include <stdio.h>

int main(void){
    int lista[8] = {4,7,4,5,8,3,2,0};
    int valor, y, x;
    int tamanhoArray = sizeof(lista)/sizeof(*lista);
    for (x=1; x <= tamanhoArray; x++){
        valor = lista[x];
        y = x - 1;
        while (y>=0 && lista[y]>valor){
                lista[y + 1] = lista[y];
                y++;
        }
    }
    for (x=0; x < tamanhoArray; x++)
        printf("%d",lista[x]);
       
    scanf("%d",&x); 
}

 Exemplo Java:



Arranjo de Shell (ShellSort)

C: (Não funciona)

#include <stdio.h>

int main(void){
    int lista[8] = {4,7,4,5,8,3,2,0};
    int aux = 0;
    int x = 0;
    int y = 0;
    int tamanhoArray = sizeof(lista)/sizeof(*lista);
    int incremento = tamanhoArray / 2;
   
    while(incremento > 0){
        for  (x = incremento; x  < tamanhoArray;  x++){
             y = x;
             aux = lista[x];
             while ((y - incremento) && (lista[y-incremento] > aux)){
                   lista[y] = lista[y  - incremento];
                   y -= incremento;
             }
             lista[y] = aux;
        }
        if (incremento == 2)
           incremento = 1;
        else
            incremento = (int) (incremento / 2.2);
    }
       
    for (x=0; x < tamanhoArray; x++)
    printf("%d",lista[x]);
       
    scanf("%d",&x);
}

Exemplo java:




Arranjos de União

Merge Sort

 


Arranjos Sem Comparação