mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-06-16 18:10:13 +08:00
Update 10-classical-sorting-algorithms.md
This commit is contained in:
parent
fa09190dd3
commit
cfd2323962
@ -77,7 +77,7 @@ tag:
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return arr
|
* @return arr
|
||||||
*/
|
*/
|
||||||
public static int[] BubbleSort(int[] arr) {
|
public static int[] bubbleSort(int[] arr) {
|
||||||
for (int i = 1; i < arr.length; i++) {
|
for (int i = 1; i < arr.length; i++) {
|
||||||
// Set a flag, if true, that means the loop has not been swapped,
|
// Set a flag, if true, that means the loop has not been swapped,
|
||||||
// that is, the sequence has been ordered, the sorting has been completed.
|
// that is, the sequence has been ordered, the sorting has been completed.
|
||||||
@ -130,7 +130,7 @@ public static int[] BubbleSort(int[] arr) {
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return arr
|
* @return arr
|
||||||
*/
|
*/
|
||||||
public static int[] SelectionSort(int[] arr) {
|
public static int[] selectionSort(int[] arr) {
|
||||||
for (int i = 0; i < arr.length - 1; i++) {
|
for (int i = 0; i < arr.length - 1; i++) {
|
||||||
int minIndex = i;
|
int minIndex = i;
|
||||||
for (int j = i + 1; j < arr.length; j++) {
|
for (int j = i + 1; j < arr.length; j++) {
|
||||||
@ -184,7 +184,7 @@ public static int[] SelectionSort(int[] arr) {
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return arr
|
* @return arr
|
||||||
*/
|
*/
|
||||||
public static int[] InsertionSort(int[] arr) {
|
public static int[] insertionSort(int[] arr) {
|
||||||
for (int i = 1; i < arr.length; i++) {
|
for (int i = 1; i < arr.length; i++) {
|
||||||
int preIndex = i - 1;
|
int preIndex = i - 1;
|
||||||
int current = arr[i];
|
int current = arr[i];
|
||||||
@ -234,7 +234,7 @@ public static int[] InsertionSort(int[] arr) {
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return arr
|
* @return arr
|
||||||
*/
|
*/
|
||||||
public static int[] ShellSort(int[] arr) {
|
public static int[] shellSort(int[] arr) {
|
||||||
int n = arr.length;
|
int n = arr.length;
|
||||||
int gap = n / 2;
|
int gap = n / 2;
|
||||||
while (gap > 0) {
|
while (gap > 0) {
|
||||||
@ -291,14 +291,14 @@ public static int[] ShellSort(int[] arr) {
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return arr
|
* @return arr
|
||||||
*/
|
*/
|
||||||
public static int[] MergeSort(int[] arr) {
|
public static int[] mergeSort(int[] arr) {
|
||||||
if (arr.length <= 1) {
|
if (arr.length <= 1) {
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
int middle = arr.length / 2;
|
int middle = arr.length / 2;
|
||||||
int[] arr_1 = Arrays.copyOfRange(arr, 0, middle);
|
int[] arr_1 = Arrays.copyOfRange(arr, 0, middle);
|
||||||
int[] arr_2 = Arrays.copyOfRange(arr, middle, arr.length);
|
int[] arr_2 = Arrays.copyOfRange(arr, middle, arr.length);
|
||||||
return Merge(MergeSort(arr_1), MergeSort(arr_2));
|
return merge(mergeSort(arr_1), mergeSort(arr_2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -308,7 +308,7 @@ public static int[] MergeSort(int[] arr) {
|
|||||||
* @param arr_2
|
* @param arr_2
|
||||||
* @return sorted_arr
|
* @return sorted_arr
|
||||||
*/
|
*/
|
||||||
public static int[] Merge(int[] arr_1, int[] arr_2) {
|
public static int[] merge(int[] arr_1, int[] arr_2) {
|
||||||
int[] sorted_arr = new int[arr_1.length + arr_2.length];
|
int[] sorted_arr = new int[arr_1.length + arr_2.length];
|
||||||
int idx = 0, idx_1 = 0, idx_2 = 0;
|
int idx = 0, idx_1 = 0, idx_2 = 0;
|
||||||
while (idx_1 < arr_1.length && idx_2 < arr_2.length) {
|
while (idx_1 < arr_1.length && idx_2 < arr_2.length) {
|
||||||
@ -364,58 +364,32 @@ public static int[] Merge(int[] arr_1, int[] arr_2) {
|
|||||||
|
|
||||||
### 代码实现
|
### 代码实现
|
||||||
|
|
||||||
|
> 来源:[使用 Java 实现快速排序(详解)](https://segmentfault.com/a/1190000040022056)
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/**
|
public static int partition(int[] array, int low, int high) {
|
||||||
* Swap the two elements of an array
|
int pivot = array[high];
|
||||||
* @param array
|
int pointer = low;
|
||||||
* @param i
|
for (int i = low; i < high; i++) {
|
||||||
* @param j
|
if (array[i] <= pivot) {
|
||||||
*/
|
int temp = array[i];
|
||||||
private static void swap(int[] arr, int i, int j) {
|
array[i] = array[pointer];
|
||||||
int tmp = arr[i];
|
array[pointer] = temp;
|
||||||
arr[i] = arr[j];
|
pointer++;
|
||||||
arr[j] = tmp;
|
|
||||||
}
|
}
|
||||||
|
System.out.println(Arrays.toString(array));
|
||||||
/**
|
|
||||||
* Partition function
|
|
||||||
* @param arr
|
|
||||||
* @param left
|
|
||||||
* @param right
|
|
||||||
* @return small_idx
|
|
||||||
*/
|
|
||||||
private static int Partition(int[] arr, int left, int right) {
|
|
||||||
if (left == right) {
|
|
||||||
return left;
|
|
||||||
}
|
}
|
||||||
// random pivot
|
int temp = array[pointer];
|
||||||
int pivot = (int) (left + Math.random() * (right - left + 1));
|
array[pointer] = array[high];
|
||||||
swap(arr, pivot, right);
|
array[high] = temp;
|
||||||
int small_idx = left;
|
return pointer;
|
||||||
for (int i = small_idx; i < right; i++) {
|
|
||||||
if (arr[i] < arr[right]) {
|
|
||||||
swap(arr, i, small_idx);
|
|
||||||
small_idx++;
|
|
||||||
}
|
}
|
||||||
|
public static void quickSort(int[] array, int low, int high) {
|
||||||
|
if (low < high) {
|
||||||
|
int position = partition(array, low, high);
|
||||||
|
quickSort(array, low, position - 1);
|
||||||
|
quickSort(array, position + 1, high);
|
||||||
}
|
}
|
||||||
swap(arr, small_idx, right);
|
|
||||||
return small_idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Quick sort function
|
|
||||||
* @param arr
|
|
||||||
* @param left
|
|
||||||
* @param right
|
|
||||||
* @return arr
|
|
||||||
*/
|
|
||||||
public static int[] QuickSort(int[] arr, int left, int right) {
|
|
||||||
if (left < right) {
|
|
||||||
int pivotIndex = Partition(arr, left, right);
|
|
||||||
sort(arr, left, pivotIndex - 1);
|
|
||||||
sort(arr, pivotIndex + 1, right);
|
|
||||||
}
|
|
||||||
return arr;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -493,7 +467,7 @@ private static void heapify(int[] arr, int i) {
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static int[] HeapSort(int[] arr) {
|
public static int[] heapSort(int[] arr) {
|
||||||
// index at the end of the heap
|
// index at the end of the heap
|
||||||
heapLen = arr.length;
|
heapLen = arr.length;
|
||||||
// build MaxHeap
|
// build MaxHeap
|
||||||
@ -561,7 +535,7 @@ private static int[] getMinAndMax(int[] arr) {
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static int[] CountingSort(int[] arr) {
|
public static int[] countingSort(int[] arr) {
|
||||||
if (arr.length < 2) {
|
if (arr.length < 2) {
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
@ -640,7 +614,7 @@ private static int[] getMinAndMax(List<Integer> arr) {
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static List<Integer> BucketSort(List<Integer> arr, int bucket_size) {
|
public static List<Integer> bucketSort(List<Integer> arr, int bucket_size) {
|
||||||
if (arr.size() < 2 || bucket_size == 0) {
|
if (arr.size() < 2 || bucket_size == 0) {
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
@ -704,7 +678,7 @@ public static List<Integer> BucketSort(List<Integer> arr, int bucket_size) {
|
|||||||
* @param arr
|
* @param arr
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static int[] RadixSort(int[] arr) {
|
public static int[] radixSort(int[] arr) {
|
||||||
if (arr.length < 2) {
|
if (arr.length < 2) {
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
@ -755,8 +729,6 @@ public static int[] RadixSort(int[] arr) {
|
|||||||
|
|
||||||
## 参考文章
|
## 参考文章
|
||||||
|
|
||||||
https://www.cnblogs.com/guoyaohua/p/8600214.html
|
- https://www.cnblogs.com/guoyaohua/p/8600214.html
|
||||||
|
- https://en.wikipedia.org/wiki/Sorting_algorithm
|
||||||
https://en.wikipedia.org/wiki/Sorting_algorithm
|
- https://sort.hust.cc/
|
||||||
|
|
||||||
https://sort.hust.cc/
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user