Provide a simple code sample of Merge sort in Java. Please and thank you

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Provide a simple code sample of Merge sort in Java.

Please and thank you

Expert Solution
Step 1: Algorithm :

Merge Sort Algorithm

MergeSort(arr[], left, right):
    If left < right:
        1. Find the middle point to divide the array into two halves:
            middle = (left + right) / 2
        2. Call MergeSort for the left half:
            MergeSort(arr, left, middle)
        3. Call MergeSort for the right half:
            MergeSort(arr, middle + 1, right)
        4. Merge the two halves:
            Merge(arr, left, middle, right)

Merge(arr[], left, middle, right):
    1. Find the sizes of the two subarrays to be merged:
        n1 = middle - left + 1
        n2 = right - middle
    2. Create temporary arrays leftArr[0...n1] and rightArr[0...n2]
    3. Copy data to the temporary arrays leftArr[] and rightArr[]:
        for i = 0 to n1 - 1:
            leftArr[i] = arr[left + i]
        for j = 0 to n2 - 1:
            rightArr[j] = arr[middle + 1 + j]
    4. Merge the temporary arrays back into the original array arr[left...right]:
        i = 0
        j = 0
        k = left
        while i < n1 and j < n2:
            if leftArr[i] <= rightArr[j]:
                arr[k] = leftArr[i]
                i++
            else:
                arr[k] = rightArr[j]
                j++
            k++
        5. Copy the remaining elements of leftArr[], if any:
        while i < n1:
            arr[k] = leftArr[i]
            i++
            k++
        6. Copy the remaining elements of rightArr[], if any:
        while j < n2:
            arr[k] = rightArr[j]
            j++
            k++


steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Radix Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education