运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
def merge(left, right): result = [] i = j = 0 while i < len(left) and j < len(right): if left[i] < right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result.extend(left[i:]) result.extend(right[j:]) return result def mergeSort(arr): step = 1 # Starting with sub-arrays of length 1 length = len(arr) while step < length: for i in range(0, length, 2 * step): left = arr[i:i + step] right = arr[i + step:i + 2 * step] merged = merge(left, right) # Place the merged array back into the original array for j, val in enumerate(merged): arr[i + j] = val step *= 2 # Double the sub-array length for the next iteration return arr unsortedArr = [3, 7, 6, -10, 15, 23.5, 55, -13] sortedArr = mergeSort(unsortedArr) print("Sorted array:", sortedArr) #Python
#include <stdio.h> #include <stdlib.h> void merge(double left[], int leftSize, double right[], int rightSize, double result[]) { int i = 0, j = 0, k = 0; while (i < leftSize && j < rightSize) { if (left[i] < right[j]) { result[k++] = left[i++]; } else { result[k++] = right[j++]; } } while (i < leftSize) { result[k++] = left[i++]; } while (j < rightSize) { result[k++] = right[j++]; } } void mergeSort(double arr[], int length) { int step = 1; while (step < length) { for (int i = 0; i < length; i += 2 * step) { int leftSize = step; int rightSize = step; if (i + step >= length) leftSize = length - i; if (i + 2 * step >= length) rightSize = length - i - step; double left[leftSize]; double right[rightSize]; for (int j = 0; j < leftSize; ++j) { left[j] = arr[i + j]; } for (int j = 0; j < rightSize; ++j) { right[j] = arr[i + step + j]; } double merged[leftSize + rightSize]; merge(left, leftSize, right, rightSize, merged); for (int j = 0; j < leftSize + rightSize; ++j) { arr[i + j] = merged[j]; } } step *= 2; } } int main() { double arr[] = {3, 7, 6, -10, 15, 23.5, 55, -13}; int length = sizeof(arr) / sizeof(arr[0]); mergeSort(arr, length); printf("Sorted array: "); for (int i = 0; i < length; ++i) { printf("%.1f ", arr[i]); } printf("\n"); return 0; } //C
import java.util.Arrays; public class Main { public static double[] merge(double[] left, double[] right) { double[] result = new double[left.length + right.length]; int i = 0, j = 0, k = 0; while (i < left.length && j < right.length) { if (left[i] < right[j]) { result[k++] = left[i++]; } else { result[k++] = right[j++]; } } while (i < left.length) { result[k++] = left[i++]; } while (j < right.length) { result[k++] = right[j++]; } return result; } public static double[] mergeSort(double[] arr) { int step = 1; int length = arr.length; while (step < length) { for (int i = 0; i < length; i += 2 * step) { double[] left = Arrays.copyOfRange(arr, i, Math.min(i + step, length)); double[] right = Arrays.copyOfRange(arr, i + step, Math.min(i + 2 * step, length)); double[] merged = merge(left, right); System.arraycopy(merged, 0, arr, i, merged.length); } step *= 2; } return arr; } public static void main(String[] args) { double[] unsortedArr = {3, 7, 6, -10, 15, 23.5, 55, -13}; double[] sortedArr = mergeSort(unsortedArr); System.out.println("Sorted array: " + Arrays.toString(sortedArr)); } } //Java
Python 结果
C 结果
Java 结果
已排序数组: [-13, -10, 3, 6, 7, 15, 23.5, 55]
已排序数组: -13.0 -10.0 3.0 6.0 7.0 15.0 23.5 55.0
已排序数组: [-13.0, -10.0, 3.0, 6.0, 7.0, 15.0, 23.5, 55.0]