快速排序是對冒泡排序的一種改進。它的基本思想是:通過一躺排序將要排序的數據分割成獨立的兩部分,
其中一部分的所有數據都比另外一部分的所有數據都要小,然後再按次方法對這兩部分數據分別進行快速排序,
整個排序過程可以遞歸進行,以此達到整個數據變成有序序列。
最壞情況的時間複雜度爲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 list
int pivot = numbers[low + (high-low)/2];
// Divide into two lists
while(i <= j){
// If the current value from the left list is smaller than the pivot
// element, then get the next element from the left list
while(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 list
while(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 j
if(i <= j){
exchange(i, j);
i++;
j--;
}
}
// Recursion
if(low < j)
quickSort(low, j);
if(i < high)
quickSort(i, high);
}
public void sort(int[] values){
// check for empty or null array
if(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();
}
@Before
public void setUp(){
numbers = new int[SIZE];
Random generator = new Random();
for(int i=0; i<numbers.length; i++){
numbers[i] = generator.nextInt(MAX);
}
}
@Test
public void testNull(){
QuickSortDemo sorter = new QuickSortDemo();
sorter.sort(null);
}
@Test
public void testEmpty(){
QuickSortDemo sorter = new QuickSortDemo();
sorter.sort(new int[0]);
}
@Test
public void testSimpleElement(){
QuickSortDemo sorter = new QuickSortDemo();
int[] test = new int[1];
test[0] = 5;
sorter.sort(test);
}
@Test
public 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);
}
@Test
public 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);
}
@Test
public 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);
}
}
測試結果是: