帮我写一段Java的快速排序
时间: 2023-10-18 13:19:19 浏览: 21
好的,下面是一段Java的快速排序代码:
```java
public static void quickSort(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int pivot = arr[left];
int i = left;
int j = right;
while (i < j) {
while (i < j && arr[j] >= pivot) {
j--;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < pivot) {
i++;
}
if (i < j) {
arr[j] = arr[i];
j--;
}
}
arr[i] = pivot;
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
}
```
希望能够帮到你!
相关问题
帮我写一段Java快速排序代码
### 回答1:
public class QuickSort {
public static void quickSort(int[] arr, int start, int end) {
int partition = partition(arr, start, end);
if(partition-1>start) {
quickSort(arr, start, partition - 1);
}
if(partition+1<end) {
quickSort(arr, partition + 1, end);
}
}
public static int partition(int[] arr, int start, int end) {
int pivot = arr[end];
for(int i=start; i<end; i++) {
if(arr[i]<pivot) {
int temp= arr[start];
arr[start]=arr[i];
arr[i]=temp;
start++;
}
}
int temp = arr[start];
arr[start] = pivot;
arr[end] = temp;
return start;
}
public static void main(String[] args) {
int[] arr = {3,2,1,4};
quickSort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
### 回答2:
下面是一段Java语言的快速排序代码:
```java
public class QuickSort {
public static void quickSort(int[] arr, int start, int end) {
if (start < end) {
int pivot = partition(arr, start, end); // 获取划分点
quickSort(arr, start, pivot - 1); // 对划分点左侧进行快速排序
quickSort(arr, pivot + 1, end); // 对划分点右侧进行快速排序
}
}
private static int partition(int[] arr, int start, int end) {
int pivot = arr[start]; // 选择第一个元素作为划分点
int left = start + 1; // 左指针
int right = end; // 右指针
while (left <= right) {
// 从左指针开始找到第一个大于划分点的元素
while (left <= right && arr[left] <= pivot) {
left++;
}
// 从右指针开始找到第一个小于划分点的元素
while (left <= right && arr[right] > pivot) {
right--;
}
// 如果左指针仍然小于等于右指针,则交换左右指针所指的元素
if (left <= right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
// 将划分点的元素放到正确的位置
int temp = arr[start];
arr[start] = arr[right];
arr[right] = temp;
return right; // 返回划分点的位置
}
public static void main(String[] args) {
int[] arr = {9, 5, 2, 7, 4, 6, 1, 3, 8};
quickSort(arr, 0, arr.length - 1);
System.out.print("排序结果:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
```
在快速排序算法中,首先选择一个基准元素,然后通过一趟排序将待排序的元素划分为两个部分,其中左边部分的所有元素都比基准元素小,右边部分的所有元素都比基准元素大。然后对这两个部分递归地进行同样的操作,直到整个数组有序。
以上代码实现了快速排序算法,并在`main`方法中演示了如何使用该算法对一个数组进行排序。
### 回答3:
当然可以帮你写一段Java快速排序代码。
快速排序(Quick Sort)是一种常用的排序算法,其基本思想是选择一个基准元素,将数组分割成两个子数组,使得左子数组的所有元素都小于基准元素,右子数组的所有元素都大于基准元素,然后递归地对左右子数组进行快速排序。
以下是一段使用Java实现的快速排序代码示例:
```java
public class QuickSort {
public static void main(String[] args) {
// 测试用例
int[] array = {29, 10, 14, 37, 13};
// 调用快速排序算法
quickSort(array, 0, array.length - 1);
// 输出排序后的结果
for (int num : array) {
System.out.print(num + " ");
}
}
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int pivotIndex = partition(array, low, high);
quickSort(array, low, pivotIndex - 1);
quickSort(array, pivotIndex + 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;
}
}
```
在这段代码中,`quickSort`方法是快速排序的主要逻辑,通过递归不断对子数组进行排序。`partition`方法用于选择基准元素并将数组分割。
希望这段代码能够帮助到你!如有任何疑问,请随时追问。
请帮我写一段JAVA的快速排序
好的,以下是快速排序的JAVA代码示例:
```
public class QuickSort {
public static void main(String[] args) {
int[] arr = {10, 80, 30, 90, 40, 50, 70};
int size = arr.length;
quickSort(arr, 0, size - 1);
System.out.println("排序后的数组:");
for (int i = 0; i < size; ++i) {
System.out.print(arr[i] + " ");
}
}
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int partitionIndex = partition(arr, left, right);
quickSort(arr, left, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, right);
}
}
public static int partition(int[] arr, int left, int right) {
int pivot = arr[right];
int i = (left - 1);
for (int j = left; j < right; 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[right];
arr[right] = temp;
return i + 1;
}
}
```
以上就是快速排序的JAVA代码示例。