几种常见排序算法的Java代码实现

首先通过下面一张图了解排序算法的分类:
下面是八种排序算法的Java代码实现:

一.插入排序

简单插入排序

[java]  view plain copy
  1. public class InsertSort {  
  2.   
  3.   
  4.     /** 
  5.      * @param args 
  6.      */  
  7.     public static void main(String[] args) {  
  8.         // TODO Auto-generated method stub  
  9.        int a[]={12,32,4,11,54,33,75,44,8,10};  
  10.        insertSort(a);  
  11.        for(int i=0;i<a.length;i++){  
  12.            System.out.print(a[i]+" ");  
  13.        }  
  14.     }  
  15.     public static void insertSort(int[] a){  
  16.         int temp = 0;  
  17.         for(int i=1;i<a.length;i++){  
  18.             temp = a[i];  
  19.             int j;  
  20.             for(j=i-1;j>=0;j--){  
  21.                 if(temp<a[j]){  
  22.                     a[j+1] = a[j];  
  23.                     //a[j] =temp;  
  24.                 }else{  
  25.                     break;  
  26.                 }  
  27.                   
  28.             }  
  29.             a[j+1] =temp;  
  30.         }  
  31.     }  
  32.   
  33.   
  34. }
二分插入排序

[java]  view plain copy
  1. public class ErfenSort {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         int a[]={12,32,4,11,54,33,75,44,8,10};  
  9.            erfenSort(a);  
  10.            for(int i=0;i<a.length;i++){  
  11.                System.out.print(a[i]+" ");  
  12.            }  
  13.     }  
  14.     public static void erfenSort(int[] a){  
  15.         int temp=0;  
  16.           
  17.         for(int i=1;i<a.length;i++){  
  18.             temp = a[i];  
  19.             int left=0;  
  20.             int right = i-1;  
  21.             int mid = 0;  
  22.             while(left<=right){  
  23.                 mid = (left+right)/2;  
  24.                 if(a[mid]>temp){  
  25.                     right = mid-1;  
  26.                 }else{  
  27.                     left = mid+1;  
  28.                 }  
  29.             }  
  30.             for(int j=i-1;j>=left;j--){  
  31.                 a[j+1] = a[j];  
  32.             }  
  33.             if(left!=i){  
  34.             a[left] = temp;  
  35.             }  
  36.         }  
  37.     }  
  38. } 

希尔排序

[java]  view plain copy
  1. public class ShellSort {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         int a[]={12,32,4,11,54,33,75,44,8,10};  
  9.            shellSort(a);  
  10.            for(int i=0;i<a.length;i++){  
  11.                System.out.print(a[i]+" ");  
  12.            }  
  13.     }  
  14.    public static void shellSort(int[] a){  
  15.        int d = a.length;  
  16.        int temp = 0;  
  17.        while(d!=1){  
  18.            d = d/2;  
  19.            for(int x=0;x<d;x++){  
  20.                for(int i=x+d;i<a.length;i+=d){  
  21.                    temp = a[i];  
  22.                    int j;  
  23.                    for(j=i-d;j>=0;j-=d){  
  24.                        if(temp<a[j]){  
  25.                            a[j+d] = a[j];  
  26.                            //a[j] = temp;  
  27.                        }else{  
  28.                            break;  
  29.                        }  
  30.                    }  
  31.                    a[j+d] = temp;  
  32.                }  
  33.            }  
  34.        }  
  35.    }  
  36. }

二.选择排序

简单选择排序

[java]  view plain copy
  1. public class SelectSort {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         int a[]={12,32,4,11,54,33,75,44,8,10};  
  9.            selectSort(a);  
  10.            for(int i=0;i<a.length;i++){  
  11.                System.out.print(a[i]+" ");  
  12.            }  
  13.     }  
  14.     public static void selectSort(int[] a){  
  15.         int temp = 0;  
  16.         for(int i=0;i<a.length;i++){  
  17.             int p = i;  
  18.             temp = a[i];  
  19.             int j;  
  20.             for(j=i+1;j<a.length;j++){  
  21.                 if(temp>a[j]){  
  22.                     temp = a[j];  
  23.                     p = j;  
  24.                 }  
  25.                   
  26.             }  
  27.             a[p] = a[i];  
  28.             a[i] = temp;  
  29.         }  
  30.     }  
  31.   
  32. }
