您的位置:首页 > 编程语言 > Java开发

Java实现快速排序

2014-08-15 09:05 357 查看
<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">        快速排序(Quicksort)是对</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/254413.htm" style="text-decoration: none; color: rgb(19, 110, 194); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">冒泡排序</a><span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">的一种改进。由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以</span><a target=_blank target="_blank" href="http://baike.baidu.com/view/96473.htm" style="text-decoration: none; color: rgb(19, 110, 194); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">递归</a><span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;">进行,以此达到整个数据变成有序序列。</span>
<span style="color: rgb(51, 51, 51); font-family: arial, 宋体, sans-serif; font-size: 14px; line-height: 24px; text-indent: 28px;"></span><div class="para" style="color: rgb(51, 51, 51); margin: 15px 0px 5px; text-indent: 2em; line-height: 24px; font-family: arial, 宋体, sans-serif; font-size: 14px;">设要排序的<a target=_blank target="_blank" href="http://baike.baidu.com/view/209670.htm" style="text-decoration: none; color: rgb(19, 110, 194);">数组</a>是A[0]……A[N-1],首先任意选取一个数据(通常选用数组的第一个数)作为关键数据,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序。值得注意的是,快速排序不是一种稳定的<a target=_blank target="_blank" href="http://baike.baidu.com/view/297739.htm" style="text-decoration: none; color: rgb(19, 110, 194);">排序算法</a>,也就是说,多个相同的值的相对位置也许会在算法结束时产生变动。</div><div class="para" style="color: rgb(51, 51, 51); margin: 15px 0px 5px; text-indent: 2em; line-height: 24px; font-family: arial, 宋体, sans-serif; font-size: 14px;">一趟快速排序的算法是:</div><div class="para" style="color: rgb(51, 51, 51); margin: 15px 0px 5px; text-indent: 2em; line-height: 24px; font-family: arial, 宋体, sans-serif; font-size: 14px;">1)设置两个<a target=_blank target="_blank" href="http://baike.baidu.com/view/296689.htm" style="text-decoration: none; color: rgb(19, 110, 194);">变量</a>i、j,<a target=_blank target="_blank" href="http://baike.baidu.com/view/58783.htm" style="text-decoration: none; color: rgb(19, 110, 194);">排序</a>开始的时候:i=0,j=N-1;</div><div class="para" style="color: rgb(51, 51, 51); margin: 15px 0px 5px; text-indent: 2em; line-height: 24px; font-family: arial, 宋体, sans-serif; font-size: 14px;">2)以第一个<a target=_blank target="_blank" href="http://baike.baidu.com/view/209670.htm" style="text-decoration: none; color: rgb(19, 110, 194);">数组</a>元素作为关键数据,赋值给<strong>key</strong>,即<strong>key</strong>=A[0];</div><div class="para" style="color: rgb(51, 51, 51); margin: 15px 0px 5px; text-indent: 2em; line-height: 24px; font-family: arial, 宋体, sans-serif; font-size: 14px;">3)从j开始向前搜索,即由后开始向前搜索(j--),找到第一个小于<strong>key</strong>的值A[j],将A[j]赋给A[i];</div><div class="para" style="color: rgb(51, 51, 51); margin: 15px 0px 5px; text-indent: 2em; line-height: 24px; font-family: arial, 宋体, sans-serif; font-size: 14px;">4)从i开始向后搜索,即由前开始向后搜索(i++),找到第一个大于<strong>key</strong>的A[i],将A[i]赋给A[j];</div><div class="para" style="color: rgb(51, 51, 51); margin: 15px 0px 5px; text-indent: 2em; line-height: 24px; font-family: arial, 宋体, sans-serif; font-size: 14px;">5)重复第3、4步,直到i=j; (3,4步中,没找到符合条件的值,即3中A[j]不小于<strong>key</strong>,4中A[i]不大于<strong>key</strong>的时候改变j、i的值,使得j=j-1,i=i+1,直至找到为止。找到符合条件的值,进行交换的时候i, j指针位置不变。另外,i==j这一过程一定正好是i+或j-完成的时候,此时令循环结束)。</div>
import java.util.*;

public class MyQuickSort{
    public static void main(String [] args){
        int length = 100;
        int [] arrays = new int[length];
        for(int i = 0; i < length; i++){
            arrays[i] = (int)(Math.random()*length);
        }
        long startTime = System.currentTimeMillis();
        arrays = quicksort(arrays , 0 , arrays.length-1);
        long endTime = System.currentTimeMillis();
        System.out.println(String.format("Time:%d",(endTime - startTime)));
        System.out.println(Arrays.toString(arrays));
    }
    
    
    public static int [] quicksort(int [] arrays, int start, int end){
        int i = start;
        int j = end;
        boolean flag = true;
        if(i >= j){
            return arrays;
        }
        while(i < j){
            if(arrays[i] > arrays[j]){
                arrays[i] = arrays[i] ^ arrays[j];
                arrays[j] = arrays[i] ^ arrays[j];
                arrays[i] = arrays[i] ^ arrays[j];
                flag = (flag == true) ? false : true;
            }
           
            if(flag){
                i++;
            }else{
                j--;
            }
        }
        i --;
        j ++;
        quicksort(arrays , start , i);
        quicksort(arrays , j ,end);
        return arrays;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: