java快速排序怎么写
时间: 2023-03-13 21:22:16 浏览: 42
这里有一个Java实现快速排序的实例: public class QuickSort {
public static void main(String[] args) {
int[] input = {24,2,45,20,56,75,2,56,99,53,12};
quickSort(input,0,input.length-1);
for(int i:input){
System.out.print(i);
System.out.print(" ");
}
}
public static void quickSort(int[] array,int start,int end){
if(start<end){
int baseNum = array[start];
int midNum;
int i = start;
int j = end;
do{
while((array[i] < baseNum) && i<end){
i++;
}
while((array[j] > baseNum) && j>start){
j--;
}
if(i <= j){
midNum = array[i];
array[i] = array[j];
array[j] = midNum;
i++;
j--;
}
}while(i<=j);
if(start<j){
quickSort(array,start,j);
}
if(end>i){
quickSort(array,i,end);
}
}
}
}
相关问题
JAVA快速排序怎么写
### 回答1:
Java快速排序的一个实现如下:
public class QuickSort {
public static void sort(int[] arr, int left, int right) {
int index = partition(arr, left, right);
if (left < index - 1) {
sort(arr, left, index - 1);
}
if (index < right) {
sort(arr, index, right);
}
}
public static int partition(int[] arr, int left, int right) {
int pivot = arr[(left + right) / 2];
while (left <= right) {
while (arr[left] < pivot) left++;
while (arr[right] > pivot) right--;
if (left <= right) {
swap(arr, left, right);
left++;
right--;
}
}
return left;
}
public static void swap(int[] arr, int left, int right) {
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
}
### 回答2:
快速排序是一种常见且高效的排序算法,可以通过递归的方式实现。以下是使用Java编写的快速排序算法的示例:
```
public class QuickSort {
// 快速排序算法
public static void quickSort(int[] arr, int left, int right) {
if (left < right) {
int pivotIndex = partition(arr, left, right); // 将数组分区,并获取中轴元素的索引
// 对中轴元素左边的子数组进行快速排序
quickSort(arr, left, pivotIndex - 1);
// 对中轴元素右边的子数组进行快速排序
quickSort(arr, pivotIndex + 1, right);
}
}
// 将数组分区并返回中轴元素的索引
public static int partition(int[] arr, int left, int right) {
int pivot = arr[right]; // 设置最右边的元素为中轴元素
int i = left - 1; // i表示小于中轴元素的元素的最右边界
for (int j = left; j < right; j++) {
if (arr[j] < pivot) {
i++;
swap(arr, i, j); // 交换元素,将小于中轴元素的元素放到左边
}
}
swap(arr, i + 1, right); // 将中轴元素放到正确的位置
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 = {5, 2, 7, 3, 9, 1, 6};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
// 输出: 1 2 3 5 6 7 9
}
}
```
以上是一个基本的快速排序的实现。快速排序的思想是选择一个中轴元素,将数组分成两个子数组,使得左边子数组的元素都小于中轴元素,右边子数组的元素都大于中轴元素。然后对左右子数组进行递归调用快速排序,直到排序完成。最后,将左右子数组和中轴元素合并。通过递归调用快速排序可以实现整个数组的排序。
希望对你有帮助!
### 回答3:
快速排序(Quick Sort)是一种经典的排序算法,其基本思想是通过分割数组为两个子数组,并递归地对子数组进行排序,最终将整个数组排序完成。下面是使用JAVA语言实现快速排序的步骤:
1.选择基准元素:从数组中选择一个基准元素,通常选择数组的第一个元素作为基准值。
2.分割数组:将比基准元素小的元素放在基准元素的左边,将比基准元素大的元素放在基准元素的右边,这样基准元素处于正确的位置。
3.递归排序:递归地对基准元素左边的子数组和右边的子数组进行快速排序。即,重复步骤1和2,直到子数组的长度为1或0。
下面是JAVA语言实现快速排序的代码示例:
```java
public class QuickSort {
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]; // 选择第一个元素作为基准元素
int i = low + 1; // 从基准元素下一个元素开始遍历
int j = high; // 从高位开始遍历
while (i <= j){
if (array[i] < pivot && array[j] > pivot){
// 交换array[i]和array[j]的位置
swap(array, i, j);
i++; // i向后移动
j--; // j向前移动
} else if (array[i] >= pivot){
i++; // i向后移动
} else if (array[j] <= pivot){
j--; // j向前移动
}
}
// 将基准元素和j位置元素交换
swap(array, low, j);
return j; // 返回基准元素的位置
}
public static void swap(int[] array, int i, int j){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args) {
int[] array = {4, 2, 9, 1, 6, 5, 3, 8, 7};
quickSort(array, 0, array.length - 1);
for (int num : array){
System.out.print(num + " ");
}
}
}
```
以上代码实现了快速排序的逻辑,通过选择基准元素和分割数组的操作,将数组按照基准元素的大小分成左右两个子数组,并递归地对子数组进行排序,最终实现了快速排序的功能。运行以上代码,可以看到输出结果为1 2 3 4 5 6 7 8 9,即表示数组已经完成排序。
Java 快速排序怎么写
Java 快速排序的代码如下:
```
import java.util.Arrays;
public class QuickSort {
public static void sort(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)
sort(arr, low, j);
if (high > i)
sort(arr, i, high);
}
public static void main(String[] args) {
int[] arr = {24, 2, 45, 20, 56, 75, 2, 56, 99, 53, 12};
System.out.println("Before: " + Arrays.toString(arr));
sort(arr, 0, arr.length - 1);
System.out.println("After: " + Arrays.toString(arr));
}
}
```
快速排序是一种分治算法,通过递归地交换数组元素来实现排序。它的基本思想是:选取一个基准元素,然后将数组分成两个子数组,使得左边的子数组中的元素都小于基准元素,右边的子数组中的元素都大于基准元素,然后分别对这两个子数组进行快速排序。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)