当前位置:网站首页>2022.02.13 - NC002. sort

2022.02.13 - NC002. sort

2022-07-06 08:21:00 A CAI continues to work hard

1. subject

 Insert picture description here

2. Ideas

(1) Quick sort

  • The most basic quick sort algorithm .

3. Code

public class Test {
    
    public static void main(String[] args) {
    
    }
}

class Solution {
    
    public int[] MySort(int[] arr) {
    
        sort(arr, 0, arr.length - 1);
        return arr;
    }

    private void sort(int[] arr, int left, int right) {
    
        if (left < right) {
    
            int low = left;
            int high = right;
            int pivot = arr[low];
            while (low < high) {
    
                while (low < high && arr[high] >= pivot) {
    
                    high--;
                }
                arr[low] = arr[high];
                while (low < high && arr[low] <= pivot) {
    
                    low++;
                }
                arr[high] = arr[low];
            }
            arr[low] = pivot;
            sort(arr, left, low - 1);
            sort(arr, low + 1, right);
        }
    }
}
原网站

版权声明
本文为[A CAI continues to work hard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131832390452.html