参考文档

快排的递归实现&非递归实现:http://blog.csdn.net/mine_song/article/details/64121553

三种快排及四种优化方式:http://blog.csdn.net/hacker00011000/article/details/52176100

尾递归:https://www.cnblogs.com/babybluevino/p/3714022.html

迭代,循环,遍历,递归的区别:https://www.cnblogs.com/feichengwulai/articles/3642107.html

递归为什么效率差:http://blog.csdn.net/qq_33797186/article/details/50766050

 

原理

快速排序使用分治的思想,通过一趟排序将待排序列分割成两部分,其中一部分记录的关键字均比另一部分记录的关键字小。之后分别对这两部分记录继续进行排序,以达到整个序列有序的目的。

举个栗子

原数组:{3,7,2,9,1,4,6,8,10,5}
期望结果:{1,2,3,4,5,6,7,8,9,10}

排序(二)快速排序

递归实现:

 

public static void quickSort(int[] numbers, int start, int end) { 
        if (start < end) {   
            int base = numbers[end]; // 选定的基准值(最后一个数值作为基准值)   
            int temp; // 记录临时中间值   
            int i = start, j = end;   
            do {   
                while ((numbers[i] < base) && (i < end))   
                    i++;   
                while ((numbers[j] > base) && (j > start))   
                    j--;   
                if (i <= j) {   
                     temp = numbers[i];                         
                     numbers[i] = numbers[j];   
                    numbers[j] = temp;   
                    i++;   
                    j--;   
                }  
            } while (i <= j);   
            
            if (start < j)   
                quickSort(numbers, start, j);   
            if (end > i)   
                quickSort(numbers, i, end);   
        }   
    }
View Code

相关文章:

  • 2021-09-02
  • 2021-11-23
  • 2022-02-04
  • 2021-07-06
  • 2021-08-17
猜你喜欢
  • 2021-08-28
  • 2021-12-29
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2021-06-20
相关资源
相似解决方案