Skip to content

Commit 2e487de

Browse files
committedAug 27, 2019
add bucket sort, heap sort and other algos
1 parent d455d91 commit 2e487de

File tree

12 files changed

+561
-16
lines changed

12 files changed

+561
-16
lines changed
 
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package codebase3.sorting.bucket;
2+
import java.util.ArrayList;
3+
import java.util.Collections;
4+
5+
public class BucketSort {
6+
int arr[];
7+
8+
9+
//Constructor
10+
public BucketSort(int arr[]) {
11+
this.arr = arr;
12+
}
13+
14+
15+
//Prints Array
16+
public void printArray() {
17+
int tmp = 0;
18+
for (int i = 0; i < arr.length; i++) {
19+
System.out.print(arr[i]+" ");
20+
tmp++;
21+
if(tmp == 20) {
22+
System.out.println();
23+
tmp = 0;
24+
}
25+
}
26+
}
27+
28+
29+
//Prints Buckets
30+
public void printBucket(ArrayList<Integer>[] buckets) {
31+
for(int i=0; i<buckets.length; i++) {
32+
System.out.println("\nBucket#" + i + " :");
33+
for (int j=0; j<buckets[i].size(); j++) {
34+
System.out.print(buckets[i].get(j)+" ");
35+
}
36+
}
37+
38+
}
39+
40+
//Sorting method
41+
public void bucketSort() {
42+
43+
//Create sqrt# of buckets, so that the distribution is even
44+
int numberOfBuckets = (int) Math.ceil(Math.sqrt(arr.length));
45+
int maxValue = Integer.MIN_VALUE;
46+
int minValue = Integer.MAX_VALUE;
47+
48+
49+
//Find the min and max value from the array
50+
for(int value: arr) {
51+
if(value < minValue) {
52+
minValue = value;
53+
}else if (value > maxValue) {
54+
maxValue = value;
55+
}
56+
}
57+
58+
59+
//Create an array of buckets
60+
ArrayList<Integer>[] buckets = new ArrayList[numberOfBuckets];
61+
62+
63+
//initializing empty buckets
64+
for(int i =0;i<buckets.length;i++) {
65+
buckets[i] = new ArrayList<Integer>();
66+
}
67+
68+
69+
for(int value: arr) {
70+
int bucketNumber = (int) Math.ceil ((value * numberOfBuckets) / maxValue);
71+
//System.out.println("bucketNumber: " + bucketNumber);
72+
buckets[bucketNumber-1].add(value);
73+
}
74+
75+
76+
System.out.println("\n\nPrinting buckets before Sorting...");
77+
printBucket(buckets);
78+
79+
80+
//Sort Buckets
81+
for(ArrayList<Integer> bucket: buckets) {
82+
Collections.sort(bucket);
83+
}
84+
85+
86+
System.out.println("\n\nPrinting buckets after Sorting...");
87+
printBucket(buckets);
88+
89+
90+
//concatenate buckets
91+
int index=0;
92+
for(ArrayList<Integer> bucket: buckets) {
93+
for(int value: bucket) {
94+
arr[index] = value;
95+
index++;
96+
}
97+
}
98+
}//end of method
99+
100+
}//end of class
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package codebase3.sorting.bucket;
2+
import java.util.Random;
3+
4+
public class BucketSortMain {
5+
public static void main(String[] args) {
6+
7+
int arr[] = new int[100];
8+
9+
//Generating 100 random numbers in the range of 0-100
10+
Random random = new Random();
11+
for(int i=0;i<100;i++) {
12+
arr[i] = random.nextInt(100)+100;
13+
}
14+
15+
16+
//Passing this array to BucketSort method
17+
BucketSort bs = new BucketSort(arr);
18+
System.out.println("Array before Sorting: ");
19+
bs.printArray();
20+
bs.bucketSort();
21+
22+
23+
System.out.println("\n\nArray after Sorting: ");
24+
bs.printArray();
25+
26+
}
27+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package codebase3.sorting.heap;
2+
3+
public class HeapByArray {
4+
int[] arr;
5+
int sizeOfTree;
6+
7+
8+
//Constructor
9+
public HeapByArray(int size) {
10+
//We are adding 1 here so that first cell of the array can be left blank all the time. This is eliminate problem of array starting from index 0.
11+
arr = new int[size+1];
12+
this.sizeOfTree = 0;
13+
System.out.println("Empty Heap has been created !");
14+
}//end of method
15+
16+
17+
public int sizeOfArray() {
18+
return arr.length;
19+
}
20+
21+
22+
public int sizeOfTree() {
23+
System.out.println("Size Of Tree: " + sizeOfTree);
24+
return sizeOfTree;
25+
}//end of method
26+
27+
28+
29+
public boolean isHeapEmpty() {
30+
if (sizeOfTree <= 0) {
31+
System.out.println("Tree is empty !");
32+
return true;
33+
}else {
34+
System.out.println("Tree is not empty !");
35+
return false;
36+
}
37+
}//end of method
38+
39+
40+
41+
public void deleteheap() {
42+
arr = null;
43+
System.out.println("Heap has been deleted !");
44+
}//end of method
45+
46+
47+
48+
//Insert a new value in Heap
49+
public void insertInHeap(int value) {
50+
//Doing +1 because sizeOfTree always points to the last occupied cell of the array
51+
System.out.println("Inserting " + value + " in Heap...");
52+
arr[sizeOfTree+1] = value;
53+
sizeOfTree++;
54+
HeapifyBottomToTop(sizeOfTree);
55+
System.out.println("Inserted " + value + " successfully in Heap !");
56+
levelOrder();
57+
}//end of method
58+
59+
60+
61+
// Peek into Heap
62+
public void peek() {
63+
if(sizeOfTree == 0) {
64+
System.out.println("Heap is empty !");
65+
}else {
66+
System.out.println("Head of the Heap is: " + arr[1]);
67+
}
68+
}//end of method
69+
70+
71+
72+
//Extract Head of Heap
73+
public int extractHeadOfHeap() {
74+
if(sizeOfTree == 0) {
75+
System.out.println("Heap is empty !");
76+
return -1;
77+
}else {
78+
System.out.println("Head of the Heap is: " + arr[1]);
79+
System.out.println("Extracting it now...");
80+
int extractedValue = arr[1];
81+
arr[1] = arr[sizeOfTree];
82+
sizeOfTree--;
83+
HeapifyTopToBottom(1);
84+
System.out.println("Successfully extracted value from Heap.");
85+
levelOrder();
86+
return extractedValue;
87+
}
88+
}//end of method
89+
90+
91+
92+
93+
public void HeapifyBottomToTop(int index) {
94+
int parent = index / 2;
95+
// We are at root of the tree. Hence no more Heapifying is required.
96+
if (index <= 1) {
97+
return;
98+
}
99+
// If Current value is smaller than its parent, then we need to swap
100+
if (arr[index] < arr[parent]) {
101+
int tmp = arr[index];
102+
arr[index] = arr[parent];
103+
arr[parent] = tmp;
104+
}
105+
HeapifyBottomToTop(parent);
106+
}//end of method
107+
108+
109+
110+
111+
public void HeapifyTopToBottom(int index) {
112+
int left = index*2;
113+
int right = (index*2)+1;
114+
int smallestChild = 0;
115+
116+
if (sizeOfTree < left) { //If there is no child of this node, then nothing to do. Just return.
117+
return;
118+
}else if (sizeOfTree == left) { //If there is only left child of this node, then do a comparison and return.
119+
if(arr[index] > arr[left]) {
120+
int tmp = arr[index];
121+
arr[index] = arr[left];
122+
arr[left] = tmp;
123+
}
124+
return;
125+
}else { //If both children are there
126+
if(arr[left] < arr[right]) { //Find out the smallest child
127+
smallestChild = left;
128+
}else {
129+
smallestChild = right;
130+
}
131+
if(arr[index] > arr[smallestChild]) { //If Parent is greater than smallest child, then swap
132+
int tmp = arr[index];
133+
arr[index] = arr[smallestChild];
134+
arr[smallestChild] = tmp;
135+
}
136+
}
137+
HeapifyTopToBottom(smallestChild);
138+
}//end of method
139+
140+
141+
142+
public void levelOrder() {
143+
System.out.println("Printing all the elements of this Heap...");// Printing from 1 because 0th cell is dummy
144+
for (int i = 1; i <= sizeOfTree; i++) {
145+
System.out.print(arr[i] + " ");
146+
}
147+
System.out.println("\n");
148+
}//end of method
149+
150+
151+
}//end of class
152+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package codebase3.sorting.heap;
2+
3+
public class HeapSort {
4+
int[] arr = null;
5+
6+
//Constructor
7+
public HeapSort(int[] arr) {
8+
this.arr = arr;
9+
}//end of method
10+
11+
12+
public void sort() {
13+
HeapByArray hba = new HeapByArray(arr.length); //We will reuse HeapByArray class to do sorting
14+
for(int i=0; i<arr.length;i++) { //Insert in Heap
15+
hba.insertInHeap(arr[i]);
16+
}
17+
for(int i=0; i<arr.length;i++) { //Extract from Heap and insert sorted data in current Array
18+
arr[i] = hba.extractHeadOfHeap();
19+
}
20+
}//end of method
21+
22+
23+
public void printArray() {
24+
for (int i = 0; i < arr.length; i++) {
25+
System.out.print(arr[i]+" ");
26+
}
27+
}//end of method
28+
29+
}//end of class
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package codebase3.sorting.heap;
2+
3+
public class HeapSortMain {
4+
5+
public static void main(String[] args) {
6+
7+
int arr[] = {10, 3, 2, 5, 8, 4, 3, 1, 2, 9, 7, 8};
8+
HeapSort hs = new HeapSort(arr);
9+
10+
System.out.println("User entered Array: ");
11+
hs.printArray();
12+
System.out.println("\n");
13+
14+
hs.sort();
15+
16+
System.out.println("\n\nAfter sorting: ");
17+
hs.printArray();
18+
}//end of method
19+
20+
}//end of class
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package codebase3.sorting.insertion;
2+
3+
public class InsertionSort {
4+
5+
static void insertionSort(int[] A) {
6+
for (int i = 1; i < A.length; i++) {
7+
int tmp = A[i], j = i;
8+
while (j > 0 && A[j - 1] > tmp) {
9+
A[j] = A[j - 1];
10+
j--;
11+
}
12+
A[j] = tmp;
13+
}//end of for loop
14+
}//end of method
15+
16+
17+
public static void printArray(int[] array) {
18+
for (int i = 0; i < array.length; i++) {
19+
System.out.print(array[i] + " ");
20+
}
21+
}//end of method
22+
23+
}//end of class
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package codebase3.sorting.insertion;
2+
3+
public class InsertionSortMain {
4+
public static void main(String[] args) {
5+
6+
int array[] = {10, 3, 2, 5, 8, 4, 3, 1, 2, 9, 7, 8};
7+
8+
System.out.println("User entered Array: ");
9+
InsertionSort.printArray(array);
10+
11+
long start = System.nanoTime();
12+
InsertionSort.insertionSort(array);
13+
long end = System.nanoTime();
14+
System.out.println("\n\nTime to execute this algo: " + (end-start));
15+
16+
System.out.println("\nAfter sorting: ");
17+
InsertionSort.printArray(array);
18+
}//end of method
19+
20+
}//end of class

0 commit comments

Comments
 (0)