Big Java Late Objects
Big Java Late Objects
2nd Edition
ISBN: 9781119330455
Author: Horstmann
Publisher: WILEY
Question
Book Icon
Chapter 18, Problem 7PE
Program Plan Intro

Merge Sort

Program plan:

Filename: “MergeSorter.java”

This code snippet creates a class “MergeSorter”. In the code,

  • Define a class “MergeSorter”.
    • Declare an array “a”.
    • Declare an array list “l”.
    • Define the constructor “MergeSorter ()”.
      • Set the array “a” equal to “anArray”.
      • Define “l”.
      • Iterate a “for” loop.
        • Set elements of “a” to “l”.
    • Define the constructor “MergeSorter ()”.
      • Set the array “l” equal to “aList”.
    • Define the method “sort()”.
      • If the size of “l” is less than or equal to 1,
        • Return.
      • Create two array lists “first” and “second”.
      • Iterate a “for” loop,
        • Add elements of “l” to “first”.
      • Iterate a “for” loop,
        • Add elements of “l” to “second”.
      • Create generic array “firstSorter”.
      • Create generic array “secondSorter”.
      • Call the method “sort()” on “firstSorter”.
      • Call the method “sort()” on “secondSorter”.
      • Call the method “merge()”.
      • If “a” is not empty,
        • Iterate a “for” loop,
          • Set elements of “l” to “a”.
    • Define the method “merge()”.
      • Declare integer “iFirst”.
      • Declare the variable “iSecond”.
      • Declare a variable “j”.
      • Iterate a “while” loop for merging.
        • if “iFirst” is less than “iSecond”.
          • Set “iFirst” to “l”.
          • Increment “iFirst” by 1.
            • Else,
              • Set “iSecond” to “l”.
              • Increment “iSecond” by 1.
            • Increment “j” by 1.
      • Iterate a “for” loop.
        • Set the first half values of “l”.
      • Iterate a “for” loop.
        • Set the second half values of “l”.

Filename: “MergeSortTester.java”

This code snippet creates a class “MergeSortTester”. In the code,

  • Define a class “MergeSortTester”.
    • Define the “main” method.
    • Define the string array “words[]”.
    • Define the object of “MergeSorter”.
    • Call “sort()”.
    • Print the values in the array “words[]”.
    • Print the expected value.

Blurred answer
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education