Arquivos da categoria: Algoritmos de Ordenação

Algoritmo de Ordenação Bolha (Bubblesort) – Pascal

CUsersClaudionorDesktopBuble.exe
//==============================================================================
// Implementação do algoritmo de ordenação bubblesort em pascal
//==============================================================================
program Bubble;
uses crt;

var
   v: array [1..10] of integer;
   I: integer;

// Procedimento para ordenação utilizando o método bolha
procedure bubble_sort();
var
   I, J, troca: integer;
begin
     for I := 1 to 10 do
     begin
          for J := I+1 to 10 do
          begin
               if v[J] < v[I] then
               begin
                    troca := v[I];
                    v[I] := v[J];
                    v[J] := troca;
               end;
          end;
     end;
end;

begin
     // Lê 10 valores
     for I := 1 to 10 do
     begin
          write('Digite um valor: ');
          read(v[I]);
     end;

     // Ordenação
     bubble_sort();

     // Impressão dos valores.
     writeln('');
     writeln('* Resultado *');
     for I := 1 to 10 do
     begin
          writeln('Valor ', I, ': ', v[I]);
     end;
     readkey;
end.

Algoritmo Quick Sort em C (Quicksort)

//================================================================
// Nome Do Arquivo: quick.c
// File Name: quick.c
//
// Descrição: Implementação do algoritmo quicksort
// Description: Quick sort Algorithm
//================================================================

// Libs
#include <stdio.h>
#include <stdlib.h>

// Define uma constante
// Define a constant
#define MAX 10

// Protótipo da função de ordenação
// Ordination function prototype
void quick_sort(int *a, int left, int right);

// Função main
// Main Function
int main(int argc, char** argv)
{
 int i, vet[MAX];

 // Lê MAX ou 10 valores
 // Read MAX or 10 values
 for(i = 0; i < MAX; i++)
 {
  printf("Digite um valor: ");
  scanf("%d", &vet[i]);
 }

 // Ordena os valores
 // Order values
 quick_sort(vet, 0, MAX - 1);

 // Imprime os valores ordenados
 // Print values in order ascendant
 printf("nnValores ordenadosn");
 for(i = 0; i < MAX; i++)
 {
  printf("%dn", vet[i]);
 }
 system("pause");
 return 0;
}

// Função de Ordenação por Seleção
// Quick sort function
void quick_sort(int *a, int left, int right) {
	int i, j, x, y;
	
	i = left;
	j = right;
	x = a[(left + right) / 2];
	
	while(i <= j) {
		while(a[i] < x && i < right) {
			i++;
		}
		while(a[j] > x && j > left) {
			j--;
		}
		if(i <= j) {
			y = a[i];
			a[i] = a[j];
			a[j] = y;
			i++;
			j--;
		}
	}
	
	if(j > left) {
		quick_sort(a, left, j);
	}
	if(i < right) {
		quick_sort(a, i, right);
	}
}

Algoritmo de Ordenação por Seleção em C (Selection Sort)

//================================================================
// Nome Do Arquivo: selection.c
// File Name: selection.c
//
// Descrição: Implementação do algoritmo de ordenação por seleção
// Description: Selection sort Algorithm
//================================================================

// Libs
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

// Define uma constante
// Define a constant
#define MAX 10

// Protótipo da função de ordenação
// Ordination function prototype
void selection_sort(int *a);

// Função main
// Main Function
int main(int argc, char** argv)
{
 int i, vet[MAX];

 // Lê MAX ou 10 valores
 // Read MAX or 10 values
 for(i = 0; i &lt; MAX; i++)
 {
  printf(&quot;Digite um valor: &quot;);
  scanf(&quot;%d&quot;, &amp;vet[i]);
 }

 // Ordena os valores
 // Order values
 selection_sort(vet);

 // Imprime os valores ordenados
 // Print values in order ascendant
 printf(&quot;nnValores ordenadosn&quot;);
 for(i = 0; i &lt; MAX; i++)
 {
  printf(&quot;%dn&quot;, vet[i]);
 }
 system(&quot;pause&quot;);
 return 0;
}

// Função de Ordenação por Seleção
// Selection sort function
void selection_sort(int *a)
{
 int i, j, k, tmp, troca;
 
 for(i = 0; i &lt; MAX-1; i++)
 {
  troca = 0;
  k = i;
  tmp = a[i];
  for(j = i+1; j &lt; MAX; j++)
  {
   if(a[j] &lt; tmp)
   {
    k = j;
    tmp = a[j];
    troca = 1;
   }
  }
  if(troca)
  {
   a[k] = a[i];
   a[i] = tmp;
  }
 }
}

Algoritmo de Ordenação por Inserção em C (Insertion Sort)

//================================================================
// Nome Do Arquivo: insertion.c
// File Name: insertion.c
//
// Descrição: Implementação do algoritmo de ordenação por inserção
// Description: Insertion sort Algorithm
//================================================================

// Libs
#include <stdio.h>
#include <stdlib.h>

// Define uma constante
// Define a constant
#define MAX 10

// Protótipo da função de ordenação
// Ordination function prototype
void insertion_sort(int *a);

// Função main
// Main Function
int main(int argc, char** argv)
{
 int i, vet[MAX];

 // Lê MAX ou 10 valores
 // Read MAX or 10 values
 for(i = 0; i < MAX; i++)
 {
  printf("Digite um valor: ");
  scanf("%d", &vet[i]);
 }

 // Ordena os valores
 // Order values
 insertion_sort(vet);

 // Imprime os valores ordenados
 // Print values in order ascendant
 printf("nnValores ordenadosn");
 for(i = 0; i < MAX; i++)
 {
  printf("%dn", vet[i]);
 }
 system("pause");
 return 0;
}

// Função de Ordenação por Inserção
// Insertion sort function
void insertion_sort(int *a)
{
 int i, j, tmp;
 
 for(i = 1; i < MAX; i++)
 {
  tmp = a[i];
  for(j = i-1; j >= 0 && tmp < a[j]; j--)
  {
   a[j+1] = a[j];
  }
  a[j+1] = tmp;
 }
}

Algoritmo de Ordenação Shell em C (Shell Sort)

//================================================================
// Nome Do Arquivo: shell.cpp
// File Name: shell.cpp
//
// Descrição: Implementação do algoritmo de ordenação Shell
// Description: Shellsort Algorithm
//================================================================

// Libs
#include <stdio.h>
#include <stdlib.h>

// Define uma constante
// Define a constant
#define MAX 10

// Protótipo da função de ordenação
// Ordination function prototype
void shell_sort(int *a, int size);

// Função main
// Main Function
int main(int argc, char** argv)
{
 int i, vet[MAX];

 // Lê MAX ou 10 valores
 // Read MAX or 10 values
 for(i = 0; i < MAX; i++)
 {
  printf("Digite um valor: ");
  scanf("%d", &vet[i]);
 }

 // Ordena os valores
 // Order values
 shell_sort(vet, MAX);

 // Imprime os valores ordenados
 // Print values in order ascendant
 printf("nnValores ordenadosn");
 for(i = 0; i < MAX; i++)
 {
  printf("%dn", vet[i]);
 }
 system("pause");
 return 0;
}

// Função de ordenação Shell
// Shellsort function
void shell_sort(int *a, int size)
{
 int i , j , value;
 int gap = 1;
 
 do {
  gap = 3*gap+1;
 } while(gap < size);
 
 do {
  gap /= 3;
  for(i = gap; i < size; i++) {
   value = a[i];
   j = i - gap;
   
   while (j >= 0 && value < a[j]) {
    a[j + gap] = a[j];
    j -= gap;
   }
   a[j + gap] = value;
  }
 }while(gap > 1);
}

Algoritmo de Ordenação Bolha em C (Bubble Sort)

//================================================================
// Nome Do Arquivo: bubble.cpp
// File Name: bubble.cpp
//
// Includes: stdio.h
//
// Descrição: Implementação do algoritmo de ordenação por seleção
// Description: Bubble Sort Algorithm
//================================================================

// Define uma constante
// Define a constant
#define MAX 10

// Protótipo da função de ordenação
// Ordination function prototype
void bubble_sort(int *a);

// Função main
// Main Function
int main(int argc, char** argv)
{
 int i, vet[MAX];

 // Lê MAX ou 10 valores
 // Read MAX or 10 values
 for(i = 0; i < MAX; i++)
 {
  printf("Digite um valor: ");
  scanf("%d", &vet[i]);
 }

 // Ordena os valores
 // Order values
 bubble_sort(vet);

 // Imprime os valores ordenados
 // Print values in order ascendant
 printf("nnValores ordenadosn");
 for(i = 0; i < MAX; i++)
 {
  printf("%dn", vet[i]);
 }

 return 0;
}

// Função de ordenação bolha
// Bubble sort function
void bubble_sort(int *a)
{
 int i, j, tmp;

  for(i = 0; i < MAX; i++)
  {
   for(j = i+1; j < MAX; j++)
   {
    if(a[j] < a[i])
    {
     tmp = a[i];
     a[i] = a[j];
     a[j] = tmp;
    }
  }
 }
}