Last active
August 29, 2015 14:06
-
-
Save reinaldorauch/b22db9f7e5b2418fe1fd to your computer and use it in GitHub Desktop.
Métodos de ordenação Heapsort e Mergesort em C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define TAM 10 | |
int array[TAM]; | |
/** | |
* Algoritmo de ordenação | |
*/ | |
void sortArray() { | |
int pass = 1, sorted = 0, aux, i; | |
while(!sorted && pass < TAM) { | |
sorted = 1; | |
for (i = 0; i < TAM - pass - 1; i++) | |
{ | |
if(array[i] > array[i + 1]) { | |
aux = array[i]; | |
array[i] = array[i + 1]; | |
array[i + 1] = aux; | |
sorted = 0; | |
} | |
} | |
pass++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define TAM 100000 | |
int array[TAM], tamHeap; | |
/** | |
* Gera uma heap no vetor | |
* @param i raiz da heap | |
*/ | |
void heapify(i) { | |
int l = 2 * i + 1, | |
r = 2 * i + 2, | |
m = 0, | |
t = 0; | |
if (l < tamHeap && array[l] > array[i]) | |
m = l; | |
else | |
m = i; | |
if (r < tamHeap && array[r] > array[m]) | |
m = r; | |
if (m != i) { | |
t = array[i]; | |
array[i] = array[m]; | |
array[m] = t; | |
heapify(m); | |
} | |
} | |
/** | |
* Algoritmo de ordenação heap sort | |
*/ | |
void heapSort() { | |
int i = 0, t = 0; | |
tamHeap = TAM; | |
for (i = ((TAM / 2) - 1); i >= 0; i--) | |
heapify(i); | |
for (i = (TAM - 1); i > 1; i--) | |
{ | |
t = array[0]; | |
array[0] = array[i]; | |
array[i] = t; | |
tamHeap--; | |
heapify(0); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define TAM 100000 | |
int array[TAM], scratch[TAM]; | |
/** | |
* Algoritmo de ordenação merge sort | |
* @param i início do vetor | |
* @param j final do vetor | |
*/ | |
void mergeSort(i, j) { | |
int m = 0, l = 0, n = 0, k = 0; | |
if(i < j) { | |
m = (i + j) / 2; | |
mergeSort(i, m); | |
mergeSort(m + 1, j); | |
l = i; | |
n = m + 1; | |
for (k = i; k <= j; k++) | |
if ((l <= m) && ((n > j) || (array[l] < array[n]))) | |
scratch[k] = array[l++]; | |
else | |
scratch[k] = array[n++]; | |
for (k = i; k <= j; k++) | |
array[k] = scratch[k]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define TAM 10000 | |
int array[TAM]; | |
/** | |
* Gera o ponto de divisão da partição | |
* @param p inicio do array | |
* @param r final do array | |
* @return ponto de divisão da partição | |
*/ | |
int partition(p, r) { | |
int piv = array[p], i = (p - 1), j = (r + 1), temp = 0; | |
while(1) { | |
while(array[--j] > piv); | |
while(array[++i] < piv); | |
if(i < j) { | |
temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} else | |
return j; | |
} | |
} | |
/** | |
* Implementação do quicksort | |
* @param p início do vetor | |
* @param r fim do vetor | |
*/ | |
void quickSort(int p, int r) { | |
int q = 0; | |
if(p < r) { | |
q = partition(p, r); | |
quickSort(p, q); | |
quickSort(q + 1, r); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment