采用分治法编程实现合并排序算法,用Java将下列数据从下到大排序:45,32,67,54,34,32,21,25,67,98
时间: 2023-08-31 20:41:37 浏览: 53
### 回答1:
以下是Java代码实现合并排序算法,对给定的数据进行排序:
```
public class MergeSort {
public static void main(String[] args) {
int[] arr = {45, 32, 67, 54, 34, 32, 21, 25, 67, 98};
mergeSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
public static void merge(int[] arr, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left, j = mid + 1, k = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (int p = 0; p < temp.length; p++) {
arr[left + p] = temp[p];
}
}
}
```
输出结果为:[21, 25, 32, 32, 34, 45, 54, 67, 67, 98]
### 回答2:
分治法是一种递归算法,它将问题分解为较小的子问题,然后将子问题的解合并为原问题的解。合并排序算法是通过分治法实现的一种经典排序算法。
要使用分治法编程实现合并排序,首先需要定义一个递归函数,将原始数组分解为两个更小的子数组,然后分别对这两个子数组进行排序,最后将两个有序子数组合并为一个有序数组。
下面是用Java语言实现合并排序算法来对给定的数据从小到大排序的代码:
```java
import java.util.Arrays;
public class MergeSort {
public static void main(String[] args) {
int[] arr = {45, 32, 67, 54, 34, 32, 21, 25, 67, 98};
mergeSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void mergeSort(int[] arr) {
if (arr == null || arr.length <= 1) {
return;
}
mergeSortHelper(arr, 0, arr.length - 1);
}
private static void mergeSortHelper(int[] arr, int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
mergeSortHelper(arr, left, mid);
mergeSortHelper(arr, mid + 1, right);
merge(arr, left, mid, right);
}
private static void merge(int[] arr, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (int p = 0; p < temp.length; p++) {
arr[left + p] = temp[p];
}
}
}
```
运行上述代码,输出结果为:[21, 25, 32, 32, 34, 45, 54, 67, 67, 98],表示已经将给定数据从小到大排序完成。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)