0% found this document useful (0 votes)
480 views3 pages

Heap Sort

This document contains the code for a HeapSort algorithm written in Java. It includes methods for building a max heap from an array, fixing the heap order property, finding the maximum child of a node, reading from and writing to files, performing an ascending sort, and exchanging elements. The main method tests the algorithm by reading numbers from a file, heap sorting them, and writing the results to an output file.

Uploaded by

ByronPérez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
480 views3 pages

Heap Sort

This document contains the code for a HeapSort algorithm written in Java. It includes methods for building a max heap from an array, fixing the heap order property, finding the maximum child of a node, reading from and writing to files, performing an ascending sort, and exchanging elements. The main method tests the algorithm by reading numbers from a file, heap sorting them, and writing the results to an output file.

Uploaded by

ByronPérez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package heapsort;
import
import
import
import
import
import
import
import
import
import

java.io.BufferedWriter;
java.io.File;
java.io.FileNotFoundException;
java.io.FileWriter;
java.io.IOException;
java.io.PrintWriter;
java.util.Arrays;
java.util.Scanner;
java.util.logging.Level;
java.util.logging.Logger;

/**
*
* @author byron
*/
public class HeapSort {
static int[] E;
static int[] H;
static int[] ee;
static int end;
//static String [] s;
public static void buildHeap(int[] E, int end) {//Construir Montn
for (int i = E.length / 2 - 1; i >= 0; i--) {// n/2 is the number of non-l
eave nodes
int k = E[i];
fixHeap(E, i, end, k);
}
}
tn

public static void fixHeap(int[] E, int root, int end, int k) {//Reparar Mon

int left = 2 * root + 1;


int m;
if (left > end) { // root is a leaf because left does not exist
E[root] = k;
} else {
m = mayor(E, root, end);
if (E[m] <= k) {
E[root] = k;
} else {
E[root] = E[m];
fixHeap(E, m, end, k);
}
}

public static int mayor(int[] E, int root, int end) {


int left = 2 * root + 1;
int right = 2 * root + 2;
int higher = left;
if (left <= end) //tiene hijo izquierdo

if (right <= end) // tiene hijo derecho


{
if (E[left] < E[right]) {
higher = right;
} else {
higher = left;
}
}

}
return higher;

public static void leerArchivo() {


try {
Scanner input = new Scanner(new File("input.txt"));
String[] s = input.nextLine().split(", ");
E = new int[s.length];
for (int i = 0; i < E.length; i++) {
E[i] = Integer.valueOf(s[i]);
}
} catch (FileNotFoundException ex) {
System.err.println("Error en lectura de archivos.\n" + ex.getLocaliz
edMessage());
}
}
public static void escribirArchivo(PrintWriter output) {
for (int i = 0; i < E.length; i++) {
output.print(E[i]);
if (i < E.length - 1) {
output.print(", ");
}
}
output.println();
//output.close();
}
public static void ordenamientoAscendente() {
buildHeap(E, E.length - 1);
}
public static void exchange(int i, int j) {
int t = E[i];
E[i] = E[j];
E[j] = t;
}
public static void maxHeap(int[] E, int i) {
int left = 2 * i;
int right = 2 * i + 1;
int largest;
if (left <= end && E[left] > E[i]) {
largest = left;
} else {
largest = i;
}
if (right <= end && E[right] > E[largest]) {
largest = right;

}
if (largest != i) {
exchange(i, largest);
maxHeap(E, largest);
}

public static void bh(int[] E) {


end = E.length - 1;
for (int i = end / 2; i >= 0; i--) {
maxHeap(E, i);
}
}
public static void heapSort(int[] e) {
H = e;
bh(H);
for (int i = end; i > 0; i--) {
exchange(0, i);
end = end - 1;
maxHeap(E, 0);
}
}
public static void main(String[] args) throws IOException {
try {
leerArchivo();
//ee = E;
System.out.println("E Before: " + Arrays.toString(E));
PrintWriter output = new PrintWriter(new BufferedWriter(new FileWrit
er("output.txt")));
escribirArchivo(output);
ordenamientoAscendente();
System.out.println("H After: " + Arrays.toString(E));
escribirArchivo(output);
heapSort(E);
System.out.println("HeapSort: " + Arrays.toString(E));
escribirArchivo(output);
output.close();
} catch (IOException ex) {
Logger.getLogger(HeapSort.class.getName()).log(Level.SEVERE, null, e
x);
}
}
}

You might also like