首页 > 编程语言 >数组中的排序算法以及普通查找

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

时间:2022-10-23 23:47:16浏览次数:44  
标签:int void arrArr 算法 查找 static array 排序 public

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

普通查找

基本查找

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. 如此反复执行,使能得到一个有序序列了

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

标签:int,void,arrArr,算法,查找,static,array,排序,public
From: https://www.cnblogs.com/wei-shen/p/16820055.html

相关文章

  • 单链表c语言实现网上查找
    插入#include<malloc.h>#defineSIZE100#defineINCREMENT_SIZE10typedefstructLNode{intdata;LNode*next;}LNode,*LinkList;//creataLinkLi......
  • 查找算法
    总结常用的查找算法,针对不同的情况,能够反应出哪种数组结构是效率最快的##顺序查找条件:无序或有序队列。原理:按顺序比较每个元素,直到找到关键字为止。时间复杂度:O(n)#......
  • (四)MySQL基础知识之union和排序
    继续连着昨天数据库的基本操作,今天看下MySQL的union功能和排序 union:UNION操作符用于连接两个以上的SELECT语句的结果组合到一个结果集合中。如果多个SELECT语句会......
  • 单链表插入和删除一个节点的伪代码算法
    插入设ai-1节点为pai+1节点为q插入节点为t则p-->t-->next=q-->next删除设ai-1节点为pai+1节点为q删除的字节为tp-->next=t-->nextfree(t)参考链接https://bl......
  • c++算法竞赛常用板子集合(持续更新)
    前言本文主要包含算法竞赛一些常用的板子,码风可能不是太好,还请见谅。后续会继续补充没有的板子。当然我太菜了有些可能写不出来T^T稍微有些分类但不多,原谅我QwQ建议Ct......
  • 基于miu小波变换的人体步态数据检测和识别算法matlab仿真
    目录一、理论基础3.2.1加速度计3.2.2陀螺仪 3.3基于IMU设备的人体步态数据的采集二、MATLAB仿真程序三、仿真结果一、理论基础在进行数据采集的过程中,需要根据实......
  • leetcode1002-查找共用字符
    1002.查找共用字符classSolution{public:vector<string>commonChars(vector<string>&words){intsize=words.size();vector<string>res......
  • 7.Python自定义排序详解
    如果以创建的对象作为列表中的元素,那么对列表进行排序时可使用sort()函数或sorted()函数,但要注意的是:①当排序对象为列表的时候两者适合的场景不同②sorted()函数会返......
  • MD5 哈希加密算法 - C++ 实现
    MD5加密算法-C++实现写在前头:还在学习中!整个文档写的很匆忙,肯定还有很多不周到的地方.欢迎在评论中提出你的宝贵意见!!算法背景BackgroundMD5消息摘要算法......
  • 贪心算法 455
    455.分发饼干classSolution{publicintfindContentChildren(int[]g,int[]s){Arrays.sort(g);//孩子列表Arrays.sort(s);//饼干列表......