原创

基本排序算法


冒泡排序

package shuangfa;
import java.util.Arrays;
public class BubbleSort {
    public static void main(String[] args) {
        int [] arr = new int[]{8,4,5,2,7,10,3};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void bubbleSort(int[] arr) {
        //控制多少轮
        for(int i = 0;i<arr.length;i++){
            //控制比较次数
            for(int j = 0;j < arr.length-1-i;j++){
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
}

快速排序

package shuangfa;
import java.sql.Array;
import java.util.Arrays;
public class QuickSort {
    public static void main(String[] args) {
        int [] arr = new int[]{8,4,5,2,7,10,3};
        quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));

    }
    public static void quickSort(int[] arr,int start,int end){
        if(start < end){
            //取一个数
            int st = arr[start];
            int low = start;
            int high = end;
            while (low < high) {
                while (low < high && st <= arr[high]){
                    high--;
                }
                arr[low] = arr[high];
                while (low < high && arr[low] <= st){
                    low++;
                }
                arr[high] = arr[low];
            }
            arr[low] = st;
            //递归左边
            quickSort(arr,start,low);
            //递归右边
            quickSort(arr,low+1,end);
        }

    }
}

选择排序

package shuangfa;
import java.util.Arrays;

public class SelectSort {
    public static void main(String[] args) {
        int [] arr = new int[]{8,4,5,2,7,10,3};
        selectSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    private static void selectSort(int[] arr) {
        //遍历所有的数
        for(int i = 0;i < arr.length;i++){
            int minIndex = i;
            for(int j = i+1;j<arr.length;j++){
                if(arr[minIndex] > arr[j]){
                    minIndex = j;
                }
            }
            //交换数据
            if(i!=minIndex){
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
        }
    }
}

归并排序

package shuangfa;
import java.util.Arrays;

public class MergeSort {
    public static void main(String[] args) {
        int [] arr = new int[]{3,1,10,89,3,32,32,45};
        mergeSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
    
    //递归调用,直到为两个数或一个停止
    public static void mergeSort(int[] arr,int low,int high){
        int middle = (low+high)/2;
        if(low<high){
            mergeSort(arr,low,middle);
            mergeSort(arr,middle+1,high);
            merge(arr,low,middle,high);
        }
    }
    //合并有序的数组
    public  static  void merge(int[] arr,int low,int middle,int high){
        //归并后的临时数组
        int[] temp = new int[high-low+1];
        //第一个数组
        int i = low;
        //第二个数组
        int j = middle + 1;
        int index = 0;
        //遍历取小的数放在临时数组中
        while (i<=middle && j<=high){
            if(arr[i] <= arr[j]){
                temp[index] = arr[i];
                i++;
            }else{
               temp[index] = arr[j];
                j++;
            }
            index++;
        }
        while (j<=high){
            temp[index] = arr[j];
            j++;
            index++;
        }
        while (i<=middle){
            temp[index] = arr[i];
            i++;
            index++;
        }
        for(int k = 0;k<temp.length;k++){
            arr[k+low] = temp[k];
        }
    }
}

插入排序

package shuangfa;
import java.util.Arrays;

public class InserSort {
    public static void main(String[] args) {
        int [] arr = new int[]{8,4,5,2,7,10,3};
        inserSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    private static void inserSort(int[] arr){
        for(int i =1; i< arr.length;i++){
            if(arr[i]<arr[i-1]){
                int temp = arr[i];
                int j;
                // 如果比前面的小就往前排
                for(j=i-1;j>=0 && temp<arr[j];j--){
                    arr[j+1] = arr[j];
                }
                //交换数据
                arr[j+1] = temp;
            }
        }
    }
}

基数排序

package shuangfa;
import java.util.Arrays;

public class RadixSort {
    public static void main(String[] args) {
        int [] arr = new int[]{8,454,545,25,75,10,3};
        radixSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void radixSort(int[] arr) {
        int max = Integer.MIN_VALUE;
        for(int i = 0;i<arr.length;i++){
            if(arr[i] > max){
                max = arr[i];
            }
        }
        int maxLength = (max+"").length();
        int[][] temp = new int[10][arr.length];
        int[] count = new int[10];
        for(int i = 0,n=1;i<maxLength;i++,n *= 10){
            for(int j = 0;j<arr.length;j++){
                int ys =arr[j]/n%10;
                temp[ys][count[ys]] = arr[j];
                count[ys]++;
            }
            int index = 0;
            for(int l = 0;l<count.length;l++){
                if(count[l]!=0){
                    for(int k=0;k<count[l];k++){
                        arr[index] = temp[l][k];
                        index++;
                    }
                    count[l] = 0;
                }
            }
        }
    }
}

java
  • 作者:黄伟明
  • 发表时间:2021-06-28 00:30
  • 版权声明:非商业自由转载

评论



留言