Skip to content

Commit 8ef083d

Browse files
🔥 created the implementation for the heap sort in java
1 parent d6c7ffd commit 8ef083d

File tree

2 files changed

+122
-50
lines changed

2 files changed

+122
-50
lines changed

HeapSort.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
public class HeapSort {
2+
public void sort(int arr[])
3+
{
4+
int N = arr.length;
5+
6+
// Build heap (rearrange array)
7+
for (int i = N / 2 - 1; i >= 0; i--)
8+
heapify(arr, N, i);
9+
10+
// One by one extract an element from heap
11+
for (int i = N - 1; i > 0; i--) {
12+
// Move current root to end
13+
int temp = arr[0];
14+
arr[0] = arr[i];
15+
arr[i] = temp;
16+
17+
// call max heapify on the reduced heap
18+
heapify(arr, i, 0);
19+
}
20+
}
21+
22+
// To heapify a subtree rooted with node i which is
23+
// an index in arr[]. n is size of heap
24+
void heapify(int arr[], int N, int i)
25+
{
26+
int largest = i; // Initialize largest as root
27+
int l = 2 * i + 1; // left = 2*i + 1
28+
int r = 2 * i + 2; // right = 2*i + 2
29+
30+
// If left child is larger than root
31+
if (l < N && arr[l] > arr[largest])
32+
largest = l;
33+
34+
// If right child is larger than largest so far
35+
if (r < N && arr[r] > arr[largest])
36+
largest = r;
37+
38+
// If largest is not root
39+
if (largest != i) {
40+
int swap = arr[i];
41+
arr[i] = arr[largest];
42+
arr[largest] = swap;
43+
44+
// Recursively heapify the affected sub-tree
45+
heapify(arr, N, largest);
46+
}
47+
}
48+
49+
/* A utility function to print array of size n */
50+
static void printArray(int arr[])
51+
{
52+
int N = arr.length;
53+
54+
for (int i = 0; i < N; ++i)
55+
System.out.print(arr[i] + " ");
56+
System.out.println();
57+
}
58+
59+
// Driver's code
60+
public static void main(String args[])
61+
{
62+
int arr[] = { 12, 11, 13, 5, 6, 7 };
63+
int N = arr.length;
64+
65+
// Function call
66+
HeapSort ob = new HeapSort();
67+
ob.sort(arr);
68+
69+
System.out.println("Sorted array is");
70+
printArray(arr);
71+
}
72+
}

main.java

+50-50
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
public class Main {
2-
public static void main(String[] args) {
3-
Main Obj = new Main();
4-
Obj.add(11);
5-
Obj.add(22);
6-
Obj.add(33);
7-
Obj.add(44);
8-
Obj.add(55);
9-
Obj.print();
10-
}
11-
public class Node{
12-
int element;
13-
Node next;
14-
public Node(int element) {
15-
this.element = element;
16-
}
17-
}
18-
19-
public Node head = null;
20-
public Node tail = null;
21-
22-
public void add(int element){
23-
Node newEle = new Node(element);
24-
if(head == null) {
25-
head = newEle;
26-
tail = newEle;
27-
newEle.next = head;
28-
}
29-
else {
30-
tail.next = newEle;
31-
tail = newEle;
32-
tail.next = head;
33-
}
34-
}
35-
36-
public void print() { //print function
37-
Node current = head;
38-
if(head == null) {
39-
System.out.println("List is empty");
40-
}
41-
else {
42-
System.out.println("Nodes of the circular linked list: ");
43-
do{
44-
System.out.print(" "+ current.element);
45-
current = current.next;
46-
}while(current != head);
47-
System.out.println();
48-
}
49-
}
50-
}
1+
public class Main {
2+
public static void main(String[] args) {
3+
Main Obj = new Main();
4+
Obj.add(11);
5+
Obj.add(22);
6+
Obj.add(33);
7+
Obj.add(44);
8+
Obj.add(55);
9+
Obj.print();
10+
}
11+
public class Node{
12+
int element;
13+
Node next;
14+
public Node(int element) {
15+
this.element = element;
16+
}
17+
}
18+
19+
public Node head = null;
20+
public Node tail = null;
21+
22+
public void add(int element){
23+
Node newEle = new Node(element);
24+
if(head == null) {
25+
head = newEle;
26+
tail = newEle;
27+
newEle.next = head;
28+
}
29+
else {
30+
tail.next = newEle;
31+
tail = newEle;
32+
tail.next = head;
33+
}
34+
}
35+
36+
public void print() { //print function
37+
Node current = head;
38+
if(head == null) {
39+
System.out.println("List is empty");
40+
}
41+
else {
42+
System.out.println("Nodes of the circular linked list: ");
43+
do{
44+
System.out.print(" "+ current.element);
45+
current = current.next;
46+
}while(current != head);
47+
System.out.println();
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)