JAVA快速排序程序
时间: 2023-08-31 08:32:13 浏览: 67
### 回答1:
我可以给你一些参考代码:
public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;
if (low >= high)
return;
// 挑选基准元素
int middle = low + (high - low) / 2;
int pivot = arr[middle];
// 数组分区
int i = low, j = high;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
// 递归调用
if (low < j)
quickSort(arr, low, j);
if (high > i)
quickSort(arr, i, high);
}
### 回答2:
JAVA快速排序是一种常用的排序算法,它基于分治的思想,通过将一个数组划分为较小的子数组,然后递归地对这些子数组进行排序,最终得到一个有序数组。
以下是一个示例的JAVA快速排序程序:
```java
public class QuickSort {
public static void main(String[] args) {
int[] arr = {5, 9, 3, 1, 6, 2, 8, 7, 4};
quickSort(arr, 0, arr.length - 1);
System.out.println("排序后的数组:");
for (int num : arr) {
System.out.print(num + " ");
}
}
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);
}
}
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++;
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;
}
}
```
通过以上示例程序,我们可以对一个包含整数的数组进行快速排序。快速排序的主要思想是通过选择一个pivot(一般选择数组中的最后一个元素),然后将数组中小于pivot的元素放在pivot的左边,大于pivot的元素放在pivot的右边。然后对pivot左边和右边的子数组进行递归排序。最终,所有子数组都被排序,整个数组也就完成了排序。
### 回答3:
快速排序是一种常见的排序算法,它的核心思想是通过分治的方法将一个序列按照某个元素分成两个子序列,再对两个子序列分别进行排序。具体步骤如下:
1. 选择一个基准元素(通常选择第一个元素),将序列分成两个部分。
2. 将小于基准元素的元素放在左边,大于基准元素的元素放在右边。
3. 递归地对左边和右边的两个子序列进行排序。
4. 合并排序后的左右子序列。
下面是一个使用Java实现快速排序的例子:
```java
public class QuickSort {
public 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 int partition(int[] arr, int low, int high) {
int pivot = arr[low]; // 将第一个元素作为基准元素
while (low < high) {
while (low < high && arr[high] >= pivot) {
high--;
}
arr[low] = arr[high]; // 比基准元素小的移到左边
while (low < high && arr[low] <= pivot) {
low++;
}
arr[high] = arr[low]; // 比基准元素大的移到右边
}
arr[low] = pivot; // 基准元素放到最终位置
return low; // 返回基准元素的索引
}
public static void main(String[] args) {
QuickSort quickSort = new QuickSort();
int[] arr = {6, 1, 8, 4, 3, 9, 7};
quickSort.quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
```
这个例子中,我们使用第一个元素作为基准元素,并使用low和high两个指针指向序列的起始和结束位置。在partition方法中,我们通过循环交换元素的位置,将小于基准元素的元素移到左边,大于基准元素的元素移到右边,最后将基准元素放到最终位置。在quickSort方法中,我们通过递归调用quickSort方法对左右子序列进行排序。最后,我们打印排序后的数组,结果为1 3 4 6 7 8 9。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)