⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quicksort.java

📁 利用快速排序算法对dat文件中保存的数据进行排序
💻 JAVA
字号:
//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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -