quicksorter.java

来自「A specific version of a quicksorter whic」· Java 代码 · 共 51 行

JAVA
51
字号

//
// This class sorts an array using the QuickSort method.

public class QuickSorter
{
	private int[] a;

	public QuickSorter(int[] anArray)
	{
		a=anArray;
	}

	public void sort(int from, int to)
	{
		if (from>=to)
			return;

		int p = partition(from,to);
		sort(from,p);
		sort(p+1,to);
	}

	private int partition(int from, int to)
	{
		int temp;
		int pivot = a[from];
		int i = from-1;
		int j = to +1;

		while(i<j)
		{
			i++;
			while (a[i]<pivot)
				i++;

			j--;

			while(a[j]>pivot)
				j--;

			if(i<j)
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
		return j;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?