Java排序--快速排序

package 排序;
/**  
 * 快速排序<br/>  
 * <ul>  
 * <li>从数列中挑出一个元素(这里取第一个数),称为“基准”</li>  
 * <li>重新排序数列,所有元素比基准值小的摆放在基准前面,所有元素比基准值大的摆在基准的后面。在这个分割之后,  
 * 该基准是它的最后位置。这个称为分割(partition)操作。</li> 
 * <li>从右边开始找,再左边</li> 
 * <li>递归地把小于基准值元素的子数列和大于基准值元素的子数列排序。</li>  
 * </ul>  
 */ 
public class Kuaisu {
	public static void main(String[] args) {
		int[] arr= {1,9,5,3,4,6,2,7,8};
		sort(arr,0,arr.length-1);
		for(int i:arr) {
			System.out.print(i+"  ");
		}
	}
	public static void sort(int[] arr,int low,int high) {
		if(arr==null||arr.length==0||low>high) {
			return ;
		}
		int i,j,temp,t;
		i=low;
		j=high;
		//temp就是基准位
		temp=arr[low];
		while(i<j){
			//先看右边,依次往左递减,包括相等
			while(arr[j]>=temp&&i<j) {
				j--;
			}
			//再看右边,依次往右递增,包括相等
			while(arr[i]<=temp&&i<j) {
				i++;
			}
			//如果满足条件则交换
			if(i<j) {
				t=arr[i];
				arr[i]=arr[j];
				arr[j]=t;
			}
		}
		//最后将基准位i和j相等位置的数字交换
		arr[low]=arr[i];
		arr[i]=temp;
		//递归调用左半数组
		sort(arr, low, j-1);
		//递归调用右半数组
		sort(arr, j+1, high);
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是快速排序代码: public class QuickSort { public static void sort(int[] arr, int start, int end){ if (start < end){ int partitionIndex = partition(arr, start, end); sort(arr, start, partitionIndex-1); sort(arr, partitionIndex+1, end); } } private static int partition(int[] arr, int start, int end){ int pivot = arr[end]; int i = start-1; for (int j=start; j<end; j++){ if (arr[j] <= pivot){ i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i+1]; arr[i+1] = arr[end]; arr[end] = temp; return i+1; } } ### 回答2: 快速排序(Quick Sort)是一种常用的排序算法,通过将待排序的序列分割成两部分,其中一部分所有元素都小于等于另一部分所有元素,然后对这两部分递归地进行排序,最终得到一个有序序列。 下面是使用Java编写的快速排序代码示例: ```java public class QuickSort { public static void main(String[] args) { int[] array = {5, 2, 9, 4, 7, 1, 6, 8, 3}; quickSort(array, 0, array.length - 1); for (int num : array) { System.out.print(num + " "); } } public static void quickSort(int[] array, int left, int right) { if (left < right) { int pivot = partition(array, left, right); quickSort(array, left, pivot - 1); quickSort(array, pivot + 1, right); } } public static int partition(int[] array, int left, int right) { int pivot = array[right]; int i = left - 1; for (int j = left; j < right; j++) { if (array[j] <= pivot) { i++; swap(array, i, j); } } swap(array, i + 1, right); return i + 1; } public static void swap(int[] array, int i, int j) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } ``` 以上代码定义了一个`QuickSort`类,其中包含了`main`方法作为程序入口,以及三个静态方法`quickSort`、`partition`和`swap`来实现快速排序算法。 在`main`方法中,首先定义了一个待排序的数组`array`,然后调用`quickSort`方法对该数组进行排序,最后将排序结果输出。 `quickSort`方法用于递归地进行快速排序,它接收一个数组、一个左边界索引`left`和一个右边界索引`right`作为参数。在`quickSort`方法中,首先通过`partition`方法将数组分割为两部分,然后对这两部分分别调用`quickSort`方法进行递归排序。 `partition`方法用于分割数组,它接收一个数组、一个左边界索引`left`和一个右边界索引`right`作为参数。在`partition`方法中,首先选择数组的最后一个元素作为基准值`pivot`,然后通过一个循环将小于等于基准值的元素放置在左侧,大于基准值的元素放置在右侧,最后将基准值放置在中间位置,并返回该位置索引。 `swap`方法用于交换数组中的两个元素,它接收一个数组和两个索引作为参数,然后将对应索引位置的元素进行交换。 以上就是使用Java生成快速排序代码的简单示例。 ### 回答3: 快速排序是一种常用的排序算法,它基于分区的概念,通过找到一个基准元素,将数组分成两个子数组,其中一个子数组的所有元素小于等于基准元素,另一个子数组的所有元素大于等于基准元素,然后递归地对子数组进行排序。 在Java中,可以使用以下代码来实现快速排序: ```java public class QuickSort { public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pivotIndex = partition(arr, low, high); quickSort(arr, low, pivotIndex - 1); quickSort(arr, pivotIndex + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] <= pivot) { i++; swap(arr, i, j); } } swap(arr, i + 1, high); return i + 1; } public static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void main(String[] args) { int[] arr = {9, 2, 5, 1, 0, 8, 3}; quickSort(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } } ``` 运行这段代码会将数组 `{9, 2, 5, 1, 0, 8, 3}` 进行快速排序,并输出排序后的结果:`0 1 2 3 5 8 9`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值