【快速排序法】

【快速排序法】
也是一種交換排序法,實際上它是冒泡法的改進。

【算法】
思想是:一次劃分使整個元素部分有序,即從序列中任選一個元素,然後對其它元素進行這樣的劃分,使得所有比它小(大)的元素在左側而所有比它大(小)的元素在右側,然後用分治的思想對左右側的序列同樣的處理,直到只有一個元素爲止。

 

 

/**
 * 
 */
package com.dianzi.arith;

/**
 * 快速排序,整個過程用遞歸算法設計,思想是分治的思想
 * @author 點子二木
 * @date 2009-5-12
 * @version 1.0
 */
public class Quick {
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		int list_length = 10;
		InitList list = new InitList(list_length);
		Quick quick = new Quick();

		int[] tempArray = list.toArrayInt();

		int[] resultArray = quick.quicksort(tempArray, 0, list_length - 1);

		System.out.println("");
		System.out.print("快速排序:");
		for (int i = 0; i < resultArray.length; i++) {
			System.out.print(resultArray[i] + " ");
		}

	}

	/**
	 * 整個快速排序的過程用遞歸算法設計,思想當然是分治的思想:
	 * 
	 * @param tempArray
	 * @param low
	 * @param high
	 * @return
	 */
	public int[] quicksort(int[] tempArray, int low, int high) {
		// 對r[low],r[low+1],...r[high]進行快速排序
		if (low < high) {
			int k;
			k = quickpass(tempArray, low, high);// 一次劃分
			quicksort(tempArray, low, k - 1);// 對左側元素以同樣的方法對待
			quicksort(tempArray, k + 1, high);// 對右側元素以同樣的方法對待
		}
		return tempArray;
	}

	/**
	 * 一次劃分的算法如下:
	 * 
	 * @param tempArray
	 * @param low
	 * @param high
	 * @return
	 */
	public int quickpass(int[] tempArray, int low, int high) {
		int i = low, j = high, x = tempArray[i]; // 置初值,i指最左端,j指最右端,選第i個元素爲劃分比較元素x
		while (i < j) {
			while ((tempArray[j] >= x) && (i < j))
				--j;
			// 自尾端進行比較,因爲i處爲空
			if (i < j) {
				tempArray[i] = tempArray[j];
				i++;// 填入i處
				while (tempArray[i] <= x && (i < j)) {
					++i;
				}
				// 自首端進行比較,j處已空
				if (i < j) {
					tempArray[j] = tempArray[i];
					j--;
				}
			}
		}
		tempArray[i] = x;// 一趟快速排序結束,將x移至正確位置
		return i;// 返回劃分比較元素的位置
	}

}

 

 

初始化數組:

/**
 * 
 */
package com.dianzi.arith;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 初始化數組
 * @author 點子二木
 * @date 2009-5-12
 * @version 1.0
 */
public class InitList {
	private static int INIT_LIST_LENGTH = 1;
	public  List global_List = null;

	public InitList(int length) {
		initIntArrayList_falseRandom(length);
		//initIntArrayList(length);
		printList();
	}
	
	/**
	 * 產生僞隨機列表
	 * @param length
	 */
	public void initIntArrayList_falseRandom(int length) {
		global_List = new ArrayList(length);
		INIT_LIST_LENGTH = length;
		Random rd = new Random(20);
		for (int i = 0; i < length; i++) {
			int newNum =  rd.nextInt(100);
			global_List.add(newNum);
			//System.out.print(newNum + " ");
		}

	}
	

	/**
	 * 產生隨機列表
	 * @param length
	 */
	public void initIntArrayList(int length) {
		global_List = new ArrayList(length);
		INIT_LIST_LENGTH = length;
		
		for (int i = 0; i < length; i++) {
			Integer newNum = Integer.valueOf((int) (Math.random() * 100)) ;
			global_List.add(newNum);
			//System.out.print(newNum + " ");
		}

	}

	public void printList() {
		if (global_List != null) {
			System.out.println("");
			System.out.print("打印列表:");
			for (int i = 0; i < global_List.size(); i++) {
				System.out.print(global_List.get(i) + " ");
			}
		}

	}
	
	public int[] toArrayInt() {
		int[] result = new int[INIT_LIST_LENGTH];
		if (global_List != null) {
			for (int i = 0; i < global_List.size(); i++) {
				result[i] = (Integer) global_List.get(i);
			}
		}
		return result;
	}

}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章