Java实现基本排序算法

Linux大全评论334 views阅读模式

本例子实现了一些常见的排序算法,注释中也有一些关于这些算法的思想的描述,这里不做多,使用Java实现基本排序算法,直接上代码。

import java.awt.List;
import java.util.ArrayList;
import java.util.Hashtable;

/**
* @ClassName: Sort
* @Description: //默认按照升序排序

*/
public class Sort {
    /**
    *
    * @Description: 直接插入排序
    * @author 陈杰
    * @date 2016年10月7日 下午9:08:26
    * @param arr
    */
    public void directlyInsertSort(int[] arr){
        if(arr==null||arr.length==0)
            return;
        //int i,j;
        //初始默认arr[0]为已排好的序列
        for(int i=1;i<arr.length;i++){
            int temp=arr[i];
            for(int j=i-1;j>=0;j--){
                if(arr[j]>temp){

                    arr[j+1]=arr[j];//元素向后移动一位
                    arr[j]=temp;
                } 
            }

        }
        //打印排序后结果
        for(int value:arr){
            System.out.print(value+"  ");
        }
    }
    /**
    *
    * @Description: 希尔排序 (这里用一句话描述这个类的作用)
    * @author 陈杰
    * @date 2016年10月8日 下午7:24:16
    * @param arr
    * @param n
    */
    public void shellSort(int arr[],int dk){
        int temp=0;
        for(int d=dk;d>0;d=d/2){
            for(int i=d;i<arr.length;i++){
                temp=arr[i];
                //int index=-1;//待插入的位置
                for(int j=i-d;j>=0;j=j-d){
                    if(arr[j]>temp){
                        //index=j;
                        arr[j+d]=arr[j];//元素向后移动一位
                        arr[j]=temp;
                    } 
                }

            }
        } 
        //打印排序后结果
        for (int value : arr) {
            System.out.print(value + "  ");
        }
    }
   
