欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

查找排序的简单算法

程序员文章站 2024-03-16 09:53:22
...

1.排序

1.冒泡

int arr = {1,3,5,2,4,6,9,0,7,8};
for (int i = arr.length -1; i > 0 ; i--){
            for (int j = 0 ; j <i ; j++){
                if(arr[j] > arr[j+1]){
                  int tem = arr[j];
                  arr[j] = arr[j+1];
                  arr[j+1] = tem;
                }
            }
        }
        for (int k = 0 ;k < arr.length ; k++){
            System.out.print(arr[k]);
        }

2.选择

int arr = {1,3,5,2,4,6,9,0,7,8};
for (int i = 0; i <arr.length ; i++){
            for (int j = i+1 ; j <arr.length ; j++){
                if(arr[j] < arr[i]){
                    int tem = arr[j];
                    arr[j] = arr[i];
                    arr[i] = tem;
                }
            }
        }
        for (int k = 0 ;k < arr.length ; k++){
            System.out.print(arr[k]);
        }

3.插入

int arr = {1,3,5,2,4,6,9,0,7,8};    
for (int i = 1 ; i < arr.length ; i++){
            int tem = 0;
             if (arr[i-1] > arr[i]){
                 tem = arr[i];
                 for (int j = i -1; j >= 0 ;j--){
                         arr[j+1] = arr[j] ;
                 if (j == 0||tem >=  arr[j-1] ){
                     arr[j] = tem ;
                     break;
                 }
                 }
             }
        }
        for (int k = 0 ;k < arr.length ; k++){
            System.out.print(arr[k]);
        }

4.快排

int arr = {1,3,5,2,4,6,9,0,7,8};
if (start >= end)
           return;
       int i = start;
       int j = end;
       int key = array[i];

       while (i<j){
           //注意j和i的移动顺序,必须先移动j,否则start元素交换时可能出错
           while (i<j&& key < array[j])
               j--;

           while (i<j&& key >= array[i])
               i++;

           if (i<j){
               int tem = array[i];
               array[i] = array[j];
               array[j] = tem;
           }
       }

       int tem = array[start];
       array[start] = array[i];
       array[i] = tem;

       quickSort(array,start,i-1);
       quickSort(array,i+1,end);

5.归并

int arr = {1,3,5,2,4,6,9,0,7,8};
public static void merge(int[] arrays, int L, int M, int R) {

        //左边的数组的大小
        int[] leftArray = new int[M - L];

        //右边的数组大小
        int[] rightArray = new int[R - M + 1];

        //往这两个数组填充数据
        for (int i = L; i < M; i++) {
            leftArray[i - L] = arrays[i];
        }
        for (int i = M; i <= R; i++) {
            rightArray[i - M] = arrays[i];
        }


        int i = 0, j = 0;
        // arrays数组的第一个元素
        int  k = L;


        //比较这两个数组的值,哪个小,就往数组上放
        while (i < leftArray.length && j < rightArray.length) {

            //谁比较小,谁将元素放入大数组中,移动指针,继续比较下一个
            if (leftArray[i] < rightArray[j]) {
                arrays[k] = leftArray[i];

                i++;
                k++;
            } else {
                arrays[k] = rightArray[j];
                j++;
                k++;
            }
        }

        //如果左边的数组还没比较完,右边的数都已经完了,那么将左边的数抄到大数组中(剩下的都是大数字)
        while (i < leftArray.length) {
            arrays[k] = leftArray[i];

            i++;
            k++;
        }
        //如果右边的数组还没比较完,左边的数都已经完了,那么将右边的数抄到大数组中(剩下的都是大数字)
        while (j < rightArray.length) {
            arrays[k] = rightArray[j];

            k++;
            j++;
        }
    }


public static void mergeSort(int[] arrays, int low, int high) {
        if(low >= hight)
            return ;
		int mid = (low+high)/2;       // 从中间划分两个字序列
		mergeSort(arrays, low, mid);    // 从左侧子序列进行递归排序
		mergeSort(arrays, mid+1, high);   // 从右侧子序列进行递归排序
		merge(arrays, low, mid+1, high);  // 归并
}

6.二分

int arr = {1,2,3,4,5,6,7,8,9}; 
int max = arr.length - 1;
          int min = 0;
          while (max >= min)
          {
              int index = (min + max + 1) /2 ;
              if(key == arr[index]){
                  System.out.println("yes");
                  min =max +1;
              }
              else if (key <arr[index]){
                  max = index -1;
              }
              else{
                  min = index +1;
              }
          }