-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheapsort.java
More file actions
176 lines (162 loc) · 4.53 KB
/
heapsort.java
File metadata and controls
176 lines (162 loc) · 4.53 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//max heap: insertion, deletion and heapsort
import java.util.*;
public class Heap {
int arr[];
int last_ind, size;
Heap(int size)
{
arr=new int[size];
last_ind=-1;
this.size=size;
}
public void heapifyUp()
{
int temp, check=last_ind;
while((check!=0)&&(arr[check]>arr[(check-1)/2]))
{
if(arr[check]>arr[(check-1)/2])
{
//swap
temp=arr[check];
arr[check]=arr[(check-1)/2];
arr[(check-1)/2]=temp;
check=(check-1)/2;
}
}
}
public void heapifyDown()
{
int temp, check=0;
while((check!=last_ind)&&((arr[check]<arr[2*check+1])||(arr[check]<arr[2*check+2]))&&(last_ind>2*check))
{
if((arr[check]<arr[2*check+1])||(arr[check]<arr[2*check+2]))
{
if(arr[2*check+1]>arr[2*check+2])
{
//swap
temp=arr[check];
arr[check]=arr[2*check+1];
arr[2*check+1]=temp;
check=2*check+1;
}
else
{
//swap
temp=arr[check];
arr[check]=arr[2*check+2];
arr[2*check+2]=temp;
check=2*check+2;
}
}
}
}
void insertion(int data)
{
if(last_ind<size-1)
{
last_ind++;
arr[last_ind]=data;
heapifyUp();
}
// else
// {
// n=2*n;
// puts("SIZE INCREASED.");
// printf("Size of array: %d\n", n);
// arr=realloc(arr, 2*sizeof(arr));
// last_ind++;
// arr[last_ind]=data;
// heapifyUp();
// }
}
void deletion()
{
if(last_ind>=0)
{
arr[0]=arr[last_ind];
last_ind--;
heapifyDown();
}
// else
// {
// printf("Heap is empty.\n");
// }
}
void heapsort()
{
int p[]=new int[last_ind+1];
int i=0,j;
long start2=System.nanoTime();
while(last_ind>=0)
{
p[i++]=arr[0];
arr[0]=arr[last_ind];
heapifyDown();
last_ind--;
}
long end2=System.nanoTime();
long duration2=end2-start2;
System.out.println("time duration "+duration2);
}
void display()
{
int i;
System.out.print("Heap: ");
for(i=0; i<=last_ind; i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rand=new Random();
try
{
int n=2000, op, data;
boolean exit=true;
Heap obj = new Heap(n);
while(exit==true)
{
System.out.println("Menu:\n1. Insertion\n2. Deletion\n3. Heap Print\n4. HeapSort\n5. Exit\nEnter Option: ");
op=sc.nextInt();
switch(op)
{
case 1:
long start=System.nanoTime();
for(int i=0;i<2000;i++)
{
int temp=rand.nextInt(2000);
obj.insertion(temp);
temp=0;
}
long end=System.nanoTime();
long duration=end-start;
System.out.println("Time duration "+duration/1000);
break;
case 2:
long start1=System.nanoTime();
obj.deletion();
long end1=System.nanoTime();
long duration1=end1-start1;
System.out.println("Time duration "+duration1);
break;
case 3:
obj.display();
break;
case 4:
obj.heapsort();
break;
case 5:
exit=false;
break;
default:
System.out.println("Wrong choice");
}
}
}
catch (Exception e) {
sc.close();
}
}
}