首页>代码>java swing动画演示各种排序算法的过程>/SortAnimation/src/SortAlgorithms/Quick.java
package SortAlgorithms;

import UI.AnimationPanel;

public class Quick
{
	public volatile static int changing;
	public volatile static int position;

	@SuppressWarnings("rawtypes")
	public static void sort(final Comparable[] a)
	{
		new Thread()
		{
			@Override
			public void run()
			{
				sort(a, 0, a.length - 1);
			}
		}.start();
	}

	@SuppressWarnings("rawtypes")
	private static void sort(Comparable[] a, int lo, int hi)
	{
		if (lo >= hi)
			return;
		int j = partition(a, lo, hi);
		sort(a, lo, j - 1);
		sort(a, j + 1, hi);
	}

	@SuppressWarnings("rawtypes")
	private static int partition(Comparable[] a, int lo, int hi)
	{
		int i = lo, j = hi + 1;
		Comparable v = a[lo];
		while (true)
		{
			while (less(a[++i], v))
				if (i == hi)
					break;
			while (less(v, a[--j]))
				if (j == lo)
					break;
			if (i >= j)
				break;
			try
			{
				position = i;
				changing = j;
				Thread.sleep(AnimationPanel.SPF);
			} catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			exch(a, i, j);
		}
		try
		{
			position = lo;
			changing = j;
			Thread.sleep(AnimationPanel.SPF);
		} catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		exch(a, lo, j);
		return j;
	}

	@SuppressWarnings("rawtypes")
	private static void exch(Comparable[] a, int i, int j)
	{
		Comparable tmp = a[i];
		a[i] = a[j];
		a[j] = tmp;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static boolean less(Comparable x, Comparable y)
	{
		return x.compareTo(y) < 0;
	}

	@SuppressWarnings("rawtypes")
	public static void show(Comparable[] a)
	{
		for (int i = 0; i < a.length; i++)
			System.out.print(a[i] + " ");
		System.out.println();
	}

	@SuppressWarnings("rawtypes")
	public static boolean isSorted(Comparable[] a)
	{
		for (int i = 1; i < a.length; i++)
			if (less(a[i], a[i - 1]))
				return false;
		return true;
	}
}
最近下载更多
xingxing1234  LV10 2023年3月22日
WASDZZ  LV13 2021年10月27日
zm2580hh  LV1 2021年1月15日
cyszzc  LV1 2020年12月16日
最代码官方  LV167 2020年5月31日
黄泽文  LV1 2020年5月25日
wei1101025  LV15 2020年3月24日
ysc123  LV5 2020年2月21日
wzx2e3  LV1 2019年12月22日
1147692290  LV1 2019年8月24日
最近浏览更多
xingxing1234  LV10 2023年3月22日
微信网友_6040315240812544  LV8 2022年10月27日
liys1234  LV9 2022年4月22日
1265260263  LV4 2022年4月8日
fudonglin0514  LV6 2021年12月4日
WASDZZ  LV13 2021年10月27日
v  LV1 2021年6月12日
ham123456 2021年4月16日
暂无贡献等级
zm2580hh  LV1 2021年1月15日
cyszzc  LV1 2020年12月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友