归并排序
程序员文章站
2022-05-21 17:14:01
...
归并排序的时间复杂度为
T(n)=T(n/2)+T(n/2)+2n-1 = O(nlogn),
该算法优于选择排序、插入排序和冒泡排序。
import java.util.Arrays;
public class MergeSort {
/**@author liuwei
* @param args
*/
public static void mergeSort(int[] data){
System.out.println("开始排序:");
sort(data,0,data.length-1);
}
/*将索引从left到right范围的数组元素进行归并排序
* data 待排序数组
* left 待排序数组的第一个元素索引
* right 待排序数组的最后一个元素索引
*/
private static void sort(int[] data, int left, int right) {
// TODO Auto-generated method stub
if(left<right){
//找出中间索引
int center=(left+right)/2;
//对左边数组进行递归
sort(data,left,center);
//对右边数组进行递归
sort(data,center+1,right);
//合并
merge(data,left,center,right);
}
}
/*将两个数组进行归并,归并前两个数组已经有序,归并后依然有序
* data 数组对象
* left 左数组的第一个元素的索引
* center 左数组的最后一个元素的索引,center+1是右数组第一个元素的索引
* right 右数组的最后一个元素的索引
*/
private static void merge(int[] data, int left, int center, int right) {
// TODO Auto-generated method stub
int [] tmpArr=new int[data.length];
int mid=center+1;
//third记录中间数组的索引
int third=left;
int tmp=left;
while(left<=center&&mid<=right){
//从两个数组中取出最小的放入中间数组
if(data[left] < (data[mid])){
tmpArr[third++]=data[left++];
}else{
tmpArr[third++]=data[mid++];
}
}
//剩余部分依次放入中间数组
while(mid<=right){
tmpArr[third++]=data[mid++];
}
while(left<=center){
tmpArr[third++]=data[left++];
}
//将中间数组中的内容复制回原数组
while(tmp<=right){
data[tmp]=tmpArr[tmp++];
}
System.out.println(Arrays.toString(data));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] data={21,30,49,30,16,9,-16,10,25,18};
System.out.println("排序之前:\n"+Arrays.toString(data));
mergeSort(data);
System.out.println("排序之后:\n"+Arrays.toString(data));
}
}
结果输出:
排序之前:
[21, 30, 49, 30, 16, 9, -16, 10, 25, 18]
开始排序:
[21, 30, 49, 30, 16, 9, -16, 10, 25, 18]
[21, 30, 49, 30, 16, 9, -16, 10, 25, 18]
[21, 30, 49, 16, 30, 9, -16, 10, 25, 18]
[16, 21, 30, 30, 49, 9, -16, 10, 25, 18]
[16, 21, 30, 30, 49, -16, 9, 10, 25, 18]
[16, 21, 30, 30, 49, -16, 9, 10, 25, 18]
[16, 21, 30, 30, 49, -16, 9, 10, 18, 25]
[16, 21, 30, 30, 49, -16, 9, 10, 18, 25]
[-16, 9, 10, 16, 18, 21, 25, 30, 30, 49]
排序之后:
[-16, 9, 10, 16, 18, 21, 25, 30, 30, 49]
T(n)=T(n/2)+T(n/2)+2n-1 = O(nlogn),
该算法优于选择排序、插入排序和冒泡排序。
import java.util.Arrays;
public class MergeSort {
/**@author liuwei
* @param args
*/
public static void mergeSort(int[] data){
System.out.println("开始排序:");
sort(data,0,data.length-1);
}
/*将索引从left到right范围的数组元素进行归并排序
* data 待排序数组
* left 待排序数组的第一个元素索引
* right 待排序数组的最后一个元素索引
*/
private static void sort(int[] data, int left, int right) {
// TODO Auto-generated method stub
if(left<right){
//找出中间索引
int center=(left+right)/2;
//对左边数组进行递归
sort(data,left,center);
//对右边数组进行递归
sort(data,center+1,right);
//合并
merge(data,left,center,right);
}
}
/*将两个数组进行归并,归并前两个数组已经有序,归并后依然有序
* data 数组对象
* left 左数组的第一个元素的索引
* center 左数组的最后一个元素的索引,center+1是右数组第一个元素的索引
* right 右数组的最后一个元素的索引
*/
private static void merge(int[] data, int left, int center, int right) {
// TODO Auto-generated method stub
int [] tmpArr=new int[data.length];
int mid=center+1;
//third记录中间数组的索引
int third=left;
int tmp=left;
while(left<=center&&mid<=right){
//从两个数组中取出最小的放入中间数组
if(data[left] < (data[mid])){
tmpArr[third++]=data[left++];
}else{
tmpArr[third++]=data[mid++];
}
}
//剩余部分依次放入中间数组
while(mid<=right){
tmpArr[third++]=data[mid++];
}
while(left<=center){
tmpArr[third++]=data[left++];
}
//将中间数组中的内容复制回原数组
while(tmp<=right){
data[tmp]=tmpArr[tmp++];
}
System.out.println(Arrays.toString(data));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] data={21,30,49,30,16,9,-16,10,25,18};
System.out.println("排序之前:\n"+Arrays.toString(data));
mergeSort(data);
System.out.println("排序之后:\n"+Arrays.toString(data));
}
}
结果输出:
排序之前:
[21, 30, 49, 30, 16, 9, -16, 10, 25, 18]
开始排序:
[21, 30, 49, 30, 16, 9, -16, 10, 25, 18]
[21, 30, 49, 30, 16, 9, -16, 10, 25, 18]
[21, 30, 49, 16, 30, 9, -16, 10, 25, 18]
[16, 21, 30, 30, 49, 9, -16, 10, 25, 18]
[16, 21, 30, 30, 49, -16, 9, 10, 25, 18]
[16, 21, 30, 30, 49, -16, 9, 10, 25, 18]
[16, 21, 30, 30, 49, -16, 9, 10, 18, 25]
[16, 21, 30, 30, 49, -16, 9, 10, 18, 25]
[-16, 9, 10, 16, 18, 21, 25, 30, 30, 49]
排序之后:
[-16, 9, 10, 16, 18, 21, 25, 30, 30, 49]