show me the quick sort code by java
时间: 2023-08-31 13:28:51 浏览: 70
### 回答1:
以下是 Java 中快速排序的代码实现:
```
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0)
return;
if (low >= high)
return;
// pick the pivot
int middle = low + (high - low) / 2;
int pivot = arr[middle];
// make left < pivot and right > pivot
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--;
}
}
// recursively sort two sub parts
if (low < j)
quickSort(arr, low, j);
if (high > i)
quickSort(arr, i, high);
}
}
```
希望这对您有所帮助。
### 回答2:
快速排序(Quick Sort)是一种常用的排序算法,使用分治法(Divide and Conquer)的思想来进行排序。下面是用Java实现的快速排序代码:
```java
public class QuickSort {
public static void main(String[] args) {
int[] array = {7, 9, 3, 5, 1, 6, 4, 2, 8};
quickSort(array, 0, array.length - 1);
System.out.println("排序结果:");
for (int num : array) {
System.out.print(num + " ");
}
}
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int pivot = partition(array, low, high); // 获取分区点
quickSort(array, low, pivot - 1); // 对分区点左侧进行递归排序
quickSort(array, pivot + 1, high); // 对分区点右侧进行递归排序
}
}
public static int partition(int[] array, int low, int high) {
int pivot = array[high]; // 选取分区点为最后一个元素
int i = low - 1; // i 表示小于分区点的元素的最右位置
for (int j = low; j < high; j++) {
if (array[j] < pivot) {
i++;
swap(array, i, j);
}
}
swap(array, i + 1, high); // 将分区点放到正确的位置
return i + 1;
}
public static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
```
以上代码实现了快速排序的核心逻辑。快速排序的思想是选择一个分区点,将比分区点小的元素放在其左侧,比分区点大的元素放在其右侧,然后对左右两侧分别进行递归排序,直到区间缩小到只有一个元素。最后,所有分区的元素正好按从小到大排序。这个代码里选择的分区点是数组的最后一个元素。
### 回答3:
以下是使用Java编写的快速排序算法代码:
```java
public class QuickSort {
public static void main(String[] args) {
int[] array = {5, 2, 6, 1, 3, 9, 4, 8, 7};
quickSort(array, 0, array.length - 1);
System.out.println("排序结果:");
for (int num : array) {
System.out.print(num + " ");
}
}
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int pivot = partition(array, low, high); // 划分数组
quickSort(array, low, pivot - 1); // 对左子数组进行快速排序
quickSort(array, pivot + 1, high); // 对右子数组进行快速排序
}
}
public static int partition(int[] array, int low, int high) {
int pivot = array[low]; // 选择第一个元素作为基准值
while (low < high) {
while (low < high && array[high] >= pivot) {
high--;
}
array[low] = array[high]; // 将比基准值小的元素移到左边
while (low < high && array[low] <= pivot) {
low++;
}
array[high] = array[low]; // 将比基准值大的元素移到右边
}
array[low] = pivot; // 将基准值放到最终位置
return low;
}
}
```
以上代码实现了快速排序算法。快速排序是一种常用的排序算法,它通过分治法的思想,在待排序序列中选择一个元素作为基准值,将序列划分为两个子序列,一个子序列中的元素都比基准值小,另一个子序列中的元素都比基准值大,然后递归地对两个子序列进行快速排序,最终得到排序后的序列。请注意,以上代码仅作为示例,实际的快速排序算法可能根据实际需求进行更多的优化。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)