quick sort _JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > quick sort

quick sort

 2011/11/14 6:40:03  fx05062219  http://javalovers.iteye.com  我要评论(0)
  • 摘要:packagetech.algorithms.sort;importjava.util.Random;publicclassQuickSort{publicstaticvoidmain(String[]args){//int[]array={13,19,9,5,1,2,8,7,4,23,2,6,11};int[]array=newint[100];Randomr=newRandom();for(inti=0;i<array.length;i++){array[i]=r.nextInt
  • 标签:
package tech.algorithms.sort;

import java.util.Random;

public class QuickSort {

	public static void main(String[] args) {
		// int[] array = { 13, 19, 9, 5, 1, 2, 8, 7, 4, 23, 2, 6, 11 };
		int[] array = new int[100];
		Random r = new Random();
		for (int i = 0; i < array.length; i++) {
			array[i] = r.nextInt(100);
		}
		QuickSort qs = new QuickSort();

		qs.sort(array, 0, array.length - 1);

		for (int loopIndex = 0; loopIndex < array.length; loopIndex++) {
			System.out.println(array[loopIndex]);
		}
	}

	private void sort(int[] array, int bottom, int top) {
		if (bottom < top) {
			int divdePoint = partition(array, bottom, top);
			sort(array, bottom, divdePoint - 1);
			sort(array, divdePoint + 1, top);
		}

	}

	private int partition(int[] array, int bottom, int top) {

		int divideValue = array[top];

		int i = bottom;
		for (int loopIndex = bottom; loopIndex < top; loopIndex++) {
			if (array[loopIndex] < divideValue) {
				int temp = array[loopIndex];
				array[loopIndex] = array[i];
				array[i] = temp;
				i++;
			}
		}

		int temp = array[top];
		array[top] = array[i];
		array[i] = temp;
		return i;

	}
}
  • 相关文章
发表评论
用户名: 匿名