Skip to content

Commit 2d09aea

Browse files
authored
Merge pull request ronijpandey#20 from antrix190/master
Added heap sort
2 parents e35817e + a9b4f00 commit 2d09aea

5 files changed

Lines changed: 58 additions & 8 deletions

File tree

DigitalMax.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package javacodes;
2-
31
import java.util.*;
42

53
public class DigitalMax {

Dine.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
public class Dine {
55

6+
67
public static void main(String[] args) {
78

89
int x = 10;

HeapSort.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/**
3+
* Created by antrix190 on 22/10/2017
4+
*/
5+
public class HeapSort {
6+
/*
7+
* HeapSort is a comparison based sorting algorithms that uses a binary heap data structure. Its run time
8+
* complexity is O(nlogn).
9+
* */
10+
public void sort(int arr[]){
11+
12+
int n = arr.length;
13+
14+
//Create heap
15+
for (int i = n / 2 - 1; i >= 0; i--)
16+
heapify(arr, n, i);
17+
18+
for (int i=n-1; i>=0; i--){
19+
//Moving current root to the end.
20+
int temp = arr[0];
21+
arr[0] = arr[i];
22+
arr[i] = temp;
23+
24+
//call Max heapify on the remaining heap.
25+
heapify(arr, i, 0);
26+
}
27+
}
28+
29+
void heapify(int arr[], int n, int i){
30+
//init
31+
int largest = i;
32+
int l = 2*i + 1;
33+
int r = 2*i + 2;
34+
35+
//if left child is larger than root, largest =left.
36+
if (l < n && arr[l] > arr[largest])
37+
largest = l;
38+
//if right child is larger than largest, largest =right
39+
if (r < n && arr[r] > arr[largest])
40+
largest = r;
41+
42+
//if largest is not root.
43+
if (largest != i){
44+
45+
//rearrange the heap.
46+
int swap = arr[i];
47+
arr[i] = arr[largest];
48+
arr[largest] = swap;
49+
50+
//Recursively heapify.
51+
heapify(arr, n, largest);
52+
}
53+
}
54+
}

TicTacToe.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package javacodes;
21

32
import java.util.Scanner;
43

University.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
/**
2-
* This class represents University and contains it's child domain classes(Course, Student, Sem, Department and Institute)
3-
*/
4-
package javacodes;
5-
1+
/*
2+
this program is used to find minimum distance between two vertices while traversing all available vertices.
3+
*/
64
import java.util.*;
75

86
/**

0 commit comments

Comments
 (0)