归并排序

下面这个是我自己根据归并排序的原理来写,很容易理解。

package arithmetic;

public class QuickSort3 {
   
    public static void sort(Comparable[] data,int low,int high) {// 参照元,一般以第一个元素为基准进行划分 
    	int i = low; 
    	int j = high; 
    	if (low < high) {
    		// 从数组两端交替地向中间扫描 
    		Comparable pivotKey = data[low]; 
    		// 进行扫描的指针i,j;i从左边开始,j从右边开始 
    		while (i < j){ 
	    		while (i < j && data[j].compareTo(pivotKey) > 0) { 
	    			j--; 
	    		} 
	    		if (i < j) {// 比参照元素小的移动到左边 
	    			data[i] = data[j]; 
	    			i++; 
	    		} 
	    		while (i < j && data[i].compareTo(pivotKey) < 0){ 
	    			i++; 
	    		} 
	    		if (i < j) { 
	    			// 比参照元素大的移动到右边 
	    			data[j] = data[i]; 
	    			j--; 
	    		} 

    		} 
		    // 将参照元素移动到正确位置 
		    data[i] = pivotKey; 
		    for (Comparable c : data) 
		    { 
		        System.out.print(c+" "); 
		    }//输出每一轮的结果 
		    System.out.println(); 
		    // 前半个子表递归排序 
		    sort(data,low,i -1); 
		    // 后半个子表递归排序 
		    sort(data,(i+1),high); 
	    } 
    } 
    
        
    public static void main(String[] args) { 
        Comparable[] c = {45,21,31,41,5,50,43,1,67}; 
      
        sort(c,0,c.length- 1); 
        for (Comparable data : c) 
       { 
           System.out.print(data+" "); 
       } 
    } 


}


在网上也看到过另外一种实现方式,代码很简练,用一个指针方向的逻辑变量来判断游标的移动方向。但是好像没有去枢纽元,有些不太理解是怎么实现归并的。这里也贴一下这种实现的代码,希望有兴趣的朋友可以指教一下。

package arithmetic;

/*快速排序是实践中最快的已知排序算法,它的平局运行时间是O(N log N)。和归并排序一样,快速排序也是一种分治的递归算法。
 * 将数组S排序的基本算法由下列四步组成:
 * 1、如果S中元素个数是0或1,则返回;
 * 2、取S中任意元素v,称之为枢纽元(pivot);
 * 3、将S-{v}(S中其中元素)分成两个不相交的集合:S1、S2
 * 4、返回{quickSort(S1)后,继随v,继而quickSort(S2)}
 * 
 */
public class QuickSort {
	public static void main(String[] args) { 
	    //声明数组 
	    int[] nums = {27, 8, 57, 9, 23, 41, 65, 19, 0, 1, 2, 4, 5}; 
	    //应用快速排序方法 
	    quickSort(nums, 0, nums.length-1); 
	    //显示排序后的数组 
	    for(int i = 0; i < nums.length; ++i) { 
	      System.out.print(nums[i] + ","); 
	    } 
	    System.out.println(""); 
	} 

	/**快速排序方法*/ 
	public static void quickSort(int[] a, int lo0, int hi0) { 
	    int lo = lo0; 
	    int hi = hi0; 
	    if (lo >= hi) 
	      return; 
	    //确定指针方向的逻辑变量 
	    boolean transfer=true; 
	    while (lo != hi) { 
	      if (a[lo] > a[hi]) { 
	        //交换数字 
	        int temp = a[lo]; 
	        a[lo] = a[hi]; 
	        a[hi] = temp; 
	        //决定下标移动,还是上标移动 
	        transfer = (transfer == true) ? false : true; 
	      } 

	      //将指针向前或者向后移动 
	      if(transfer) 
	        hi--; 
	      else 
	        lo++; 

	      //显示每一次指针移动的数组数字的变化 
	      /*for(int i = 0; i < a.length; ++i) { 
	        System.out.print(a[i] + ","); 
	      } 
	      System.out.print(" (lo,hi) = " + "(" + lo + "," + hi + ")"); 
	      System.out.println("");*/ 
	    } 

	    //将数组分开两半,确定每个数字的正确位置 
	    lo--; 
	    hi++; 
	    quickSort(a, lo0, lo); 
	    quickSort(a, hi, hi0); 
	} 
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值