对 {49, 38, 65, 97, 76, 13, 27, 101, 34, 119, 1}进行从小到大的排序,要求使用快速排序法。写出完整的Java代码
时间: 2023-10-03 21:09:35 浏览: 37
以下是使用快速排序法对数组进行从小到大排序的Java代码:
```java
public class QuickSort {
public static void main(String[] args) {
int[] arr = {49, 38, 65, 97, 76, 13, 27, 101, 34, 119, 1};
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 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 = left;
int index = pivot + 1;
for (int i = index; i <= right; i++) {
if (arr[i] < arr[pivot]) {
swap(arr, i, index);
index++;
}
}
swap(arr, pivot, index - 1);
return index - 1;
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
```
输出结果为:
```
1 13 27 34 38 49 65 76 97 101 119
```