java快速排序

/*
 * 
 * 功能:快速排序
 */

package com.haitao.array;

import java.util.Calendar;

public class Demo5_6 {

	public static void main(String[] args) {
		
		int len = 800000;
		int[] arr1=new int[len];
		for(int i=0;i<len;i++)
		{
			//让程序随机产生一组1-1000数
			//Math.random()会产生一个0~1
			int t=(int)(Math.random()*1000);
			arr1[i]=t;
	
		}
		
		//创建一个新实例
			InsertSort is=new InsertSort();
			//获取排序前的时间
			Calendar cal=Calendar.getInstance();
			System.out.println("排序前:"+cal.getTime());
			//排序
			is.sort(arr1);
			//重新得到实例
			cal=Calendar.getInstance();
			System.out.println("排序后:"+cal.getTime());
			
//			for(int j=0;j<arr1.length;j++)
//			{
//				System.out.print(" "+arr1[j]+"  ");
//				
//				
//			}

		
		
	

	}

}


//快速排序
class QuickSort
{

	public static void sort(int[] arr) {
        innerQuickSort(arr, 0, arr.length - 1);
    }
 
    private static void innerQuickSort(int[] arr, int head, int end) {
        if (head >= end) {
            return;
        }
        int index = head;
        for (int i = head; i < end; i++) {
            if (arr[i] <= arr[end] && index != i) {
                arr[index] ^= arr[i];
                arr[i] ^= arr[index];
                arr[index] ^= arr[i];
                index++;
            }
        }
        if (index != end) {
            arr[index] ^= arr[end];
            arr[end] ^= arr[index];
            arr[index] ^= arr[end];
        }
        innerQuickSort(arr, head, index - 1);
        innerQuickSort(arr, index + 1, end);
    }
 

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值