堆排序

[java]  view plain copy
  1. public class HeapSort {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         int a[]={12,32,4,11,54,33,75,44,8,10};  
  9.            heapSort(a);  
  10.            for(int i=0;i<a.length;i++){  
  11.                System.out.print(a[i]+" ");  
  12.            }  
  13.     }  
  14.    private static void swap(int[] a,int i,int j){  
  15.        int temp = a[i];  
  16.        a[i] = a[j];  
  17.        a[j] = temp;  
  18.    }  
  19.    public static void heapSort(int[] a){  
  20.        for(int i=0;i<a.length;i++){  
  21.            buildMaxHeap(a,a.length-1-i);  
  22.            swap(a,0,a.length-1-i);  
  23.        }  
  24.    }  
  25.    private static void buildMaxHeap(int[]a,int lastIndex){  
  26.        for(int i=(lastIndex-1)/2;i>=0;i--){  
  27.            int k=i;  
  28.            while(2*k+1<=lastIndex){  
  29.                int biggerIndex = 2*k+1;  
  30.                if(biggerIndex<lastIndex){  
  31.                    if(a[biggerIndex]<a[biggerIndex+1]){  
  32.                        biggerIndex++;  
  33.                    }  
  34.                }  
  35.                if(a[k]<a[biggerIndex]){  
  36.                    swap(a,k,biggerIndex);  
  37.                    k = biggerIndex;  
  38.                }else{  
  39.                    break;  
  40.                }  
  41.            }  
  42.        }  
  43.    }  
  44. }

三.交换排序

冒泡排序

[java]  view plain copy
  1. public class BubbleSort {  
  2.    public static void main(String[] args){  
  3.        int a[]={12,32,4,11,54,33,75,44,8,10};  
  4.        bubbleSort(a);  
  5.        for(int i=0;i<a.length;i++){  
  6.            System.out.print(a[i]+" ");  
  7.        }  
  8.    }  
  9.    public static void bubbleSort(int[] a){  
  10.        for(int i=0;i<a.length-1;i++){  
  11.            for(int j=0;j<a.length-1-i;j++){  
  12.                if(a[j]>a[j+1]){  
  13.                    int temp = a[j];  
  14.                    a[j] = a[j+1];  
  15.                    a[j+1] = temp;  
  16.                }  
  17.                 
  18.            }  
  19.        }  
  20.    }  
  21. }

快速排序

[java]  view plain copy
  1. public class QuickSort {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.         int a[]={12,32,4,11,54,33,75,44,8,10};  
  9.         if(a.length>0){  
  10.           quickSort(a,0,a.length-1);  
  11.         }  
  12.            for(int i=0;i<a.length;i++){  
  13.                System.out.print(a[i]+" ");  
  14.            }  
  15.     }  
  16.    public static int getMiddle(int[] a,int low,int high){  
  17.        int mid = a[low];  
  18.        while(low<high){  
  19.            //System.out.println(low+" "+high);  
  20.        while(low<high&&mid<=a[high]){  
  21.             
  22.            high--;  
  23.        }  
  24.        a[low] = a[high];  
  25.        while(low<high&&mid>=a[low]){  
  26.              
  27.            low++;  
  28.                      
  29.        }  
  30.        a[high] = a[low];  
  31.        }  
  32.        a[low] = mid;  
  33.        return low;  
  34.          
  35.    }  
  36.    public static void quickSort(int[] a,int low,int high){  
  37.        if(low<high){  
  38.            int middle = getMiddle(a,low,high);  
  39.            quickSort(a,low,middle-1);  
  40.            quickSort(a,middle+1,high);  
  41.        }  
  42.    }  
  43. }

