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;         }     } } "

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter6: Using Arrays
Section: Chapter Questions
Problem 19RQ
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
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage