0% found this document useful (0 votes)
27 views

Merge Sort

The document describes an implementation of merge sort in Java. It reads in an array of numbers, sorts the array using a recursive merge sort algorithm, and outputs the sorted array along with the number of comparisons made during the sort.

Uploaded by

Lê Tiến Phát
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Merge Sort

The document describes an implementation of merge sort in Java. It reads in an array of numbers, sorts the array using a recursive merge sort algorithm, and outputs the sorted array along with the number of comparisons made during the sort.

Uploaded by

Lê Tiến Phát
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

public class MergeSortExample {

private static int comparisonCount = 0;

public static void main(String[] args) {


// Step 1: Read the size and elements of the array
int[] numbers = readNumbers();

// Step 2: Print the unsorted array


System.out.print("Unsorted array: ");
printNumbers(numbers);
System.out.println();

// Step 3: Perform merge sort and count comparisons


mergeSort(numbers, 0, numbers.length - 1);

// Step 4: Print the sorted array


System.out.print("Sorted array: ");
printNumbers(numbers);
System.out.println();

// Step 5: Print the number of comparisons


System.out.println("Number of comparisons: " + comparisonCount);
}

private static int[] readNumbers() {


Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
int[] numbers = new int[size];
for (int i = 0; i < size; i++) {
numbers[i] = scanner.nextInt();
}
return numbers;
}

private static void printNumbers(int[] numbers) {


for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + " ");
}
}

private static void mergeSort(int[] numbers, int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;

// Recursive calls for left and right sub-arrays


mergeSort(numbers, left, mid);
mergeSort(numbers, mid + 1, right);

// Merge the sorted sub-arrays


merge(numbers, left, mid, right);
}
}

private static void merge(int[] numbers, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

// Create temporary arrays


int[] leftArray = new int[n1];
int[] rightArray = new int[n2];

// Copy data to temporary arrays


System.arraycopy(numbers, left, leftArray, 0, n1);
System.arraycopy(numbers, mid + 1, rightArray, 0, n2);

// Merge the temporary arrays

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {


comparisonCount++;
if (leftArray[i] <= rightArray[j]) {
numbers[k] = leftArray[i];
i++;
} else {
numbers[k] = rightArray[j];
j++;
}
k++;
}

while (i < n1) {


numbers[k] = leftArray[i];
i++;
k++;
}

while (j < n2) {


numbers[k] = rightArray[j];
j++;
k++;
}
}
}

You might also like