四.归并排序

[cpp]  view plain copy
  1. public class MergingSort {  
  2.   
  3.     /** 
  4.      * @param args 
  5.      */  
  6.     public static void main(String[] args) {  
  7.         // TODO Auto-generated method stub  
  8.           int a[]={12,32,4,11,54,33,75,44,8,10};  
  9.            mergingSort(a,0,a.length-1);  
  10.            for(int i=0;i<a.length;i++){  
  11.                System.out.print(a[i]+" ");  
  12.            }  
  13.     }  
  14.     public static void mergingSort(int[] a,int left,int right){  
  15.         if(left<right){  
  16.             int center = (left+right)/2;  
  17.             mergingSort(a,left,center);  
  18.             mergingSort(a,center+1,right);  
  19.             meging(a,left,center,right);  
  20.         }  
  21.     }  
  22.     public static void meging(int[] a,int left,int center,int right){  
  23.         int[] tmpArr = new int[a.length];  
  24.         int mid = center+1;  
  25.         int s = left;  
  26.         int temp = left;  
  27.         while(left<=center&&mid<=right){  
  28.             if(a[left]<=a[mid]){  
  29.                 tmpArr[temp++] = a[left++];  
  30.             }else{  
  31.                 tmpArr[temp++] = a[mid++];  
  32.             }  
  33.         }  
  34.         while(mid<=right){  
  35.             tmpArr[temp++] = a[mid++];  
  36.         }  
  37.         while(left<=center){  
  38.             tmpArr[temp++] = a[left++];  
  39.         }  
  40.           
  41.         while(s<=right){  
  42.             a[s] = tmpArr[s++];  
  43.         }  
  44.     }  
  45. }

五.基数排序

import java.util.ArrayList;  
import java.util.List;  
  
  
public class RadixSort {  

    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        int a[]={12,32,4,11,54,33,75,44,8,10};  
           radixSort(a);  
           for(int i=0;i<a.length;i++){  
               System.out.print(a[i]+" ");  
           }  
    }  
      
    public static void radixSort(int[] a){  
        int max = a[0];  
        for(int i=0;i<a.length;i++){  
            if(a[i]>max){  
                max = a[i];  
            }  
        }  
        int time = 0;  
        while(max>0){  
            max = max/10;  
            time++;  
        }  
        List<ArrayList> queue = new ArrayList<ArrayList>();  
        for(int i=0;i<10;i++){  
            ArrayList<Integer> queue1 = new ArrayList<Integer>();  
            queue.add(queue1);  
        }  
        for(int i=0;i<time;i++){  
            for(int j=0;j<a.length;j++){  
                int x = a[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i);  
                ArrayList<Integer> queue2 = queue.get(x);  
                queue2.add(a[j]);  
                queue.set(x, queue2);  
            }  
            int count = 0;  
            for(int k=0;k<10;k++){  
                while(queue.get(k).size()>0){  
                 ArrayList<Integer> queue3 = queue.get(k);  
                    a[count++] = queue3.get(0);  
                    queue3.remove(0);  
                }  
            }  
        }  
    }  
  
}  


对于求职的应届生小伙伴们,必须熟记以上八种排序(当然重要是理解),面试的时候面试官可能会让你写一个你最拿手的排序算法(千万别写冒泡,丢不起那人!!!),本人建议,快排和堆排序选一个写,逼格瞬间提高,或者两个排序全都写,边写便介绍两种排序的优缺点和适用场景,绝对能让面试官刮目相看!!!!

面试的时候可能还会问及几种排序的时间复杂度和空间复杂度,再次不再多做介绍,网上大把大神写的资料等着你,本文只是讲八种排序算法的Java代码实现,方便初学的小伙伴编程的时候做个参考,以上代码都是本人亲自实现过的,可放心参考!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值