Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

create a merge sort code which takes in number of elements to sort and randomnly…

  1. User

    create a merge sort code which takes in number of elements to sort and randomnly generates the number from 1 to 9 ,it should also calculate the total time taken to complete the merge sort write in java

  2. Assistant

    Sure! Here's a Merge Sort code in Java that takes in the number of elements to sort, randomly generates the numbers from 1 to 9, and calculates the total time taken to complete the merge sort:

    import java.util.Random;
    
    public class MergeSortExample {
        public static void main(String[] args) {
            // Generate random numbers
            int numElements = 10; // Number of elements to sort
            int[] arr = new int[numElements];
            Random rand = new Random();
            for (int i = 0; i < numElements; i++) {
                arr[i] = rand.nextInt(9) + 1; // Random number from 1 to 9
            }
            
            System.out.println("Before Sorting:");
            printArray(arr);
            
            long startTime = System.nanoTime();
            
            mergeSort(arr, 0, arr.length - 1);
            
            long endTime = System.nanoTime();
            long totalTime = endTime - startTime;
            
            System.out.println("\nAfter Sorting:");
            printArray(arr);
            
            System.out.println("\nTotal time taken: " + totalTime + " nanoseconds");
        }
        
        public static void mergeSort(int[] arr, int l, int r) {
            if (l < r) {
                int m = (l + r) / 2;
                
                mergeSort(arr, l, m);
                mergeSort(arr, m + 1, r);
                
                merge(arr, l, m, r);
            }
        }
        
        public static void merge(int[] arr, int l, int m, int r) {
            int n1 = m - l + 1;
            int n2 = r - m;
            
            int[] L = new int[n1];
            int[] R = new int[n2];
            
            for (int i = 0; i < n1; i++) {
                L[i] = arr[l + i];
            }
            for (int j = 0; j < n2; j++) {
                R[j] = arr[m + 1 + j];
            }
            
            int i = 0;
            int j = 0;
            
            int k = l;
            
            while (i < n1 && j < n2) {
                if (L[i] <= R[j]) {
                    arr[k] = L[i];
                    i++;
                } else {
                    arr[k] = R[j];
                    j++;
                }
                k++;
            }
            
            while (i < n1) {
                arr[k] = L[i];
                i++;
                k++;
            }
            
            while (j < n2) {
                arr[k] = R[j];
                j++;
                k++;
            }
        }
        
        public static void printArray(int[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
    }
    

    In this code, the mergeSort method implements the merge sort algorithm, the merge method merges two sorted subarrays, and the printArray method is used to print the elements of an array.

    Hope this helps! Let me know if you have any further questions.