常见的排序算法Java源码示例

排序算法源码示例


代码将常见的排序算法源码放在一个类中。本文算法已经经过验证,可以直接拿来进行使用。源码中主要包含插入排序(直接插入排序,二分插入排序,希尔排序),交换排序(冒泡排序,快速排序(由quick,partition两个方法完成)),选择排序(简单选择排序,堆排序(由heap,buildMaxHeap,adjustDown三个方法完成)),归并排序(举例了二路归并排序的例子,由mergefrom2和merge两个方法完成)

public class Sort{
    //插入排序
    class InsertSort{
        //直接插入排序
        public void direct(int[] src){
            int length = src.length;
            for(int i=1; i<length; i++){
                int temp = src[i];
                int j;
                for(j=i-1; j>=0 && temp<src[j]; j--)
                    src[j+1] = src[j];
                src[j+1] = temp;
            }
        }
        //二分插入排序
        public void half(int[] src){
            int length = src.length;
            for(int i=1; i<length; i++){
                int temp = src[i];
                int left = 0;
                int right = i-1;
                while(left<=right){
                    int mid = (left+right)/2;
                    if(temp < src[mid])
                        right = mid - 1;
                    else
                        left = mid + 1;
                }
                for(int j=i; j>left; j--)
                    src[j] = src[j-1];
                src[left] = temp;
            }
        }
        //希尔排序
        public void shell(int[] src){
            int d = src.length/2;
            while(d>=1){
                int length = src.length;
                for(int i=d; i<length; i++){
                    int temp = src[i];
                    int j;
                    for(j=i-d; j>=0 && temp<src[j]; j -=d)
                        src[j+d] = src[j];
                    src[j+d] = temp;
                }
                d /= 2;
            }
        }
    }
    //交换排序
    class SwapSort{
        //冒泡排序
        public void bubble(int[] src){
            int length = src.length-1;
            for(int i=0; i<length; i++){
                boolean flag = false;
                for(int j=length; j>i; j--)
                    if(src[j] < src[j-1]){
                        int temp = src[j];
                        src[j] = src[j-1];
                        src[j-1] = temp;
                        flag = true;
                    }
                if(!flag)
                    return ;
            }
        }
        //快速排序
        public void quick(int[] src, int left, int right){
            if(left < right){
                int pivot = partition(src, left, right);
                quick(src, left, pivot-1);
                quick(src, pivot+1, right);
            }
        }
        public int partition(int[] src, int left, int right){
            int pivot = src[left];
            while(left < right){
                while(left < right && src[right] >= pivot)
                    --right;
                src[left] = src[right];
                while(left < right && src[left] <= pivot)
                    ++left;
                src[right] = src[left];
            }
            src[left] = pivot;
            return left;
        }
    }
    //选择排序
    class SelectSort{
        //简单选择排序
        public void easy(int[] src){
            int length = src.length-1;
            for(int i=0; i<length; i++){
                int min = i;
                int len = src.length;
                for(int j=i+1; j<len; j++)
                    min = src[j] < src[min] ? j : min;
                int temp = src[i];
                src[i] = src[min];
                src[min] = temp;
            }
        }
        //堆排序
        public void heap(int[] src){
            buildMaxHeap(src);
            for(int i=src.length-1; i>0; i--){
                int temp = src[i];
                src[i] = src[0];
                src[0] = temp;
                adjustDown(src, 0, i-1);
            }
        }
        public void buildMaxHeap(int[] src){
            for(int i=(src.length-1)/2; i>=0; i--)
                adjustDown(src, i, src.length-1);
        }
        public void adjustDown(int[] src, int i, int j){
            int temp = src[i];
            for(int k=2*i+1; k<=j; k=2*k+1){
                if(k<j && src[k] < src[k+1])
                    ++k;
                if(temp > src[k])
                    break;
                else{
                    src[i] = src[k];
                    i = k;
                }
            }
            src[i] = temp;
        }
    }
    //归并排序
    class MergeSort{
        //二路归并排序
        public void mergefrom2(int[] src, int left, int right){
            if(left < right){
                int mid = (left+right) / 2;
                mergefrom2(src, left, mid);
                mergefrom2(src, mid+1, right);
                merge(src, left, mid, right);
            }
        }
        public void merge(int[] src, int left, int mid, int right){
            int[] src0 = new int[src.length];
            System.arraycopy(src, 0, src0, 0, src.length);
            int i,j,k;
            for(i=left, j=mid+1, k=i; i<=mid && j<=right; k++)
                src[k] = src0[i] <= src0[j] ? src0[i++] : src0[j++];
            while(i<=mid) src[k++] = src0[i++];
            while(j<=right) src[k++] = src0[j++];
        }
    }
}
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页