    /**
 *
* @Description: 折半插入排序
* @author 陈杰
* @date 2016年10月8日 上午10:27:07
* @param arr
 */
    public void halfInsertSort(int[] arr){
        if(arr==null||arr.length==0)
            return;
        int i,j,low,high,mid;
        for(i=1;i<arr.length;i++){
            mid=0;
            low=0;
            high=i-1;
            int temp=arr[i];
            //折半获得插入位置
            while(low<=high){
                mid=(low+high)/2;
                if(temp>arr[mid])
                    low=mid+1;
                else
                    high=mid-1;
            }
            //移动位置
            for(j=i-1;j>=high+1;--j){
                arr[j+1]=arr[j];
            }
            arr[high+1]=temp;
           
        }
        //打印排序后结果
        for(int value:arr){
            System.out.print(value+"  ");
        }
    }
    /**
    *
    * @Description: 冒泡排序(这里用一句话描述这个类的作用)
    * @author 陈杰
    * @date 2016年10月8日 上午11:14:05
    * @param arr
    */
    public void buddleSort(int[] arr){
        if(arr==null||arr.length==0)
            return;
        int length=arr.length;
        for(int i=0;i<length-1;i++){
            for(int j=length-1;j>i;j--){
                int temp=arr[j];
                if(arr[j-1]>arr[j]){
                    arr[j]=arr[j-1];
                    arr[j-1]=temp;
                }
            }
        }
        //打印排序后结果
        for(int value:arr){
            System.out.print(value+"  ");
        }
    }
    /**
    *
    * @Description: 快速排序
    * @author 陈杰
    * @date 2016年10月8日 下午6:25:16
    * @param arr
    */
    public void quickSort(int[] arr,int low,int high){
        if (arr == null || low < 0 || high < 0 || low >= arr.length) return;
        if(low<high){
            int pivotIndex=partion(arr, low, high);
            quickSort(arr, low, pivotIndex-1);
            quickSort(arr, pivotIndex+1, high);
        }
       
    }
    /**
    *
    * @Description: 快速排序的核心划分算法
    * @author 陈杰
    * @date 2016年10月8日 下午6:52:58
    * @param arr
    * @param low
    * @param high
    */
    private int partion(int[] arr,int low,int high){
        int pivot=arr[low];
        while(low<high){
            while((low<high)&&arr[high]>=pivot)
                high--;
            arr[low]=arr[high];//将比基准小的移动到左侧
            while((low<high)&&arr[low]<=pivot)
                low++;
            arr[high]=arr[low];//将比基准大的移动到右端
        }
        arr[low]=pivot;
        return low;
       
    }
    /**
    *
    * @Description: 简单选择排序 (这里用一句话描述这个类的作用)
    * @author 陈杰
    * @date 2016年10月8日 下午8:16:21
    * @param arr
    */
    public void selectSort(int arr[]){
        for(int i=0;i<arr.length-1;i++){
            int min=i;//记录本次待排序最小元素的位置
            for(int j=i+1;j<arr.length;j++){
                if(arr[min]>arr[j])
                    min=j;
            }
            //将未排序列表中的最小元素和待排序元素的位置互换
            if(min!=i){
                int temp=arr[i];
                arr[i]=arr[min];
                arr[min]=temp;
            }
               
        }
        //打印排序后结果
        for (int value : arr) {
            System.out.print(value + "  ");
        }
    }
    /**
    *
    * @Description: 堆排序算法 (这里用一句话描述这个类的作用)
    * @author 陈杰
    * @date 2016年10月9日 下午5:40:56
    * @param arr
    */
    public void heapSort(int arr[]){
        if(arr==null||arr.length<=0)
            return;
        bulidHeap(arr);
        for(int i=arr.length-1;i>=0;i--){
            //交换堆顶元素和堆低元素的值,以打破堆让其重新调整,从而每次输出最大值(堆顶的值),直至堆为空
            int temp=arr[0];//堆顶元素
            arr[0]=arr[i];
            arr[i]=temp;
            adjustHeap(arr, 0, i);
        }
       
        //打印排序后结果
        for (int value : arr) {
            System.out.print(value + "  ");
        }
    }
    /**
    *
    * @Description:构建初始堆(以大根堆为例)
    * @author 陈杰
    * @date 2016年10月9日 下午5:43:02
    * @param arr
    */
    private void  bulidHeap(int arr[]){
        int length=arr.length;
        for(int i=length/2-1;i>=0;i--){
            adjustHeap(arr,i,length-1);
        }
    }
    /**
    *
    * @Description: 调整堆的方法,自下向上调整堆,含有递归部分
    * @author 陈杰
    * @date 2016年10月9日 下午5:48:00
    * @param arr
    * @param nodeIndex 分支节点的索引下标
    * @param max
    */
    private void adjustHeap(int arr[],int nodeIndex,int max){
        int left=2*nodeIndex+1;//左孩子
        int right=2*nodeIndex+2;//右孩子
        int maxValueIndex=nodeIndex;//用来记录当前三个节点间最大值的下标值
        if(left<max&&arr[left]>arr[nodeIndex])
            maxValueIndex=left;
        if(right<max&&arr[right]>arr[maxValueIndex])
            maxValueIndex=right;
        if(maxValueIndex!=nodeIndex){
            //交换元素,构造堆
            int temp=arr[nodeIndex];
            arr[nodeIndex]=arr[maxValueIndex];
            arr[maxValueIndex]=temp;
            //对交换后的元素向下继续调整堆
            adjustHeap(arr,maxValueIndex,max);
        }
       
       
    }
    /**
    *
    * @Description: 二路归并排序(基于分治的思想)
    * @author 陈杰
    * @date 2016年10月9日 下午6:50:15
    * @param arr
    * @param low
    * @param high
    */
    private void mergeSort(int[] arr,int low,int high){
        if(low<high){
            int mid=(low+high)/2;
            mergeSort(arr,low,mid);
            mergeSort(arr, mid+1,high);
            merge(arr, low, mid, high);
        }
    }
    /**
    *
    * @Description: 二路归并排序中将前后两个有序表合并成一个有序表的过程
    * @author 陈杰
    * @date 2016年10月9日 下午7:13:47
    * @param arr
    * @param low
    * @param mid
    * @param high
    */
    private void merge(int arr[],int low,int mid, int high){
        int[] temp=new int[high-low+1];//临时数组
        int index=0;
        int left=low;//左表的下标开始值
        int right=mid+1;//右表的下标开始值
        //比较两个表中较小的值将其放在临时数组中
        while(left<=mid&&right<=high){
            if(arr[left]>arr[right]){
                temp[index++]=arr[right++];
            }else{
                temp[index++]=arr[left++];
            } 
        }
        //若左表中有剩余的元素,将其复制到临时表中
        while(left<=mid)
            temp[index++]=arr[left++];
        //若右表中有剩余的元素,将其复制到临时表中
        while(right<=high)
            temp[index++]=arr[right++];
        //将排好序的临时数组回归到原数组中,获得排序结果
        for(int k=0;k<temp.length;k++)
            arr[low+k]=temp[k];
       
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int arr[]={1,4,2,6,3,9,6,5,3,8,6,3};
        Sort sort=new Sort();
        System.out.println("直接插入排序的结果:");
        sort.directlyInsertSort(arr);
        System.out.println();
        System.out.println("折半插入排序的结果");
        int arr2[]={1,4,2,6,3,9,6,5,3,8,6,3};
        sort.halfInsertSort(arr2);
        System.out.println();
        System.out.println("冒泡排序的结果");
        int arr3[]={1,4,2,6,3,9,6,5,3,8,6,3};
        sort.buddleSort(arr3);
        System.out.println();
        System.out.println("快速排序的结果");
        int arr4[]={1,4,2,6,3,9,6,5,3,8,6,3};
        sort.quickSort(arr4, 0, arr4.length-1);
        //打印排序后结果
        for (int value : arr4) {
            System.out.print(value + "  ");
        }
        System.out.println();
        System.out.println("希尔排序的结果");
        int arr5[]={1,4,2,6,3,9,6,5,3,8,6,3};
        sort.shellSort(arr5, 6);
        System.out.println();
        System.out.println("简单选择排序的结果");
        int arr6[]={1,4,2,6,3,9,6,5,3,8,6,3};
        sort.selectSort(arr6);
        System.out.println();
        System.out.println("堆排序的结果");
        int arr7[]={1,4,2,6,3,9,6,5,3,8,6,3};
        sort.heapSort(arr7);
        System.out.println();
        System.out.println("二路归并排序的结果");
        int arr8[]={1,4,2,6,3,9,6,5,3,8,6,3};
        sort.mergeSort(arr8, 0, arr8.length-1);
        //打印排序后结果
        for (int value : arr8) {
            System.out.print(value + "  ");
        }
        ArrayList<String> ss=new ArrayList<String>();
        ss.equals(arr8);
        ss.hashCode();
    }

}

企鹅博客
  • 本文由 发表于 2020年6月26日 08:18:01
  • 转载请务必保留本文链接:https://www.qieseo.com/135433.html

发表评论