美文网首页
【算法】快排

【算法】快排

作者: 浅浅星空 | 来源:发表于2019-02-27 08:55 被阅读4次

    1.时间复杂度

    image.png

    2.快排

    public class QuickSortTest {
    
        public static void main(String[] args) {
            int[] a = {2, 34, 56, 7, 9, 12, 33};
            quickSort(a, 0, a.length - 1);
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
        }
    
        public static void quickSort(int[] a, int left, int right) {
            int i = left;
            int j = right;
            if (i >= j) return;
            int temp = a[left];
    
            while (i < j) {
                while (i < j && temp >= a[i]) {
                    i++;
                }
                while (i < j && temp <= a[j]) {
                    j--;
                }
                if (i < j) {
                    int m = a[i];
                    a[i] = a[j];
                    a[j] = m;
                }
            }
            a[left] = a[i];
            a[i] = temp;
    
            quickSort(a, left, i - 1);
            quickSort(a, i + 1, right);
        }
    }
    

    相关文章

      网友评论

          本文标题:【算法】快排

          本文链接:https://www.haomeiwen.com/subject/voxxuqtx.html