java编写快速排序算法
时间: 2023-08-31 14:27:04 浏览: 74
### 回答1:
Java 版快速排序算法如下:
```
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
private 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++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}
```
主函数调用方式如下:
```
int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
QuickSort.quickSort(arr, 0, arr.length - 1);
```
### 回答2:
快速排序是一种经典的排序算法,其基本思想是通过遍历数组,将小于某个基准值的元素移到基准值的左边,将大于基准值的元素移到基准值的右边。然后分别对基准值左右两侧的子数组进行递归排序,直到子数组的长度为1或0,排序完成。
在Java中,我们可以使用以下代码实现快速排序算法:
```java
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivot = partition(arr, low, high); // 获取基准值的位置
quickSort(arr, low, pivot - 1); // 对左侧子数组进行排序
quickSort(arr, pivot + 1, high); // 对右侧子数组进行排序
}
}
private static int partition(int[] arr, int low, int high) {
int pivot = arr[low]; // 选择第一个元素作为基准值
int i = low; // 左右指针
int j = high;
while (i < j) {
// 从右向左找到第一个小于基准值的元素
while (i < j && arr[j] >= pivot) {
j--;
}
// 将找到的小于基准值的元素移到左侧
arr[i] = arr[j];
// 从左向右找到第一个大于基准值的元素
while (i < j && arr[i] <= pivot) {
i++;
}
// 将找到的大于基准值的元素移到右侧
arr[j] = arr[i];
}
// 将基准值放入正确的位置
arr[i] = pivot;
return i;
}
public static void main(String[] args) {
int[] arr = {5, 2, 7, 3, 9, 1, 6};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
```
运行以上代码,输出结果为:1 2 3 5 6 7 9,即为排序后的数组。
### 回答3:
快速排序算法是一种经典的排序算法,其基本思想是通过一次划分过程将待排序序列分成两个子序列,其中一个子序列的所有元素小于划分元素,另一个子序列的所有元素大于划分元素,然后再对子序列进行递归划分,直到每个子序列只有一个元素为止。
具体实现快速排序算法的关键步骤如下:
1. 选择一个划分元素,一般选择待排序序列的第一个元素,记为pivot。
2. 初始化两个指针,left指向待排序序列的开始位置,right指向待排序序列的结束位置。
3. 从right开始,不断向左移动left指针,直到找到一个元素小于等于pivot为止。
4. 从left开始,不断向右移动right指针,直到找到一个元素大于等于pivot为止。
5. 如果left<right,则交换left指向的元素和right指向的元素。
6. 重复步骤3-5,直到left>=right。
7. 将pivot与left指向的元素交换位置,即将pivot放到它最终的位置上。
8. 对pivot左边的子序列和右边的子序列分别进行递归快速排序。
以下是用Java编写快速排序算法的示例代码:
```
public class QuickSort {
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int pivotIndex = partition(arr, left, right); // 划分数组
quickSort(arr, left, pivotIndex - 1); // 对左子序列进行递归快速排序
quickSort(arr, pivotIndex + 1, right); // 对右子序列进行递归快速排序
}
}
private static int partition(int[] arr, int left, int right) {
int pivot = arr[left]; // 选择第一个元素作为划分元素
while (left < right) {
while (left < right && arr[right] >= pivot) {
right--;
}
arr[left] = arr[right];
while (left < right && arr[left] <= pivot) {
left++;
}
arr[right] = arr[left];
}
arr[left] = pivot;
return left;
}
public static void main(String[] args) {
int[] arr = {7, 3, 9, 5, 1, 6};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
```
以上就是用Java编写快速排序算法的方法和示例代码。通过调用quickSort方法即可对待排序数组进行排序,最终结果会以升序输出。