首先通过下面一张图了解排序算法的分类:
下面是八种排序算法的Java代码实现:
一.插入排序
简单插入排序
- public class InsertSort {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a[]={12,32,4,11,54,33,75,44,8,10};
- insertSort(a);
- for(int i=0;i<a.length;i++){
- System.out.print(a[i]+" ");
- }
- }
- public static void insertSort(int[] a){
- int temp = 0;
- for(int i=1;i<a.length;i++){
- temp = a[i];
- int j;
- for(j=i-1;j>=0;j--){
- if(temp<a[j]){
- a[j+1] = a[j];
- //a[j] =temp;
- }else{
- break;
- }
- }
- a[j+1] =temp;
- }
- }
- }
- public class ErfenSort {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a[]={12,32,4,11,54,33,75,44,8,10};
- erfenSort(a);
- for(int i=0;i<a.length;i++){
- System.out.print(a[i]+" ");
- }
- }
- public static void erfenSort(int[] a){
- int temp=0;
- for(int i=1;i<a.length;i++){
- temp = a[i];
- int left=0;
- int right = i-1;
- int mid = 0;
- while(left<=right){
- mid = (left+right)/2;
- if(a[mid]>temp){
- right = mid-1;
- }else{
- left = mid+1;
- }
- }
- for(int j=i-1;j>=left;j--){
- a[j+1] = a[j];
- }
- if(left!=i){
- a[left] = temp;
- }
- }
- }
- }
希尔排序
- public class ShellSort {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a[]={12,32,4,11,54,33,75,44,8,10};
- shellSort(a);
- for(int i=0;i<a.length;i++){
- System.out.print(a[i]+" ");
- }
- }
- public static void shellSort(int[] a){
- int d = a.length;
- int temp = 0;
- while(d!=1){
- d = d/2;
- for(int x=0;x<d;x++){
- for(int i=x+d;i<a.length;i+=d){
- temp = a[i];
- int j;
- for(j=i-d;j>=0;j-=d){
- if(temp<a[j]){
- a[j+d] = a[j];
- //a[j] = temp;
- }else{
- break;
- }
- }
- a[j+d] = temp;
- }
- }
- }
- }
- }
二.选择排序
简单选择排序
- public class SelectSort {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a[]={12,32,4,11,54,33,75,44,8,10};
- selectSort(a);
- for(int i=0;i<a.length;i++){
- System.out.print(a[i]+" ");
- }
- }
- public static void selectSort(int[] a){
- int temp = 0;
- for(int i=0;i<a.length;i++){
- int p = i;
- temp = a[i];
- int j;
- for(j=i+1;j<a.length;j++){
- if(temp>a[j]){
- temp = a[j];
- p = j;
- }
- }
- a[p] = a[i];
- a[i] = temp;
- }
- }
- }
- public class HeapSort {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a[]={12,32,4,11,54,33,75,44,8,10};
- heapSort(a);
- for(int i=0;i<a.length;i++){
- System.out.print(a[i]+" ");
- }
- }
- private static void swap(int[] a,int i,int j){
- int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- public static void heapSort(int[] a){
- for(int i=0;i<a.length;i++){
- buildMaxHeap(a,a.length-1-i);
- swap(a,0,a.length-1-i);
- }
- }
- private static void buildMaxHeap(int[]a,int lastIndex){
- for(int i=(lastIndex-1)/2;i>=0;i--){
- int k=i;
- while(2*k+1<=lastIndex){
- int biggerIndex = 2*k+1;
- if(biggerIndex<lastIndex){
- if(a[biggerIndex]<a[biggerIndex+1]){
- biggerIndex++;
- }
- }
- if(a[k]<a[biggerIndex]){
- swap(a,k,biggerIndex);
- k = biggerIndex;
- }else{
- break;
- }
- }
- }
- }
- }
三.交换排序
冒泡排序
- public class BubbleSort {
- public static void main(String[] args){
- int a[]={12,32,4,11,54,33,75,44,8,10};
- bubbleSort(a);
- for(int i=0;i<a.length;i++){
- System.out.print(a[i]+" ");
- }
- }
- public static void bubbleSort(int[] a){
- for(int i=0;i<a.length-1;i++){
- for(int j=0;j<a.length-1-i;j++){
- if(a[j]>a[j+1]){
- int temp = a[j];
- a[j] = a[j+1];
- a[j+1] = temp;
- }
- }
- }
- }
- }
快速排序
- public class QuickSort {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a[]={12,32,4,11,54,33,75,44,8,10};
- if(a.length>0){
- quickSort(a,0,a.length-1);
- }
- for(int i=0;i<a.length;i++){
- System.out.print(a[i]+" ");
- }
- }
- public static int getMiddle(int[] a,int low,int high){
- int mid = a[low];
- while(low<high){
- //System.out.println(low+" "+high);
- while(low<high&&mid<=a[high]){
- high--;
- }
- a[low] = a[high];
- while(low<high&&mid>=a[low]){
- low++;
- }
- a[high] = a[low];
- }
- a[low] = mid;
- return low;
- }
- public static void quickSort(int[] a,int low,int high){
- if(low<high){
- int middle = getMiddle(a,low,high);
- quickSort(a,low,middle-1);
- quickSort(a,middle+1,high);
- }
- }
- }
四.归并排序
- public class MergingSort {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int a[]={12,32,4,11,54,33,75,44,8,10};
- mergingSort(a,0,a.length-1);
- for(int i=0;i<a.length;i++){
- System.out.print(a[i]+" ");
- }
- }
- public static void mergingSort(int[] a,int left,int right){
- if(left<right){
- int center = (left+right)/2;
- mergingSort(a,left,center);
- mergingSort(a,center+1,right);
- meging(a,left,center,right);
- }
- }
- public static void meging(int[] a,int left,int center,int right){
- int[] tmpArr = new int[a.length];
- int mid = center+1;
- int s = left;
- int temp = left;
- while(left<=center&&mid<=right){
- if(a[left]<=a[mid]){
- tmpArr[temp++] = a[left++];
- }else{
- tmpArr[temp++] = a[mid++];
- }
- }
- while(mid<=right){
- tmpArr[temp++] = a[mid++];
- }
- while(left<=center){
- tmpArr[temp++] = a[left++];
- }
- while(s<=right){
- a[s] = tmpArr[s++];
- }
- }
- }
五.基数排序
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代码实现,方便初学的小伙伴编程的时候做个参考,以上代码都是本人亲自实现过的,可放心参考!!!