排序

作者: whiteList | 来源:发表于2017-09-20 20:14 被阅读0次
        public static void main(String[] args) {
            int[] a ={1,2,3,4,5,-8,5,3};
            // sortOne(a);
            // sortTwo(a);
            // sortThree(a);
            // sortFour(a,0,a.length-1);
        }
        /**
         * 排序算法:冒泡排序
         */
        public static void sortOne(int a[]){
            System.out.println("one");
            int length = a.length;
            for (int i = 0; i <length-1 ; i++) {
                for (int j = 0; j <a.length-1-i ; j++) {
                    if(a[j]>a[j+1]){
                        swap(a,j,j+1);
                    }
                }
            }
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]+ " ");
            }
            System.out.println();
        }
    
    
        /**
         *排序算法:选择排序
         */
        public static void sortTwo(int a[]){
            System.out.println("rwo:");
            int temp,max;
    
            for (int i = 0; i < a.length-1; i++) {
                max = a[i];
                temp =i;
                for (int j = i+1; j < a.length; j++) {
                    if(a[j]>max){
                        max = a[j];
                        temp =j;
    
                    }
                }
                swap(a,i,temp);
            }
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]+ " ");
            }
            System.out.println();
        }
    
        /**
         * 排序算法:插入排序
         */
        public static void sortThree(int a[]){
            System.out.println("three:");
    
            for (int i = 1; i < a.length; i++) {
                int max = a[i];
                int temp =i;
                while (temp>0&&max>a[temp-1]){
                    a[temp] = a[temp-1];
                    temp--;
                }
                a[temp] =max;
            }
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]+ " ");
            }
            System.out.println();
        }
    
        /**
         * 排序算法:快速排序
         *
         */
        public static void sortFour(int a[],int left,int right){
    
            if(left<=right){
                int index = left; //index相当于中间值得索引
                for(int temp=left+1;temp<=right;temp++){
                    if(a[temp]<=a[left]){
                        swap(a,temp,++index);
                    }
                }
                swap(a,left,index);
                sortFour(a,left,index-1);
                sortFour(a,index+1,right);
            }
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]+ " ");
            }
            System.out.println();
    
    
        }
        /**
         * 交换数据
         */
        public static void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }```

    相关文章

      网友评论

          本文标题:排序

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