七大排序算法

package com.heidan.test;

import java.util.Arrays;

public class Review03 {
    public static void main(String[] args) {
        int[] arr = {15,35,95,65,75};
//        bubbleSort(arr);
//        quickSort(arr,0,arr.length-1);
//        selectSort(arr);
//        insertSort(arr);
//        shellSort(arr);
//        mergeSort(arr,0,args.length-1);
        heapSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    //冒泡排序
    public static void bubbleSort(int[] arr){
        for (int i=0;iarr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    
    //快速排序
    public static void quickSort(int[] arr,int start,int end){
        if (start=arr[low]){
                    low++;
                }
                arr[high] = arr[low];
            }
            arr[low] = standard;
            quickSort(arr,start,low);
            quickSort(arr,low+1,high);
        }
    }
    
    //选择排序
    public static void selectSort(int[] arr){
        for (int i=0;iarr[j]){
                    minIndex = j;
                    min = arr[j];
                }
            }
            if (minIndex!=i){
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
        }
    }

    //插入排序
    public static void insertSort(int[] arr){
        for (int i=1;i0 && temp0;d/=2){
            for (int i=d;i=0;j-=d){
                    if (arr[j]>arr[j+d]){
                        int temp = arr[j];
                        arr[j] = arr[j+d];
                        arr[j+d] = temp;
                    }
                }
            }
        }
    }
    //归并排序
    public static void mergeSort(int[] arr,int low,int high){
        int middle = (low+high)/2;
        if (low=0;i--){
            maxHeap(arr,arr.length,i);
        }
        for (int i=arr.length-1;i>0;i--){
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;
            maxHeap(arr,i,0);
        }
    }

    public static void maxHeap(int[] arr,int size,int index){
        int leftNode = 2*index+1;
        int rightNode = 2*index+2;
        int max = index;
        if (leftNodearr[max]){
            max = leftNode;
        }
        if (rightNodearr[max]){
            max = rightNode;
        }
        if (max!=index){
            int temp = arr[index];
            arr[index] = arr[max];
            arr[max] = temp;
            maxHeap(arr,size,max);
        }
    }

}

 

你可能感兴趣的:(数据结构和算法,java)