交换排序中快速排序的递归实现

package com.org.insert;

public class fastSort {
	public static int Partition(int a[],int low , int high) {
		
		int key;
		key = a[low];
		
		while(low<high) {
			while(low<high && a[high]>=key) 
				high--;
			
			a[low]=a[high];
			
			while(low<high && a[low]<=key) 
				low++;
			
			a[high]=a[low];
		}
		
		a[low]=key;
	
		return low;
	}
	static void QSort(int a[],int low, int high) {
		int mid;
		if(low<high) {
			mid = Partition(a, low, high);
			QSort(a, low, mid-1);
			QSort(a, mid+1, high);
		}
	}
	public static void main(String[] args) {
		int[] a = {3,1,55,76,12,99,23};
		QSort(a,0,6);
		for (int i : a) {
			System.out.print(i + " ");
		}
	}
}

 

发布了6 篇原创文章 · 获赞 4 · 访问量 4198
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览