-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.java
More file actions
157 lines (125 loc) · 4.35 KB
/
Util.java
File metadata and controls
157 lines (125 loc) · 4.35 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
import java.util.Arrays;
/**
* Write a description of class Ultilities here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Util
{
public static void printArray(int[] array){
for(int i : array){
System.out.print(i + " ");
}
System.out.println();
}
// ###############
// SORT ALGORITHMS
// ###############
// Bubble sort
public static void bubbleSort(int[] array){
int n = array.length;
// outer loop(steps), start from 1
for (int i = 1; i < n; i++) {
// inner loop(elements(init 4) to compare) start from 0(index-based):
// i=1->j=3 i=2->j=2 -->> j<n-i
boolean swapped = false;
for (int j = 0; j < n - i; j++) {
// compare and swap j vs j+1
if (array[j] > array[j + 1]) {
int temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
// flag to exit when the array is sorted
swapped = true;
}
}
if(!swapped){
break;
}
}
}
// Heap Sort
// Step1: build heap (heapify) to n element with root node defined
private static void heapify(int[] array, int n, int root){
int largest = root;
int leftNode = root * 2 + 1;
int rightNode = root * 2 +2;
if(leftNode < n && array[leftNode] > array[largest]){
largest = leftNode;
}
if(rightNode < n && array[rightNode] > array[largest]){
largest = rightNode;
}
if(largest != root){
// swap root with greater child node
int temp = array[largest];
array[largest] = array[root];
array[root] =temp;
// recursive heapify the heap from largest node if it was damaged
heapify(array, n, largest);
}
}
// Step2: swap root with last node and rebuild
public static void heapSort(int[] arr){
// heapify from last possibility parent node to root (0th)
int n = arr.length;
for(int root = n/2 - 1; root >= 0; root--){
heapify(arr, n, root);
}
// swap root with last node and rebuild
for(int i = n-1; i > 0; i--){
int temp = arr[0];
arr[0]=arr[i];
arr[i]=temp;
heapify(arr, i, 0);
}
}
// Merge sort
// Step1: Incursively split the array ultil there is only one element in array parameter
public static int[] mergeSort(int[] array){
if(array.length <= 1){
return array;
}
// find mid to split the array
int mid = array.length / 2;
int[] leftArray = Arrays.copyOfRange(array, 0, mid);
int[] rightArray = Arrays.copyOfRange(array, mid, array.length);
// incursively split until there is only one element in each sub-arrays and merge
int[] sortedLeftArray = mergeSort(leftArray);
int[] sortedRightArray = mergeSort(rightArray);
// merge two halves left and right
return merge(sortedLeftArray, sortedRightArray);
}
// Step2: compare and merge 2 sorted arrays
private static int[] merge(int[] arr1, int[] arr2){
int[] result = new int[arr1.length + arr2.length];
// compare from the first element of each arrays ultil i or j out of parent array range
// i, j: index of compare elements of arr1 and arr2 start from 0
// k : index of smaller element to assign in the merged array
int i,j,k;
i = j = k = 0;
while(i < arr1.length && j < arr2.length){
if(arr2[j] < arr1[i]){
result[k] = arr2[j];
j++;
}else{
result[k] = arr1[i];
i++;
}
k++;
}
// copy remain elements (of one of 2 subarrays) to end of merged array
while(i < arr1.length){
result[k] = arr1[i];
i++;
k++;
}
while(j < arr2.length){
result[k] = arr2[j];
j++;
k++;
}
return result;
}
}