5种java排序算法汇总工具类

时间:2022-09-06 09:21:55

工具类简单明了地总结了java的快速排序,希尔排序,插入排序,堆排序,归并排序五种排序算法,代码中并没有对这几种排序算法的一个说明,关于思想部分希望在自行查阅相关说明,这里只是对这几种算法进行一个概括,以供大家使用。

?
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
140
141
142
143
144
145
146
147
148
149
150
151
public class Sort {
 public static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a) {
  insertionSort(a, 0, a.length - 1);
 }
 
 private static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a, int left, int right) {
  int j; // 记录第一个比tmp小的元素的后边一位的位置
 
  for (int p = left; p <= right; p++) {
   AnyType tmp = a[p];
   for (j = p; j > left && tmp.compareTo(a[j - 1]) < 0; j--) {
    a[j] = a[j - 1];
   }
   a[j] = tmp;
  }
 }
 
 public static <AnyType extends Comparable<? super AnyType>> void shellSort(AnyType[] arr) {
  int j;
 
  for (int gap = arr.length / 2; gap > 0; gap /= 2) {
   for (int i = gap; i < arr.length; i++) {
    AnyType tmp = arr[i];
    for (j = i; j >= gap && tmp.compareTo(arr[j - gap]) < 0; j -= gap) {
     arr[j] = arr[j - gap];
    }
    arr[j] = tmp;
   }
  }
 }
 
 private static int leftChild(int i) {
  return i * 2 + 1;
 }
 
 private static <AnyType extends Comparable<? super AnyType>> void perculateDown(AnyType[] arr, int i, int size) {
  AnyType tmp = arr[i];
 
  for (int child; (child = leftChild(i)) < size; i = child) {
   if (child != size - 1 && arr[child].compareTo(arr[child + 1]) < 0) {
    child++;
   }
   if (tmp.compareTo(arr[child]) < 0) {
    arr[i] = arr[child];
   } else {
    break;
   }
  }
  arr[i] = tmp;
 }
 
 public static <AnyType extends Comparable<? super AnyType>> void heapSort(AnyType[] arr) {
  for (int i = arr.length / 2; i >= 0; i--) {
   perculateDown(arr, i, arr.length);
  }
  for (int i = arr.length - 1; i >= 0; i--) {
   swapReferences(arr, 0, i);
   perculateDown(arr, 0, i);
  }
 }
 
 private static <AnyType extends Comparable<? super AnyType>> void swapReferences(AnyType[] arr, int i, int j) {
  AnyType tmp = arr[i];
  arr[i] = arr[j];
  arr[j] = tmp;
 }
 
 public static <AnyType extends Comparable<? super AnyType>> void mergeSort(AnyType[] arr) {
  AnyType[] tmp = ((AnyType[]) new Comparable[arr.length]);
  mergeSort(arr, 0, arr.length - 1, tmp);
 }
 
 private static <AnyType extends Comparable<? super AnyType>> void mergeSort(AnyType[] arr, int start, int end, AnyType[] tmp) {
  if (start < end) {
   int mid = (start + end) >> 1;
   mergeSort(arr, start, mid, tmp);
   mergeSort(arr, mid + 1, end, tmp);
   merge(arr, start, mid, end, tmp);
  }
 }
 
 private static <AnyType extends Comparable<? super AnyType>> void merge(AnyType[] arr, int start, int mid, int end, AnyType[] tmp) {
  int i = start, j = mid + 1, k = start;
  while (i <= mid && j <= end) {
   if (arr[i].compareTo(arr[j]) < 0) {
    tmp[k++] = arr[i++];
   } else {
    tmp[k++] = arr[j++];
   }
  }
 
  while (i <= mid) {
   tmp[k++] = arr[i++];
  }
 
  while (j <= end) {
   tmp[k++] = arr[j++];
  }
 
  for (int m = start; m <= end; m++) {
   arr[m] = tmp[m];
  }
 }
 
 public static <AnyType extends Comparable<? super AnyType>> void quickSort(AnyType[] arr) {
  quickSort(arr, 0, arr.length - 1);
 }
 
 private static <AnyType extends Comparable<? super AnyType>> void quickSort(AnyType[] arr, int left, int right) {
  if (left + LENGTH_DIFF <= right) {
 
   AnyType pivot = medium(arr, left, right);
 
   int i = left, j = right;
   while (true) {
    while (arr[++i].compareTo(pivot) < 0);
    while (arr[--j].compareTo(pivot) > 0);
 
    if (i < j) {
     swapReferences(arr, i, j);
    } else {
     break;
    }
   }
 
   swapReferences(arr, i, right);
   quickSort(arr, left, i - 1);
   quickSort(arr, i + 1, right);
  } else {
   insertionSort(arr, left, right);
  }
 }
 
 private static <AnyType extends Comparable<? super AnyType>> AnyType medium(AnyType[] arr, int left,
   int right) {
  int center = (left + right) / 2;
  if (arr[center].compareTo(arr[left]) < 0) {
   swapReferences(arr, center, left);
  }
  if (arr[left].compareTo(arr[right]) > 0) {
   swapReferences(arr, left, right);
  }
  if (arr[center].compareTo(arr[right]) < 0) {
   swapReferences(arr, center, right);
  }
   
  return arr[right];
 }
 
 private final static int LENGTH_DIFF = 20;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。