2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 排序算法(插入 快速 归并)java实现

排序算法(插入 快速 归并)java实现

时间:2021-12-19 05:55:47

相关推荐

排序算法(插入 快速 归并)java实现

独角兽企业重金招聘Python工程师标准>>>

首先给上一张算法的复杂度

其中稳定性是指 数据中 有两个相同的元素,排序先后的相对位置是否变化,

插入排序

public static void insertSort(int[] data,int be,int end){for(int i=be;i<=end;i++){for(int j=i;j>be&&data[j-1]>data[j];j--){int temp=data[j];data[j]=data[j-1];data[j-1]=temp;}}System.out.println(Arrays.toString(data));}

快速排序

public static void qSort(int[] data,int be, int end){if(be>=end || data.length<=0){return ;}int temp = getMiddle(data, be,end);System.out.println("temp:" + temp);qSort(data,be,temp-1);qSort(data,temp+1,end);}private static int getMiddle(int[] data,int be,int end){int key = data[be];int first =be;while(be<end){while(be<end && key<=data[end] ){end--;}data[be]=data[end];while(be<end && key>=data[be]){be++;}data[end]=data[be];}data[be]=key;return be;}

归并排序

public static void mergeSort(int[] data,int be ,int end){if(be<end){int mid =(be+end)/2;mergeSort(data,be,mid);mergeSort(data,mid+1,end);merge(data,be,mid,end);}}// 合并,两个排序数据 的依次比较private static void merge(int[] data,int be,int center,int end){int lcur =be;int rcur =center+1;int m=be;int[] temp = new int[data.length];while(lcur<=center && rcur<=end){if(data[lcur]<data[rcur]){temp[m] =data[lcur];lcur++;}else{temp[m]=data[rcur];rcur++;}m++;}while(lcur<=center){temp[m++]=data[lcur++];}while(rcur<=end){temp[m++]=data[rcur++];}int tmp =be;while(tmp<=end){data[tmp]=temp[tmp];tmp++;}System.out.println(Arrays.toString(data));}

/uid-25906157-id-3318529.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。