Run ❯
Get your
own
website
×
Change Orientation
Change Theme, Dark/Light
Go to 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
#include
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 + step >= length) rightSize = 0; else if (i + 2 * step > length) rightSize = length - (i + step); double *left = (double *)malloc(leftSize * sizeof(double)); double *right = (double *)malloc(rightSize * sizeof(double)); double *merged = (double *)malloc((leftSize + rightSize) * sizeof(double)); 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]; } merge(left, leftSize, right, rightSize, merged); for (int j = 0; j < leftSize + rightSize; ++j) { arr[i + j] = merged[j]; } free(left); free(right); free(merged); } 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) { int mid = Math.min(i + step, length); int end = Math.min(i + 2 * step, length); double[] left = Arrays.copyOfRange(arr, i, mid); double[] right = Arrays.copyOfRange(arr, mid, end); 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 result:
C result:
Java result:
Sorted array: [-13, -10, 3, 6, 7, 15, 23.5, 55]
Sorted array: -13.0 -10.0 3.0 6.0 7.0 15.0 23.5 55.0
Sorted array: [-13.0, -10.0, 3.0, 6.0, 7.0, 15.0, 23.5, 55.0]