快速排序初学笔记整理

package com.jzx.quicksort;

public class TestQuckSort {

public static void main(String[] args) {
	//输入数组
	int[] arr = {67,35,68,79,62,1,45,8,10};
	int low = 0;
	int high = arr.length-1;
	//打印原有数组
	for (int i : arr) {
		System.out.print(i+"\t");
	}
	System.out.println();
	//排序
	quickSort(arr,low,high);
	//输出打印数组
	for (int i : arr) {
		System.out.print(i+"\t");
	}
	
	
	
}


private static void quickSort(int[] arr ,int low ,int high) {
	//递归条件low<high
	if(low<high){
		//分区返回索引值
		int index = patition( arr , low , high);
		//左边递归
		quickSort(arr, low , index-1);
		//右边递归
		quickSort(arr, index+1 , high);	
	}
	
}

private static int patition(int[] arr, int low,int high) {
	
	//第一把基准值设置为第一个放入临时变量中
	int temp=arr[low];
	//填坑1、循环右边的数组把小于基准数的填入左边的坑中;再把左边的数组大于基准数的值填入右边边的坑
	while (low<high) {
		//填左边的坑
		while ( low < high && arr[high] >= temp) {
			
			high--;
			
		}
		arr[low] = arr[high];
		//a填右边的坑
		while (low < high &&arr[low] <= temp ) {
			
			low++;
				
		}
		arr[high] =arr[low];
			
		}
	
		
	
	//返回當前索引
	arr[low] = temp;
	return low;
	
	
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值