0% found this document useful (0 votes)
30 views3 pages

MyNova DSA - Module 3 Programming Assignment

This Java code implements a merge sort algorithm to sort an array of random integers. It uses a recursive merge sort method to divide the array in half and then merge the sorted halves back together. The main method generates a random array, times the sorting process, prints the sorted results, and outputs performance statistics.

Uploaded by

carl biwott
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)
30 views3 pages

MyNova DSA - Module 3 Programming Assignment

This Java code implements a merge sort algorithm to sort an array of random integers. It uses a recursive merge sort method to divide the array in half and then merge the sorted halves back together. The main method generates a random array, times the sorting process, prints the sorted results, and outputs performance statistics.

Uploaded by

carl biwott
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

Guided Assignment

Problem 1: Mystery sort

Code:
import java.util.Random;

public class MergeSort {


public static void mergeSort(int[] arr){
mergeSortRec(arr, 0, arr.length-1);
}

private static void mergeSortRec(int[] arr, int first, int last){


if(first < last){
int mid = (first + last) / 2;
mergeSortRec(arr, first, mid);
mergeSortRec(arr, mid+1, last);
merge(arr, first, mid, mid+1, last);
}
}

private static void merge(int[] arr, int leftFirst, int leftLast, int
rightFirst, int rightLast){
int[] aux = new int[arr.length]; // extra space, this is downside of this
algorithm
int index = leftFirst;
int saveFirst = leftFirst;
while(leftFirst <= leftLast && rightFirst <= rightLast){
if(arr[leftFirst] <= arr[rightFirst]){
aux[index++] = arr[leftFirst++];
}else{
aux[index++] = arr[rightFirst++];
}
}
while(leftFirst <= leftLast){
aux[index++] = arr[leftFirst++];
}
while(rightFirst <= rightLast){
aux[index++] = arr[rightFirst++];
}
for(index = saveFirst; index <= rightLast; index++){
arr[index] = aux[index];
}
}

public static void main(String[] args) {


if (args.length != 3) {
System.out.println("Usage: java MergeSort <N> <A> <B>");
return;
}

int N = Integer.parseInt(args[0]);
int A = Integer.parseInt(args[1]);
int B = Integer.parseInt(args[2]);

if (N <= 0 || A >= B) {
System.out.println("Invalid input. N must be positive and A must be
less than B.");
return;
}

int[] arr = new int[N];


Random random = new Random();
for (int i = 0; i < N; i++) {
// random numbers between A and B
arr[i] = random.nextInt(B - A + 1) + A;
}

long startTime = System.nanoTime();

mergeSort(arr);

long endTime = System.nanoTime();

long sortingTime = endTime - startTime;

System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();

System.out.println("Number of elements: " + N);


System.out.println("Sorting time: " + sortingTime + " nanoseconds");
}
}
- What kind of sort is this mystery sort?
This sort is a variation of the Bubble Sort algorithm.

- What is the average Big O for this sort?


The average Big O complexity for this sort is O(n^2), where n is the number of elements in the array.

You might also like