请用java写实现快速排序的代码
时间: 2024-04-29 17:24:48 浏览: 7
当我听到你的问题时,我会直接给出代码实现,如下所示:
```java
public class QuickSort {
public void sort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
int pivot = nums[(left + right) / 2];
while (i <= j) {
while (nums[i] < pivot) {
i++;
}
while (nums[j] > pivot) {
j--;
}
if (i <= j) {
int tmp = nums[i];
nums[i++] = nums[j];
nums[j--] = tmp;
}
}
sort(nums, left, j);
sort(nums, i, right);
}
}
```
希望能够解决你的问题!
相关问题
用java代码写快速排序
我可以为您提供一个用Java代码实现快速排序的示例:
public class QuickSort {
public static void main(String[] args) {
int[] array = {3,5,8,4,9,1,6,2,7};
quickSort(array, 0, array.length-1);
for (int i : array) {
System.out.print(i);
System.out.print(" ");
}
}
public static void quickSort(int[] array, int low, int high) {
if (low < high) {
int part = partation(array, low, high);
quickSort(array, low, part-1);
quickSort(array, part+1, high);
}
}
public static int partation(int[] array, int low, int high) {
int temp = array[low];
while (low < high) {
while (low < high && array[high] >= temp) {
high--;
}
array[low] = array[high];
while (low < high && array[low] <= temp) {
low++;
}
array[high] = array[low];
}
array[low] = temp;
return low;
}
}
用JAVA写一个快速排序代码
### 回答1:
当然可以,以下是一个简单的JAVA快速排序代码:
public class QuickSort {
public static void main(String[] args) {
int[] arr = {5, 2, 9, 3, 7, 6, 1, 8, 4};
quickSort(arr, 0, arr.length - 1);
for (int i : arr) {
System.out.print(i + " ");
}
}
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int pivot = partition(arr, left, right);
quickSort(arr, left, pivot - 1);
quickSort(arr, pivot + 1, right);
}
}
public 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;
}
}
### 回答2:
快速排序是一种高效的排序算法,使用递归的方式实现。下面是使用Java语言编写的快速排序代码:
```java
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high); // 将数组划分为两部分,并返回划分的中心索引
quickSort(arr, low, pi - 1); // 对划分的左边部分进行排序
quickSort(arr, pi + 1, high); // 对划分的右边部分进行排序
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high]; // 以最右边的元素作为基准值
int i = low - 1; // i用于记录小于或等于基准值的元素的索引位置
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 = {7, 2, 4, 1, 5, 3};
int n = arr.length;
quickSort(arr, 0, n - 1);
System.out.println("排序后的数组为:");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
```
以上代码实现了快速排序算法。在`quickSort`方法中,首先选取最右边的元素作为基准值,然后对数组进行划分,将小于或等于基准值的元素移到左边,大于基准值的元素移到右边。然后通过递归分别对左右两部分进行排序,直到最后数组有序。
运行以上代码,输出为`排序后的数组为:1 2 3 4 5 7`,即将输入的数组按照升序进行排序。
### 回答3:
快速排序(Quick Sort)是一种高效的排序算法,其基本思想是通过一趟排序将待排序的数据分割成独立的两部分,其中一部分的所有数据都比另一部分小,然后再按照此方法对这两部分数据进行排序,依次递归完成排序过程。
下面是使用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);
}
}
private static int partition(int[] arr, int low, int high) {
// 以最右边的元素作为基准
int pivot = arr[high];
// partitionIndex表示小于基准的元素的索引
int partitionIndex = low;
for (int i = low; i < high; i++) {
// 如果当前元素小于等于基准,则将其与partitionIndex位置的元素交换
if (arr[i] <= pivot) {
swap(arr, i, partitionIndex);
partitionIndex++;
}
}
// 将基准元素放置到合适的位置
swap(arr, partitionIndex, high);
return partitionIndex;
}
private 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, 7, 5, 11, 12, 2, 14, 3, 10, 6};
quickSort(arr, 0, arr.length - 1);
System.out.println("排序结果:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
```
上述代码定义了一个`QuickSort`类,其中的`quickSort`方法用于实现快速排序,`partition`方法用于对数组进行分区,`swap`方法用于交换数组中的两个元素。在`main`方法中,我们可以通过创建一个示例数组,调用`quickSort`方法对其进行快速排序,并打印排序结果。
运行上述代码,将会得到如下的排序结果:
```
排序结果:
2 3 5 6 7 9 10 11 12 14
```
以上就是使用JAVA编写的快速排序算法的代码实现。