• Java四种排序:冒泡,选择,插入,二分(折半插入)


    四种排序:冒泡,选择,插入,二分(折半插入)

    public class Test{
    //    public static void main(String[] args) {
    //        Test t=new Test();
            public static void bubbleSort(int[] source){
                // 交换类排序思想: 两两比较待排序的关键字,发现记录相反则交换,直到没有反序的记录。
                for(int i = source.length - 1; i > 0; i--){
                    for(int j = 0; j < i; j++){
                        if(source[j] > source[j + 1]){
                            swap(source, j, j+1);
                        }
                    }
                }
            }
    
            
            public static void selectSort(int[] source){
                // 选择类排序思想:首先在未排序的序列中找到最小元素,存放到排序序列的起始位置,
                // 然后再从剩余未排序的元素中找到下一个最小元素,放到已排序序列的末尾。
                for (int i = 0; i < source.length; i++){
                    for (int j = i+1; j < source.length; j++){
                        if (source[j] > source[i]){
                            swap(source, i, j);
                        }
                    }
                }
                
            }
            // 从第一个元素开始,该元素可以认为已经被元素
            // 取出下一个元素,在已经拍序的元素中从后往前扫描,如果该元素大于新一个,则将该元素移到下一个
            public static void insertSort(int[] source){
                for (int i = 1; i < source.length; i++){
                    for (int j = i ; (j > 0) && (source[j] < source[j - 1]); j--){
                        swap(source, j, j-1);
                    }
                    
                }
            }
            public static void halfSort(int[] source){
                // 二分查找(折半插入)排序思想: 对于第j个元素而言,前面j-1个元素已经有序。
                //在有序的队列里面先折半查找出待插入的位置,再统一后移。最后插入要插入的元素。
                int temp, low, high, mid;
                for (int i =1; i<10; i++){
                    temp = source[i];
                    low = 0;
                    high = i-1;
                    while (low <= high){
                        mid = (low + high) / 2;
                        if (source[mid] > temp) 
                            high = mid - 1;
                        else
                            low = mid + 1;
                    }
                    for (int j = i+1; j > high; j--){
                        source[j+1] = source[j];
                    }
                    source[high+1] = temp;
                }
            }
            
            private static void swap(int[] source, int x, int y){
                int temp = source[x];
                source[x] = source[y];
                source[y] = temp;
            }
    
            public static void main(String[] args){
                int[] a = {4, 2, 1, 3, 4, 6, 7, 8, 0};
                int i;
                bubbleSort(a);
                for (i = 0;i<a.length;i++){
                    System.out.printf("%d ", a[i]);
                }
            }
    }
    

      

      

  • 相关阅读:
    beautifulsoup的一些使用
    requests(爬虫常用)库的使用
    find a maximum product triplet in this array
    Minimum difference between two arrays
    [LeetCode] Binary Tree Upside Down
    n 刀切多少块pizza
    Biased Random Number Generator
    linked list焦点问题,面经里很多,考虑相交不相交,有环无环 + Find Leaves of Binary Tree (Java)
    replace string use another
    Union and Intersection of two sorted list
  • 原文地址:https://www.cnblogs.com/smuxiaolei/p/7731446.html
Copyright © 2020-2023  润新知