• 排序


    1 冒泡排序(不想多说)

    public class BubbleSort {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int arr[]={1,6,0,-1,-100,90};
            int temp=0;
            for (int i = 0; i < arr.length-1; i++) {
                for (int j = 0; j < arr.length-i-1; j++) {
                    if(arr[j]>arr[j+1]){
                        temp=arr[j];
                        arr[j]=arr[j+1];
                        arr[j+1]=temp;
                    }
                }
            }
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i]+" ");
            }
        }
    }

    2 简单选择排序(不想说)

    public class Select_sort {
        public static void main(String[] args){
            int arr[]={8,3,2,1,7,11,6,5};
            int temp=0;
            for (int i = 0; i < arr.length-1; i++) {
                for (int j = i+1; j < arr.length; j++) {
                    if(arr[j]<arr[i])
                    {
                        temp=arr[i];
                        arr[i]=arr[j];
                        arr[j]=temp;
                    }
                }
            }
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i]+" ");
            }
        }
    }

    3 直接插入排序

    public class InserSort {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int arr[]={23,15,-13,62,5,-23,0,17};
            for (int i = 1; i < arr.length; i++) {
                int insertVal=arr[i];
                int index=i-1;
                while(index>=0&&insertVal<arr[index]){
                    arr[index+1]=arr[index];
                    index--;
                }
                arr[index+1]=insertVal;
            }
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i]+" ");
            }
        }
    }

    4 快速排序(只是参考,看我下一个严版的快排)http://www.cnblogs.com/clarencezzh/p/5117103.html

    public class QuickSort {
        public void sort(int left,int right,int arr[]){
            int l=left;
            int r=right;
            int pivot=arr[(left+right)/2];
            int temp=0;
            while(l<r){
                while(arr[l]<pivot)l++;
                while(arr[r]>pivot)r--;
                if(l>=r)break;
                temp=arr[l];
                arr[l]=arr[r];
                arr[r]=temp;
                if(arr[l]==pivot)--r;
                if(arr[r]==pivot)++l;
            }
            if(l==r){
                l++;
                r--;
            }
            if(left<r) sort(left,r,arr);
            if(right>l) sort(l,right,arr);
        }
    }
    public class Test {
        public static void main(String[] args) {
            int arr[]={-1,-5,6,2,0,9,-3,-8,12,7};
            QuickSort quickSort=new QuickSort();
            quickSort.sort(0, arr.length-1, arr);
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
        }
    }

    5 二分查找

    看王道上的书

    class BinaryFind{
        public void find(int leftIndex,int rightIndex,int val,int arr[]){
            //首先找到中间的数
            int midIndex=((rightIndex+leftIndex)/2);
            int midVal=arr[midIndex];
            if(rightIndex>=leftIndex){
                //如果要找的数比midVal大
                if(midVal>val){
                    //在arr数组左边数列中找
                    find(leftIndex,midIndex-1,val,arr);
                }else if(midVal<val){
                    //在arr数组右边数列中找
                    find(midIndex+1,rightIndex,val,arr);
                }else if(midVal==val){
                    System.out.println("数组arr["+midIndex+"]中的数字是"+arr[midIndex]);
                }
            }else{
                System.out.println("没有找到你要找的数!");
            }
        }
    }
    public class Test {
        public static void main(String[] args) {
            int arr[]={-1,-5,6,2,0,9,-3,-8,12,7};
            BinaryFind binaryFind=new BinaryFind();
            binaryFind.find(0, arr.length-1, 15, arr);
        }
    }
    加油啦!加油鸭,冲鸭!!!
  • 相关阅读:
    SignalR
    log4net
    nginx
    mongodb
    uni-app监听软键盘是否弹起,ios不支持
    js插件开发兼容commonJS、AMD、CDM的UMD写法
    uni-app 实现后端返回的图片文件流转base64
    input输入框限制只能输入数字和一个小数点
    Uni-app工程转vue-cli工程步骤
    H5 移动端开发注意事项
  • 原文地址:https://www.cnblogs.com/clarencezzh/p/5117105.html
Copyright © 2020-2023  润新知