Skip to content

Instantly share code, notes, and snippets.

@supermomonga
Created May 6, 2013 22:04
Show Gist options
  • Save supermomonga/5528606 to your computer and use it in GitHub Desktop.
Save supermomonga/5528606 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class heapsort {
int parent(int i) {
// 親を添字を返す
return (i - 1) / 2;
}
int left(int i) {
// 左の子の添字を返す
}
int right(int i) {
// 右の子の添字を返す
}
void MaxHeapify(int[] a, int i, int heapsize) {
// MaxHeapifyを実装(heap-sizeは引数で指定)
}
void BuildMaxHeap(int[] a) {
// BuildMaxHeapを実装
}
void Heapsort(int[] a) {
// Heapsortを実装(ソートすべき配列a[]を入力とし, a[]をソートする.)
}
public static void main(String[] args) {
int i, n;
Scanner input = new Scanner(System.in);
System.out.print("ソートする配列の要素数: ");
n = input.nextInt();
int[] a = new int [n];
for (i = 0; i < n; i++) {
System.out.print("a[" + i + "]を入力: ");
a[i] = input.nextInt();
}
System.out.print("ソート前の配列: ");
for (i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
heapsort hs = new heapsort();
hs.Heapsort(a);
System.out.print("ソート後の配列: ");
for (i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment