77
77
* @param arr
78
78
* @return arr
79
79
*/
80
- public static int [] BubbleSort (int [] arr) {
80
+ public static int [] bubbleSort (int [] arr) {
81
81
for (int i = 1 ; i < arr. length; i++ ) {
82
82
// Set a flag, if true, that means the loop has not been swapped,
83
83
// that is, the sequence has been ordered, the sorting has been completed.
@@ -130,7 +130,7 @@ public static int[] BubbleSort(int[] arr) {
130
130
* @param arr
131
131
* @return arr
132
132
*/
133
- public static int [] SelectionSort (int [] arr) {
133
+ public static int [] selectionSort (int [] arr) {
134
134
for (int i = 0 ; i < arr. length - 1 ; i++ ) {
135
135
int minIndex = i;
136
136
for (int j = i + 1 ; j < arr. length; j++ ) {
@@ -184,7 +184,7 @@ public static int[] SelectionSort(int[] arr) {
184
184
* @param arr
185
185
* @return arr
186
186
*/
187
- public static int [] InsertionSort (int [] arr) {
187
+ public static int [] insertionSort (int [] arr) {
188
188
for (int i = 1 ; i < arr. length; i++ ) {
189
189
int preIndex = i - 1 ;
190
190
int current = arr[i];
@@ -234,7 +234,7 @@ public static int[] InsertionSort(int[] arr) {
234
234
* @param arr
235
235
* @return arr
236
236
*/
237
- public static int [] ShellSort (int [] arr) {
237
+ public static int [] shellSort (int [] arr) {
238
238
int n = arr. length;
239
239
int gap = n / 2 ;
240
240
while (gap > 0 ) {
@@ -291,14 +291,14 @@ public static int[] ShellSort(int[] arr) {
291
291
* @param arr
292
292
* @return arr
293
293
*/
294
- public static int [] MergeSort (int [] arr) {
294
+ public static int [] mergeSort (int [] arr) {
295
295
if (arr. length <= 1 ) {
296
296
return arr;
297
297
}
298
298
int middle = arr. length / 2 ;
299
299
int [] arr_1 = Arrays . copyOfRange(arr, 0 , middle);
300
300
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));
302
302
}
303
303
304
304
/**
@@ -308,7 +308,7 @@ public static int[] MergeSort(int[] arr) {
308
308
* @param arr_2
309
309
* @return sorted_arr
310
310
*/
311
- public static int [] Merge (int [] arr_1, int [] arr_2) {
311
+ public static int [] merge (int [] arr_1, int [] arr_2) {
312
312
int [] sorted_arr = new int [arr_1. length + arr_2. length];
313
313
int idx = 0 , idx_1 = 0 , idx_2 = 0 ;
314
314
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) {
364
364
365
365
### 代码实现
366
366
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 )
379
368
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++ ;
399
379
}
380
+ System . out. println(Arrays . toString(array));
400
381
}
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;
403
386
}
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);
417
392
}
418
- return arr;
419
393
}
420
394
```
421
395
@@ -493,7 +467,7 @@ private static void heapify(int[] arr, int i) {
493
467
* @param arr
494
468
* @return
495
469
*/
496
- public static int [] HeapSort (int [] arr) {
470
+ public static int [] heapSort (int [] arr) {
497
471
// index at the end of the heap
498
472
heapLen = arr. length;
499
473
// build MaxHeap
@@ -561,7 +535,7 @@ private static int[] getMinAndMax(int[] arr) {
561
535
* @param arr
562
536
* @return
563
537
*/
564
- public static int [] CountingSort (int [] arr) {
538
+ public static int [] countingSort (int [] arr) {
565
539
if (arr. length < 2 ) {
566
540
return arr;
567
541
}
@@ -640,7 +614,7 @@ private static int[] getMinAndMax(List<Integer> arr) {
640
614
* @param arr
641
615
* @return
642
616
*/
643
- public static List<Integer > BucketSort (List<Integer > arr, int bucket_size) {
617
+ public static List<Integer > bucketSort (List<Integer > arr, int bucket_size) {
644
618
if (arr. size() < 2 || bucket_size == 0 ) {
645
619
return arr;
646
620
}
@@ -704,7 +678,7 @@ public static List<Integer> BucketSort(List<Integer> arr, int bucket_size) {
704
678
* @param arr
705
679
* @return
706
680
*/
707
- public static int [] RadixSort (int [] arr) {
681
+ public static int [] radixSort (int [] arr) {
708
682
if (arr. length < 2 ) {
709
683
return arr;
710
684
}
@@ -755,8 +729,6 @@ public static int[] RadixSort(int[] arr) {
755
729
756
730
## 参考文章
757
731
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