commit
6b036fe5a2
Binary file not shown.
Binary file not shown.
|
|
@ -1,5 +1,4 @@
|
||||||
#include "heapsort.h"
|
#include "heapsort.h"
|
||||||
#include "geracao.h"
|
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
void constroi(int *array, int tamHeap){
|
void constroi(int *array, int tamHeap){
|
||||||
for(int i = tamHeap; i > 1 && array[i] > array[i/2]; i /= 2){
|
for(int i = tamHeap; i > 1 && array[i] > array[i/2]; i /= 2){
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef HEAPSORT_H
|
#ifndef HEAPSORT_H
|
||||||
#define HEAPSORT_H
|
#define HEAPSORT_H
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
|
#include "geracao.h"
|
||||||
|
//=============================================================================
|
||||||
void constroi(int *array, int tamHeap);
|
void constroi(int *array, int tamHeap);
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
int getMaiorFilho(int *array, int i, int tamHeap);
|
int getMaiorFilho(int *array, int i, int tamHeap);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,45 @@
|
||||||
#include "mergesort.h"
|
#include "mergesort.h"
|
||||||
|
#include <stdlib.h>
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
void mergesort(int *array, int n) {
|
void mergesort(int *array, int n) {
|
||||||
array[0] = n;
|
mergesortRec(array, 0, n-1);
|
||||||
|
}
|
||||||
|
//=============================================================================
|
||||||
|
void mergesortRec(int *array, int esq, int dir){
|
||||||
|
if (esq < dir){
|
||||||
|
int meio = (esq + dir) / 2;
|
||||||
|
mergesortRec(array, esq, meio);
|
||||||
|
mergesortRec(array, meio + 1, dir);
|
||||||
|
intercalar(array, esq, meio, dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//=============================================================================
|
||||||
|
void intercalar(int* array, int esq, int meio, int dir){
|
||||||
|
int n1, n2, i, j, k;
|
||||||
|
|
||||||
|
//Definir tamanho dos dois subarrays
|
||||||
|
n1 = meio-esq+1;
|
||||||
|
n2 = dir - meio;
|
||||||
|
|
||||||
|
int* a1 = (int*) malloc((n1+1) * sizeof(int));
|
||||||
|
int* a2 = (int*) malloc((n2+1) * sizeof(int));
|
||||||
|
|
||||||
|
//Inicializar primeiro subarray
|
||||||
|
for(i = 0; i < n1; i++){
|
||||||
|
a1[i] = array[esq+i];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Inicializar segundo subarray
|
||||||
|
for(j = 0; j < n2; j++){
|
||||||
|
a2[j] = array[meio+j+1];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sentinela no final dos dois arrays
|
||||||
|
a1[i] = a2[j] = 0x7FFFFFFF;
|
||||||
|
|
||||||
|
//Intercalacao propriamente dita
|
||||||
|
for(i = j = 0, k = esq; k <= dir; k++){
|
||||||
|
array[k] = (a1[i] <= a2[j]) ? a1[i++] : a2[j++];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,8 @@
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
void mergesort(int *array, int n);
|
void mergesort(int *array, int n);
|
||||||
//=============================================================================
|
//=============================================================================
|
||||||
|
void mergesortRec(int *array, int esq, int dir);
|
||||||
|
//=============================================================================
|
||||||
|
void intercalar(int* array, int esq, int meio, int dir);
|
||||||
|
//=============================================================================
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -27,6 +27,7 @@ class Mergesort extends Geracao {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sort() {
|
public void sort() {
|
||||||
|
mergesort(0, n-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -35,5 +36,46 @@ class Mergesort extends Geracao {
|
||||||
* @param int dir fim do array a ser ordenado
|
* @param int dir fim do array a ser ordenado
|
||||||
*/
|
*/
|
||||||
private void mergesort(int esq, int dir) {
|
private void mergesort(int esq, int dir) {
|
||||||
|
if (esq < dir){
|
||||||
|
int meio = (esq + dir) / 2;
|
||||||
|
mergesort(esq, meio);
|
||||||
|
mergesort(meio + 1, dir);
|
||||||
|
intercalar(esq, meio, dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Algoritmo que intercala os elementos entre as posicoes esq e dir
|
||||||
|
* @param int esq inicio do array a ser ordenado
|
||||||
|
* @param int meio posicao do meio do array a ser ordenado
|
||||||
|
* @param int dir fim do array a ser ordenado
|
||||||
|
*/
|
||||||
|
public void intercalar(int esq, int meio, int dir){
|
||||||
|
int n1, n2, i, j, k;
|
||||||
|
|
||||||
|
//Definir tamanho dos dois subarrays
|
||||||
|
n1 = meio-esq+1;
|
||||||
|
n2 = dir - meio;
|
||||||
|
|
||||||
|
int[] a1 = new int[n1+1];
|
||||||
|
int[] a2 = new int[n2+1];
|
||||||
|
|
||||||
|
//Inicializar primeiro subarray
|
||||||
|
for(i = 0; i < n1; i++){
|
||||||
|
a1[i] = array[esq+i];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Inicializar segundo subarray
|
||||||
|
for(j = 0; j < n2; j++){
|
||||||
|
a2[j] = array[meio+j+1];
|
||||||
|
}
|
||||||
|
|
||||||
|
//Sentinela no final dos dois arrays
|
||||||
|
a1[i] = a2[j] = 0x7FFFFFFF;
|
||||||
|
|
||||||
|
//Intercalacao propriamente dita
|
||||||
|
for(i = j = 0, k = esq; k <= dir; k++){
|
||||||
|
array[k] = (a1[i] <= a2[j]) ? a1[i++] : a2[j++];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue