数组中的排序算法以及普通查找

Posted on 2022-10-23 23:32  嗷呦不错呦  阅读(63)  评论(0)    收藏  举报

数组中的排序算法以及普通查找

普通查找

基本查找

public class Demo01 {
        public static void main(String[] args) {
            int[] m ={10,45,78,4,6,85,14,35};
            Scanner input =new Scanner(System.in);
            System.out.println("请输入你要搜索的值:");
            int p = input.nextInt();
            int h = search(m,p);
            System.out.println("匹配的值为下标为:"+h);
        }
       static int search(int[] m,int j){
            for (int i = 0; i < m.length; i++) {
                if(j==m[i]){
                    return i;
                }
            }
            return -1;
        }
    }

二分查找

要求:数组有序

思想:每一次都会查找中间的元素,比较大小就能减少一半的元素

//二分查找
public class Demo02 {
    public static void main(String[] args) {
        int[] core = {4, 8, 45, 78, 98, 112, 442, 789, 879, 987};
        Scanner input = new Scanner(System.in);
        System.out.println("请输入你要搜索的值:");
        int in = input.nextInt();
        int Index = ReachTndex(core, in);
        System.out.println("匹配的下标值为:"+Index);
    }

    private static int ReachTndex(int[] core, int in) {
        int minIndex = 0;
        int maxIndex = core.length - 1;
        while (minIndex <= maxIndex) {
            int centerIndex = (minIndex + maxIndex) / 2;
            if (in == core[centerIndex]) {
                return centerIndex;
            } else if (in > core[centerIndex]) {
                minIndex = centerIndex - 1;
            } else if (in < core[centerIndex]) {
                maxIndex = centerIndex - 1;
            }
        }
        return -1;
    }
}

排序算法

将无序数组变为有序数组

冒泡排序

原理:数组两两比较,交换位置。

//冒泡排序
public class Demo03 {
    public static void main(String[] args) {
        int[] array  = {42,5,78,2,23,0,564,2};
        int temp1;
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 1; j < array.length - i; j++) {
                if (array[j - 1] > array[j]) {
                    temp1 = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = temp1;
                }
            }
            System.out.println(Arrays.toString(array));
        }
    }
}

选择排序

public class Demo04 {
    public static void main(String[] args) {
        int[] array ={12,45,7,45,1,0,21,5,232,0,23,96,4};
        for (int i = 0; i < array.length-1; i++) {
            int temp;
            for (int j = i+1; j < array.length; j++) {
                if(array[i]>array[j]){
                    temp= array[i];
                    array[i]=array[j];
                    array[j]=temp;
                }
            }
            System.out.println(Arrays.toString(array));
        }
    }
}

直接插入排序

public class Demo05 {
    public static void main(String[] args) {
        int[] m ={4,45,8,9,56,27,41,75};
        for (int i = 1; i < m.length; i++) {
            int j=i;
            while(j>0&&m[j]<m[j-1]){
                int temp=m[j];
                m[j]=m[j-1];
                m[j-1]=temp;
                j--;
            }
        }
        System.out.println(Arrays.toString(m));
    }
}

希尔排序

//希尔排序
public class Demo06 {
    public static void main(String[] args) {
        int[] arr01 = {45,56,23,23,56,23,0,56,35,23,52,24};
          check(arr01);
        System.out.println(Arrays.toString(arr01));
    }
    private static void check(int[] arr01) {
       //克努特序列
        int jiange=1;
        while(jiange<arr01.length){
            jiange = jiange*3+1;
        }
        for(int h=jiange;h>0;h=(h-1)/3){
              for(int i=h;i<arr01.length;i++){
                  for(int j=i;j>h-1;j-=h){
                      if(arr01[j]<arr01[j-h]){
                          swapvalue(arr01,j,j-h);
                      }
                  }
              }
        }
    }

    private static void swapvalue(int[] arr01, int j, int i) {
    int t =arr01[j];
    arr01[j]=arr01[i];
    arr01[i]=t;
    }
}

快速排序

分治法:比大小,再分区。

  1. 从数组中取一个数,作为基准数
  2. 分区:将比这个数大或等于的数全放在他的右边,小于他的数全放到他的左边
  3. 再对左右分区区间重复第二步,直到各区间只有一个数
public class Demo07 {
    public static void main(String[] args) {
        //定义一个数组
        int arr[] ={12,74,52,43,25,25,2,87,95,41,32};
        //调用工具类
        QuickSoutUtils.quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
}
class QuickSoutUtils{
    public static void quickSort(int arr[],int start,int end){
     if(start<end){
         int index = getIndex(arr,start,end);
         quickSort(arr,start,index-1);
         quickSort(arr,index+1,end);
     }
        }
    private static int getIndex(int[] arr, int start, int end) {
             int i =start;
             int j =end;
             int x = arr[i];
           while(i<j){
               //由后往前找比他小的数,找到后挖出来填到后一个坑中
               while (i < j && arr[j]>=x) {
                   j--;
               }
               if(i<j){
                   arr[i]=arr[j];
                   i++;
               }
               //由前往后找比他大或等于的数,找到后也挖出来填到前一个坑中
               while (i < j && arr[i]<=x) {
                   i++;
               }
               if(i<j){
                   arr[j]=arr[i];
                   j--;
               }
           }
           arr[i]=x;//把基准数放入最后一个坑
             return i;
    }
}

归并排序

image-20221022154339689

图片引用于 西部开源

package arraySearchDemo;

//归并排序
public class Demo08 {
    public static void main(String[] args) {
        int[] arrArr = {45, 8, 6, 245, 63, 74, 57, 14, 35, 49, 52};
        chaifen(arrArr, 0, arrArr.length - 1);
    }

    private static void chaifen(int[] arrArr, int startIndex, int endIndex) {
        int centerIndex = (startIndex + endIndex) / 2;
        if (startIndex < endIndex) {
            chaifen(arrArr, startIndex, centerIndex);
            chaifen(arrArr, centerIndex + 1, endIndex);
            guibing(arrArr, startIndex, centerIndex, endIndex);
        }
    }

    private static void guibing(int[] arrArr, int startIndex, int centerIndex, int endIndex) {
        //定义一个临时数组
        int[] tempArr = new int[endIndex - startIndex + 1];
        //定义左边的起始数组
        int i = startIndex;
        //定义右边的起始数组
        int j = centerIndex + 1;
        //定义临时数组的起始数组
        int index = 0;
        if (i <= centerIndex && j <= endIndex) {
            if (arrArr[i] <= arrArr[j]) {
                tempArr[index] = arrArr[i];
                i++;
            } else {
                tempArr[index] = arrArr[j];
                j++;
            }
            index++;
        }
        //剩余元素处理
        while (i <= centerIndex) {
            tempArr[index] = arrArr[i];
            i++;
            index++;
        }
        while (j <= endIndex) {
            tempArr[index] = arrArr[j];
            i++;
            index++;
        }
    }
}

基数排序

基数排序不同于先前的排序

前面的排序或多或少是通过使用比较和移动记录来实现排序,而基数排序的实现不需要进行对关键字的比较,只需要对关键字进行“分配”和“收集”两种操作即可完成。

public class Demo09 {
    public static void main(String[] args) {
  int[] arrArr ={12,85,47,68,127,452,896,74,339};
     fenzhu(arrArr);
        System.out.println(Arrays.toString(arrArr));
    }

    private static void fenzhu(int[] arrArr) {
        //定义一个二维数组,放十个桶
        int[][] tempArr = new int[10][arrArr.length];
        //定义统计数组
        int[] counts = new int[10];
        int max = getMax(arrArr);
        int len = String.valueOf(max).length();
        //循环轮次
        for (int i = 0,n=1; i < len; i++,n*=10) {
            for(int j=0;j<arrArr.length;j++){
                int ys = arrArr[j]/n%10;
                tempArr[ys][counts[ys]++]=arrArr[j];
            }
            //取出桶种元素
            int index = 0;
            for (int k = 0; k < counts.length; k++) {
                if(counts[k]!=0){
                    for(int h=0;h<counts[k];h++){
                        arrArr[index]=tempArr[k][h];
                        index++;
                    }
                    counts[k]=0;
                }
            }
        }
    }
    private static int getMax(int[] arrArr) {
        int max= arrArr[0];
        for (int i = 1; i < arrArr.length; i++) {
            if(arrArr[i]>max){
                max = arrArr[i];
            }
        }
        return max;
    }
}

堆排序

是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序

堆排序的基本思想是:

  1. 将待排序序列构成一个大顶锥,整个序列的最大值就是堆顶的根节点。
  2. 将其末尾元素进行交换,此时末尾就为最大值
  3. 然后将剩余的n-1个元素从新构成一个堆,这样会得到n个元素的次小值。
  4. 如此反复执行,使能得到一个有序序列了

大顶锥 一般升序;小顶锥 一般降序

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3