算法之快速排序----------用Java实现

来源:互联网 发布:中国银行软件中心 西安 编辑:程序博客网 时间:2024/04/30 02:39

快速排序是对冒泡排序的一种改进。它的基本思想是:通过一躺排序将要排序的数据分割成独立的两部分,
其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按次方法对这两部分数据分别进行快速排序,
整个排序过程可以递归进行,以此达到整个数据变成有序序列。

最坏情况的时间复杂度为O(n2),最好情况时间复杂度为O(nlog2n)。

以下是源代码:平台是eclipse Juno + Junit4.6

QuickSortDemo.java

public class QuickSortDemo {private int[] numbers;private int number;private void exchange(int i, int j){int temp = numbers[i];numbers[i] = numbers[j];numbers[j] = temp;}private void quickSort(int low, int high){int i = low, j = high;// Get the pivot element from the middle of the listint pivot = numbers[low + (high-low)/2];// Divide into two listswhile(i <= j){// If the current value from the left list is smaller than the pivot// element, then get the next element from the left listwhile(numbers[i] < pivot){i++;}// If the current value from the right list is larger than the pivot// element, then get the next element from the right listwhile(numbers[j] > pivot){j--;}// If we have found a values in the left list which is larger then// the pivot element and if we have found a value in the right list// which is smaller than the pivot element, then we exchange the values.// As we are done we can increase i and jif(i <= j){exchange(i, j);i++;j--;}}// Recursionif(low < j)quickSort(low, j);if(i < high)quickSort(i, high);}public void sort(int[] values){// check for empty or null arrayif(values == null || values.length == 0){return;}this.numbers = values;number = values.length;quickSort(0, number-1);}}
QuicksortTest.java

import java.util.Arrays;import java.util.Random;import org.junit.Assert;import org.junit.Before;import org.junit.Test;import org.junit.internal.runners.statements.Fail;public class QuicksortTest {private int[] numbers;private final static int SIZE = 7; private final static int MAX = 20;private boolean validate(int[] numbers){for(int i=0; i<numbers.length-1; i++){if(numbers[i] > numbers[i+1]){return false;}}return true;}private void printResult(int[] numbers){for(int i=0; i<numbers.length; i++){System.out.print(numbers[i]);}System.out.println();}@Beforepublic void setUp(){numbers = new int[SIZE];Random generator = new Random();for(int i=0; i<numbers.length; i++){numbers[i] = generator.nextInt(MAX);}}@Testpublic void testNull(){QuickSortDemo sorter = new QuickSortDemo();sorter.sort(null);}@Testpublic void testEmpty(){QuickSortDemo sorter = new QuickSortDemo();sorter.sort(new int[0]);}@Testpublic void testSimpleElement(){QuickSortDemo sorter = new QuickSortDemo();int[] test = new int[1];test[0] = 5;sorter.sort(test);}@Testpublic void testSpecial(){QuickSortDemo sorter = new QuickSortDemo();int[] test = {5,5,6,6,4,4,5,5,4,4,6,6,5,5};sorter.sort(test);if(!validate(test)){Assert.fail("Should not happen");}printResult(test);}@Testpublic void testQuickSort(){for(Integer i : numbers){System.out.println(i + " ");}long startTime = System.currentTimeMillis();QuickSortDemo sorter = new QuickSortDemo();sorter.sort(numbers);long stopTime = System.currentTimeMillis();long elapsedTime = stopTime - startTime;System.out.println("Quicksort " + elapsedTime);if(!validate(numbers)){Assert.fail("Should not happen");}Assert.assertTrue(true);}@Testpublic void testStandardSort(){long startTime = System.currentTimeMillis();Arrays.sort(numbers);long stopTime = System.currentTimeMillis();long elapsedTime = stopTime - startTime;System.out.println("Standard Java sort " + elapsedTime);if(!validate(numbers)){Assert.fail("Should not happen");}Assert.assertTrue(true);}}

测试结果是:


原创粉丝点击