• 交换排序


     

    首先排序分为四种: 

          交换排序: 包括冒泡排序,快速排序。

          选择排序: 包括直接选择排序,堆排序。

          插入排序: 包括直接插入排序,希尔排序。

          归并排序

          基数排序

    交换排序主要有两种:

    1:冒泡排序

    import java.util.Arrays;
    
    public class Main {
        public static void main(String[] args) throws InterruptedException {
            int[] array = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3};
    
            System.out.println("Before sort:");
            //ArrayUtils.printArray(array);
            BubbleSort(array);
            System.out.println(Arrays.toString(array));
    
        }
    
        public static void BubbleSort(int[] array){
            int temp;
            int len = array.length;
            for(int i=0;i<len-1;i++){  //外层循环:每循环一次就确定了一个相对最大元素
                for(int j=1;j<len-i;j++){  //内层循环:有i个元素已经排好,根据i确定本次的比较次数
                    if(array[j-1]>array[j]){  //如果前一位大于后一位,交换位置
                        temp = array[j-1];
                        array[j-1] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }
    }
    Before sort:
    [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    View Code

    2:快速排序法

    import java.util.Arrays;
    
    public class Main {
        public static void main(String[] args) throws InterruptedException {
            int[] array = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3};
            System.out.println(Arrays.toString(array));
            quickSort(array);
            System.out.println("=====================================");
            System.out.println(Arrays.toString(array));
        }
    
        //快速排序
        public static void quickSort(int[] array){
            recursiveQuickSort(array,0,array.length-1);
        }
    
        /**
         * 递归的快速排序
         *@param low  数组的最小下标
         *@param high  数组的最大下标
         */
        private static void recursiveQuickSort(int[] array,int low,int high){
            if(low>=high)
            {
                return;
            }
            else
            {
                int pivot = array[low];  //以第一个元素为基准
                int partition =partition(array,low,high,pivot);  //对数组进行划分,比pivot小的元素在低位段,比pivot大的元素在高位段
    
                System.out.println(pivot);
                display(array);
    
                recursiveQuickSort(array,low,partition-1);  //对划分后的低位段进行快速排序
                recursiveQuickSort(array,partition+1,high);  //对划分后的高位段进行快速排序
            }
        }
    
        /**
         * 以pivot为基准对下标low到high的数组进行划分
         *@param low 数组段的最小下标
         *@param high 数组段的最大下标
         *@param pivot 划分的基准元素
         *@return 划分完成后基准元素所在位置的下标
         */
        private static int partition(int[] array,int low,int high,int pivot){
    
            while(low<high){
    
                while(low<high &&array[high]>=pivot){  //从右端开始扫描,定位到第一个比pivot小的元素
                    high--;
                }
                swap(array,low,high);
    
                while(low<high &&array[low]<=pivot){  //从左端开始扫描,定位到第一个比pivot大的元素
                    low++;
                }
                swap(array,low,high);
    
            }
            return low;
    
        }
        /**
         * 交换数组中两个元素的数据
         *@param low 欲交换元素的低位下标
         *@param high 欲交换元素的高位下标
         */
        private static void swap(int[] array,int low,int high){
            int temp = array[high];
            array[high] = array[low];
            array[low] = temp;
        }
    
        private static void display(int[] array){
            System.out.println(Arrays.toString(array));
        }
    }
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3]
    9
    [-3, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, 9]
    -3
    [-3, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, 9]
    8
    [-3, -2, 7, 6, 5, 4, 3, 2, 1, 0, -1, 8, 9]
    -2
    [-3, -2, 7, 6, 5, 4, 3, 2, 1, 0, -1, 8, 9]
    7
    [-3, -2, -1, 6, 5, 4, 3, 2, 1, 0, 7, 8, 9]
    -1
    [-3, -2, -1, 6, 5, 4, 3, 2, 1, 0, 7, 8, 9]
    6
    [-3, -2, -1, 0, 5, 4, 3, 2, 1, 6, 7, 8, 9]
    0
    [-3, -2, -1, 0, 5, 4, 3, 2, 1, 6, 7, 8, 9]
    5
    [-3, -2, -1, 0, 1, 4, 3, 2, 5, 6, 7, 8, 9]
    1
    [-3, -2, -1, 0, 1, 4, 3, 2, 5, 6, 7, 8, 9]
    4
    [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    2
    [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    =====================================
    [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    View Code
     public static void main(String[] args) throws InterruptedException {
            int[] array = new int[100000];
            Random random = new Random();
            for (int i = 0; i < array.length; i++) {
                array[i] = Math.abs(random.nextInt()) % 10000000;
            }
            int[] array2=array.clone();
            long t1=System.nanoTime();
            Arrays.sort(array);
            System.out.println(System.nanoTime()-t1);
    
            long t2=System.nanoTime();
            quickSort(array2);
            System.out.println(System.nanoTime()-t2);
        }

    跟系统的进行比较

    51746258
    19995678
    View Code

    还是系统比较快,但是数据比较少,比如10条的时候发现自己的比较快。

    快速排序法:自己的总结就是从数组中选择一个数当成基准数,然后在数组一次遍历的时候,将基准点移动到它正确排序的位置,并且左边的都比它小,右边的都比它大,然后分别递归左侧跟右侧的数据。

    1.将第一个数57作为基准点,然后从右边往左边查找比基准点57小的数,交换位置

    2.从左边往右边查找比基准点57大的数,交换位置

    来回往复,一直到基准点57找不到可以交换的元素

    这就是数组的第一次循环,然后将基准点左边的,右边的都按上面的循环。。。。。。。

    又一个版本:

    import java.util.Arrays;
    import java.util.Random;
    
    public class Main {
        public static void main(String[] args) throws InterruptedException {
            int[] array = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3};
            System.out.println(Arrays.toString(array));
            quickSort(array,0,array.length-1);
            System.out.println("=====================================");
            System.out.println(Arrays.toString(array));
        }
    
        public static void quickSort(int[] numbers, int start, int end) {
            if (start < end) {
                int base = numbers[start]; // 选定的基准值(第一个数值作为基准值)
                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

    http://blog.csdn.net/u012152619/article/category/2694319

    http://www.cnblogs.com/hexiaochun/archive/2012/09/03/2668324.html

    http://www.cnblogs.com/sevenyuan/archive/2009/12/04/1616897.html

    http://blog.csdn.net/happy_wu/article/details/51841244

    查找算法:

    http://blog.csdn.net/why_still_confused/article/details/51295909

     

  • 相关阅读:
    IO基础
    集合框架
    数据结构基础
    进程和线程
    matlab绘制三维图形
    matlab figure 窗口最大化
    Matlab中的fread函数
    matlab中fopen 和 fprintf函数总结
    matlab中findstr,strfind,strcmp,strncmp区别与联系
    matlab取消和添加注释以及一些快捷键
  • 原文地址:https://www.cnblogs.com/hongdada/p/6155833.html
Copyright © 2020-2023  润新知