7777 * @param arr
7878 * @return arr
7979 */
80- public static int [] BubbleSort (int [] arr) {
80+ public static int [] bubbleSort (int [] arr) {
8181 for (int i = 1 ; i < arr. length; i++ ) {
8282 // Set a flag, if true, that means the loop has not been swapped,
8383 // that is, the sequence has been ordered, the sorting has been completed.
@@ -130,7 +130,7 @@ public static int[] BubbleSort(int[] arr) {
130130 * @param arr
131131 * @return arr
132132 */
133- public static int [] SelectionSort (int [] arr) {
133+ public static int [] selectionSort (int [] arr) {
134134 for (int i = 0 ; i < arr. length - 1 ; i++ ) {
135135 int minIndex = i;
136136 for (int j = i + 1 ; j < arr. length; j++ ) {
@@ -184,7 +184,7 @@ public static int[] SelectionSort(int[] arr) {
184184 * @param arr
185185 * @return arr
186186 */
187- public static int [] InsertionSort (int [] arr) {
187+ public static int [] insertionSort (int [] arr) {
188188 for (int i = 1 ; i < arr. length; i++ ) {
189189 int preIndex = i - 1 ;
190190 int current = arr[i];
@@ -234,7 +234,7 @@ public static int[] InsertionSort(int[] arr) {
234234 * @param arr
235235 * @return arr
236236 */
237- public static int [] ShellSort (int [] arr) {
237+ public static int [] shellSort (int [] arr) {
238238 int n = arr. length;
239239 int gap = n / 2 ;
240240 while (gap > 0 ) {
@@ -291,14 +291,14 @@ public static int[] ShellSort(int[] arr) {
291291 * @param arr
292292 * @return arr
293293 */
294- public static int [] MergeSort (int [] arr) {
294+ public static int [] mergeSort (int [] arr) {
295295 if (arr. length <= 1 ) {
296296 return arr;
297297 }
298298 int middle = arr. length / 2 ;
299299 int [] arr_1 = Arrays . copyOfRange(arr, 0 , middle);
300300 int [] arr_2 = Arrays . copyOfRange(arr, middle, arr. length);
301- return Merge(MergeSort (arr_1), MergeSort (arr_2));
301+ return merge(mergeSort (arr_1), mergeSort (arr_2));
302302}
303303
304304/**
@@ -308,7 +308,7 @@ public static int[] MergeSort(int[] arr) {
308308 * @param arr_2
309309 * @return sorted_arr
310310 */
311- public static int [] Merge (int [] arr_1, int [] arr_2) {
311+ public static int [] merge (int [] arr_1, int [] arr_2) {
312312 int [] sorted_arr = new int [arr_1. length + arr_2. length];
313313 int idx = 0 , idx_1 = 0 , idx_2 = 0 ;
314314 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) {
364364
365365### 代码实现
366366
367- ``` java
368- /**
369- * Swap the two elements of an array
370- * @param array
371- * @param i
372- * @param j
373- */
374- private static void swap(int [] arr, int i, int j) {
375- int tmp = arr[i];
376- arr[i] = arr[j];
377- arr[j] = tmp;
378- }
367+ > 来源:[ 使用 Java 实现快速排序(详解)] ( https://segmentfault.com/a/1190000040022056 )
379368
380- /**
381- * Partition function
382- * @param arr
383- * @param left
384- * @param right
385- * @return small_idx
386- */
387- private static int Partition(int [] arr, int left, int right) {
388- if (left == right) {
389- return left;
390- }
391- // random pivot
392- int pivot = (int ) (left + Math . random() * (right - left + 1 ));
393- swap(arr, pivot, right);
394- int small_idx = left;
395- for (int i = small_idx; i < right; i++ ) {
396- if (arr[i] < arr[right]) {
397- swap(arr, i, small_idx);
398- small_idx++ ;
369+ ``` java
370+ public static int partition(int [] array, int low, int high) {
371+ int pivot = array[high];
372+ int pointer = low;
373+ for (int i = low; i < high; i++ ) {
374+ if (array[i] <= pivot) {
375+ int temp = array[i];
376+ array[i] = array[pointer];
377+ array[pointer] = temp;
378+ pointer++ ;
399379 }
380+ System . out. println(Arrays . toString(array));
400381 }
401- swap(arr, small_idx, right);
402- return small_idx;
382+ int temp = array[pointer];
383+ array[pointer] = array[high];
384+ array[high] = temp;
385+ return pointer;
403386}
404-
405- /**
406- * Quick sort function
407- * @param arr
408- * @param left
409- * @param right
410- * @return arr
411- */
412- public static int [] QuickSort(int [] arr, int left, int right) {
413- if (left < right) {
414- int pivotIndex = Partition(arr, left, right);
415- sort(arr, left, pivotIndex - 1 );
416- sort(arr, pivotIndex + 1 , right);
387+ public static void quickSort(int [] array, int low, int high) {
388+ if (low < high) {
389+ int position = partition(array, low, high);
390+ quickSort(array, low, position - 1 );
391+ quickSort(array, position + 1 , high);
417392 }
418- return arr;
419393}
420394```
421395
@@ -493,7 +467,7 @@ private static void heapify(int[] arr, int i) {
493467 * @param arr
494468 * @return
495469 */
496- public static int [] HeapSort (int [] arr) {
470+ public static int [] heapSort (int [] arr) {
497471 // index at the end of the heap
498472 heapLen = arr. length;
499473 // build MaxHeap
@@ -561,7 +535,7 @@ private static int[] getMinAndMax(int[] arr) {
561535 * @param arr
562536 * @return
563537 */
564- public static int [] CountingSort (int [] arr) {
538+ public static int [] countingSort (int [] arr) {
565539 if (arr. length < 2 ) {
566540 return arr;
567541 }
@@ -640,7 +614,7 @@ private static int[] getMinAndMax(List<Integer> arr) {
640614 * @param arr
641615 * @return
642616 */
643- public static List<Integer > BucketSort (List<Integer > arr, int bucket_size) {
617+ public static List<Integer > bucketSort (List<Integer > arr, int bucket_size) {
644618 if (arr. size() < 2 || bucket_size == 0 ) {
645619 return arr;
646620 }
@@ -704,7 +678,7 @@ public static List<Integer> BucketSort(List<Integer> arr, int bucket_size) {
704678 * @param arr
705679 * @return
706680 */
707- public static int [] RadixSort (int [] arr) {
681+ public static int [] radixSort (int [] arr) {
708682 if (arr. length < 2 ) {
709683 return arr;
710684 }
@@ -755,8 +729,6 @@ public static int[] RadixSort(int[] arr) {
755729
756730## 参考文章
757731
758- https://www.cnblogs.com/guoyaohua/p/8600214.html
759-
760- https://en.wikipedia.org/wiki/Sorting_algorithm
761-
762- https://sort.hust.cc/
732+ - https://www.cnblogs.com/guoyaohua/p/8600214.html
733+ - https://en.wikipedia.org/wiki/Sorting_algorithm
734+ - https://sort.hust.cc/
0 commit comments