排序之那些令人凌乱的那些小程序

     排序之那些令人凌乱的那些小程序

     写排序写了三天了,程序看起来好像很简单,但是我还是写了三天,有一种智商被无情地碾压的感觉,怀疑人生了,我只有c语言的基础,基础也不是很好,学了一个多月的java,说实话,我有点吃力了,和周围的小伙伴相比较更是有着深深的挫败感......但是别人的信手拈来和流畅的思维也是一步步累积起来的,所以呀,最大的敌人永远是自己,不要被自己设防的心理逼到阴暗的墙角,就像第一次戴隐形眼镜一样,完全是自己吓唬自己.....同时我们也要提高自主学习的效率,不能什么都依赖老师,尽快把基础补上去。一句话概括就是:要做自己的主宰者也要看到与别人的差距!碎碎念就到此结束,下面进入正题:

      我只做了三种简单的排序:

      一:选择排序

            思路:规定一个最大值,然后不断的和下面的数进行比较,如果比它小就互换位置。

public class xuanze {
    public static void main(String[] args) {
        xuanze X = new xuanze();
        int[] A = { 50, 21, 32, 58, 40, 78, 24, 12, 6, 8,98,210 };
        X.sort(A);
        for (int i = 0; i < A.length; i++) {
            System.out.print(A[i] + "	");
        }
        System.out.println();
    }
    public void sort(int[] A) {
        for (int i = 0; i < A.length; i++) {
            int max = i;
            for (int j = i+1; j < A.length; j++) {
                /*如果要使运行的结果是顺序,可以写成:
                 * for (int j = 0; j < A.length; j++){}
                 */
                if (A[max] < A[j]) {
                    max = j;
                }
                //交换
                if (max != i) {
                    int temp = A[max];
                    A[max] = A[i];
                    A[i] = temp;
                }
                /*if (A[max] < A[j]) {
                    max = j;
                    int temp = A[max];
                    A[max] = A[i];
                    A[i] = temp;
                }
                 */
            }

        }

    }
}

      二:冒泡排序法

          思路:每次将相邻两个数比较,将小的调到前头。

public class maopao {
    public static void main(String[] args) {
        maopao m = new maopao();
        int[] a = { 50, 21, 32, 58, 40, 78, 24, 12, 6, 8, 98, 210 };
        m.mp(a);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "
");
        }
        System.out.println();
    }
    /**
     * 每次将相邻两个数比较,将小的调到前头。
     * 要比较j-1趟,每一趟比较(数的个数-1)次
     */
    public void mp(int[] a) {
        for(int j=0;j<a.length-1;j++){  //要比较多少趟,每一趟下来就会下沉一个其中最大的数
            for(int i=0;i<(a.length-1)-j;i++){ //每一趟要进行多少次比较
                if(a[i]>a[i+1]){//相邻两个数进行比较,如果前面的比后面的数大,就互换位置
                    int t=a[i];
                    a[i]=a[i+1];
                    a[i+1]=t;
                }
            }
        }
    }

  

三:快速排序法:

           思路:选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分。

 private static void quickSort(int[] a, int low, int high) {
            if(low<high){ //如果不加这个判断递归会无法退出导致堆栈溢出异常
                int d = sort(a,low,high);
                quickSort(a, 0, low-1);
                quickSort(a, low+1, high);//相当于 quickSort(a, 0, d-1); quickSort(a, d+1, high);
            }
        }
    public static int sort(int[] a,int low,int high) {
         int temp = a[low];//基准元素
            while(low<high){
                //找到比基准元素小的元素位置
                while(low<high && a[high]>=temp){
                    high--;                                                                               
                }
                a[low] = a[high]; 
                while(low<high && a[low]<=temp){
                    low++;
                }
                a[high] = a[low];
            }
            a[low] = temp;
            return low;
        }
  
原文地址:https://www.cnblogs.com/java-7/p/5657677.html