Java实现快速排序算法

4 篇文章 0 订阅

之前的博客中其实已经写过有关于快速排序的算法了,之所以再写一遍,是因为快速排序的应用场景还算是多的,而且在Java面试过程中有不小的几率会考察到快速排序的算法,这里我们只提供算法,至于详细的讲解,请看我之前的博客:

《Java实现快速排序算法》链接:https://blog.csdn.net/IBLiplus/article/details/81056945

package paixu;

public class FastSortSelf {
	public static void sort(int []x ,int head,int foot) {
		int left = head;
		int right = foot;
		while(left<right) {
			int key = x[left];
			while(left<right&&x[right]>key) {
				right--;
			}
			if(x[right]<=key) {
				int temp = x[right];
				x[right] = x[left];
				x[left] = temp;
			}
			while(left<right&&x[left]<key) {
				left++;
			}
			if(x[left]<=key) {
				int temp = x[right];
				x[right] = x[left];
				x[left] = temp;
			}
		}
		if(left>head) {sort(x,head,left-1);}
		if(right<foot) {sort(x,right+1,foot);}
	}
	
	public static void main(String[] args) {
		int [] x = {12,20,5,16,15,10,30,45};
		sort(x,0,x.length-1);
		for(int i = 0;i<x.length;i++) {
			System.out.print(x[i]+",");
		}
	}
	
	
	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值