forked from destiny1020/algorithm_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortUtils.java
More file actions
90 lines (74 loc) · 1.55 KB
/
SortUtils.java
File metadata and controls
90 lines (74 loc) · 1.55 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
package chap2;
import java.lang.reflect.Array;
import chap2.heapsort.HeapNode;
public class SortUtils
{
@SuppressWarnings("unchecked")
public static <T> boolean less(Comparable<T> v, Comparable<T> w)
{
return v.compareTo((T) w) < 0;
}
public static boolean less(int v, int w)
{
return v < w;
}
@SuppressWarnings("unchecked")
public static <T> boolean lessOrEqual(Comparable<T> v, Comparable<T> w)
{
int result = v.compareTo((T) w);
return result < 0 || result == 0;
}
public static boolean lessOrEqual(int v, int w)
{
return v <= w;
}
public static <T> void exch(Comparable<T>[] a, int i, int j)
{
Comparable<T> temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void exch(int[] a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
@SuppressWarnings("unchecked")
public static <T> boolean eq(Comparable<T> a, Comparable<T> b)
{
return (a.compareTo((T) b) == 0);
}
public static <T> void show(Comparable<T>[] a)
{
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
System.out.println();
}
public static <T> boolean isSorted(Comparable<T>[] a)
{
for (int i = 1; i < a.length; i++)
{
if (less(a[i], a[i - 1]))
{
return false;
}
}
return true;
}
// Index based: Check range [start, end)
@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean isHeapSortedReverse(HeapNode[] a, int start, int end)
{
for (int i = start + 1; i < end; i++)
{
if (less(a[i - 1].getKey(), a[i].getKey()))
{
return false;
}
}
return true;
}
}