Why won't my java code run properly? " P1 Implement a binary search on an array iteratively using the method public static > boolean inArraylterative Sorted(T[] anArray, T anEntry) P2 Consider a revised selection sort algorithm so that on each pass it finds both the largest and smallest values in the unsorted portion of the array. The sort then moves each of these values into its correct location by swapping array entries. Implement the modified selection sort using the method 1. public static > void modifiedSelectionSort(T[] a, int n) 2. How many comparisons are necessary to sort n values?"   " import java.util.*; public class Main {     public static void main(String[] args) {                  Integer[] xb = {1, 3, 5, 7, 9, 11, 13, 15, 17};         int y = 9;         System.out.println( y + " present:   " + inArrayIterativeSorted(xb, y));                   Integer[] xi = {2, 4, 6, 8, 10, 12, 14, 16, 18};         System.out.println("Before sorting: " + Arrays.toString(xi));         modifiedSelectionSort(xi, xi.length);         System.out.println("After sorting: " + Arrays.toString(xi));           int n = xi.length;         int t = n * (n - 1) / 2;           System.out.println("Comparisons required to sort " + n + " values: " + t);     }     // Problem 1     public static >     boolean inArrayIterativeSorted(T[] anArray, T anEntry) {                  int x = 0;         int u = anArray.length - 1;         while (x <= u) {             int p = (x + u) / 2;             int cmp = anArray[p].compareTo(anEntry);             if (cmp == 0) {                 return true;             } else if (cmp < 0) {                 x = p + 1;             } else {                 u = p - 1;             }         }         return false;     }     // Problem 2     public static >     void modifiedSelectionSort(T[] a, int n) {         for (int i = 0; i < n / 2; i++) {             int y = i;             int z = i;             for (int j = i + 1; j < n - i; j++) {                 if (a[j].compareTo(a[y]) < 0) {                     y = j;                 } else if (a[j].compareTo(a[z]) > 0) {                     z = j;                 }             }             T temp = a[i];             a[i] = a[y];             a[y] = temp;             if (z == i) {                 z = y;               }             temp = a[n - i - 1];             a[n - i - 1] = a[z];             a[z] = temp;         }     } } "

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

Why won't my java code run properly?

"
P1
Implement a binary search on an array iteratively using the method
public static <T extends Comparable<? super T>> boolean inArraylterative Sorted(T[]
anArray, T anEntry)
P2
Consider a revised selection sort algorithm so that on each pass it finds both the largest and
smallest values in the unsorted portion of the array. The sort then moves each of these values
into its correct location by swapping array entries.
Implement the modified selection sort using the method
1.
public static <T extends Comparable<? super T>> void modifiedSelectionSort(T[] a, int n)
2. How many comparisons are necessary to sort n values?"

 

"

import java.util.*;

public class Main {
    public static void main(String[] args) {
        
        Integer[] xb = {1, 3, 5, 7, 9, 11, 13, 15, 17};
        int y = 9;
        System.out.println( y + " present:   " + inArrayIterativeSorted(xb, y));

         
        Integer[] xi = {2, 4, 6, 8, 10, 12, 14, 16, 18};
        System.out.println("Before sorting: " + Arrays.toString(xi));
        modifiedSelectionSort(xi, xi.length);
        System.out.println("After sorting: " + Arrays.toString(xi));
 
        int n = xi.length;
        int t = n * (n - 1) / 2;  
        System.out.println("Comparisons required to sort " + n + " values: " + t);
    }

    // Problem 1
    public static <T extends Comparable<? super T>>
    boolean inArrayIterativeSorted(T[] anArray, T anEntry) {
        
        int x = 0;
        int u = anArray.length - 1;
        while (x <= u) {
            int p = (x + u) / 2;
            int cmp = anArray[p].compareTo(anEntry);
            if (cmp == 0) {
                return true;
            } else if (cmp < 0) {
                x = p + 1;
            } else {
                u = p - 1;
            }
        }
        return false;
    }

    // Problem 2
    public static <T extends Comparable<? super T>>
    void modifiedSelectionSort(T[] a, int n) {
        for (int i = 0; i < n / 2; i++) {
            int y = i;
            int z = i;
            for (int j = i + 1; j < n - i; j++) {
                if (a[j].compareTo(a[y]) < 0) {
                    y = j;
                } else if (a[j].compareTo(a[z]) > 0) {
                    z = j;
                }
            }
            T temp = a[i];
            a[i] = a[y];
            a[y] = temp;

            if (z == i) {
                z = y;  
            }

            temp = a[n - i - 1];
            a[n - i - 1] = a[z];
            a[z] = temp;
        }
    }
}

"

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
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.
Similar questions
  • SEE MORE QUESTIONS
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