quicksort.java

来自「利用快速排序算法对dat文件中保存的数据进行排序」· Java 代码 · 共 63 行

JAVA
63
字号
//Sort 100 numbers which are stored in a file 
//through the quick sort

public class QuickSort{
	
	static int a[] ={2,5,3,4,8,9,10,7,1,6};
	//static int[] a= new int[10];
	//a[10] ={2,5,3,4,8,9,10,7,1,6};
	
	public static void qSort(int m, int n)
	{
		if(m < n){
			
			int l = partition(m, n);
			qSort(m, l - 1); //对左半段排序
			qSort(l + 1, n); //对右半段排序
		}
	}
	
	public static int partition(int m, int n)
	{
		int i = m,
		    j = n + 1;
		    
		int x = a[m];
		
		while(true){
			
			while(a[++i] < x);
			while(a[--j] > x);
			
			if(i >= j)
				break;
				
			swap(a, i , j);
	
		}
		
		a[m] = a[j];
		a[j] = x;
		
		return j;
		    
	}
	public static void main(String args[])
	{
		qSort(0,9);
		
		for(int n=0; n < a.length; n++)
			System.out.println(""+ a[n]);
		
		
	}

	public static void swap(int []a, int x, int y)
	{
		int temp;

		temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}
}

⌨️ 快捷键说明

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