-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHeapsortMethods.java
More file actions
105 lines (87 loc) · 2.72 KB
/
HeapsortMethods.java
File metadata and controls
105 lines (87 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//Name: Kritika Partha
//Date: 2/10/20
import java.text.DecimalFormat;
public class HeapsortMethods
{
public static final int SIZE = 10;
private static DecimalFormat df2 = new DecimalFormat("#.##");
public static void main(String[] args)
{
//Part 1: Given a heap, sort it. Do this part first.
double heap[] = {-1,99,80,85,17,30,84,2,16,1};
display(heap);
sort(heap);
display(heap);
//Part 2: Generate 10 random numbers, make a heap, sort it.
// double[] heap = new double[SIZE];
// createRandom(heap);
// display(heap);
// makeHeap(heap);
// display(heap);
// sort(heap);
// display(heap);
}
//******* Part 1 ******************************************
public static void display(double[] array)
{
for(int k = 1; k < array.length; k++)
System.out.print(array[k] + " ");
System.out.println("\n");
}
public static void sort(double[] array)
{
int n = array.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapDown(array, n);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// call max heapify on the reduced heap
heapDown(array, i);
}
}
public static void swap(double[] array, int a, int b)
{
double t = array[a];
array[a] = array[b];
array[b] = t;
}
public static void heapDown(double[] array, int size)
{
int largest = size; // Initialize largest as root
int l = 2*size + 1; // left = 2*i + 1
int r = 2*size + 2; // right = 2*i + 2
// If left child is larger than root
if (l < size && array[l] > array[largest])
largest = l;
// If right child is larger than largest so far
if (r < size && array[r] > array[largest])
largest = r;
// If largest is not root
if (largest != size)
{
array[size] = array[largest];
// Recursively heapify the affected sub-tree
heapDown(array, size);
}
}
// ****** Part 2 *******************************************
public static void makeHeap(double[] array)
{
int size = 0;
int length = array.length;
for (int i = 0; i < length; i++) {
double e = array[i];
}
}
//Generate 100 random numbers (between 1 and 100, formatted to 2 decimal places)
public static void createRandom(double[] array)
{
int[] list = new int[100];
for (int i=0; i<100; i++){
int n = (int)(Math.random()*100 + 1);
list[i] = n;
}
}
}