Write array methods that carry out the following tasks A and

Write array methods that carry out the following tasks A and B, respectively, for an array of integers by completing the ArrayMethods class below.

Public class ArrayMethods

{ private int[ ] values; public ArrayMethods (int[ ] initialValues)

{values = initalValues; }

public boolean isIncreasingOrder( ) { … }

public void sortIncOrder( ) { … } }

A. Return true if the array is currently sorted in increasing order, otherwise return false.

B. Sort the array in an increasing order.

Write a program that initializes an array with a sequence of random numbers, displays the array, test the two methods, and displays the method call results (Boolean value and the sorted array).

Solution

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;

public class ArrayMethods

{
    private int[] values;
  
    public ArrayMethods (int[] initialValues)
    {
        values = initialValues;
    }

    public boolean isIncreasingOrder()
    {
        boolean result = true;
      
        for(int i=1;i<values.length;i++)
        {
            if(values[i-1]>values[i])
            {
                result = false;
                break;
            }
        }
        return result;
    }
  
    public void printArray()
    {
        for(int i=0;i<values.length;i++)
            System.out.print(values[i] + \" \");
        System.out.println();
    }
  
    private int partitionArray (int arr[], int start, int end)
    {
        int pivot = arr[end]; // pivot
        int smaller = (start - 1); // Index of smaller element
        int temp;

        for (int j = start; j <= end- 1; j++)
        {
            if (arr[j] <= pivot)
            {
                smaller++;
                temp = arr[smaller];
                arr[smaller] = arr[j];
                arr[j] = temp;
            }
        }

        temp = arr[smaller+1];
        arr[smaller+1] = arr[end];
        arr[end] = temp;

        return (smaller + 1);
    }

    private void quickSort(int arr[], int start, int end)
    {
        if(arr == null)
        {
            System.out.println(\"Please pass a valid array.\");
            return;
        }
        if (start < end)
        {
            //Partition the array based on the partition function defined.
            int partition_index = partitionArray(arr, start, end);

            // Separately sort elements before
            // partition and after partition
            quickSort(arr, start, partition_index - 1);
            quickSort(arr, partition_index + 1, end);
        }
    }
    public void sortIncOrder()
    {
        quickSort(values, 0, values.length-1);
    }
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg;

public class TestArrayMethod {
  
    public static void main(String[] args)
    {
        int[] arr = {5,4,3,2,1};
        ArrayMethods a = new ArrayMethods(arr);
      
        if(a.isIncreasingOrder()==true)
            System.out.println(\"Elements of array are already sorted.\");
        else
        {
            System.out.println(\"Elements of array are not sorted.Sorting Array elements : \ \");
            a.sortIncOrder();
            System.out.println(\"Array elements after sorting are : \");
            a.printArray();
        }
    }
}

OUTPUT:

run:
Elements of array are not sorted.Sorting Array elements :

Array elements after sorting are :
1 2 3 4 5
BUILD SUCCESSFUL (total time: 0 seconds)

Write array methods that carry out the following tasks A and B, respectively, for an array of integers by completing the ArrayMethods class below. Public class
Write array methods that carry out the following tasks A and B, respectively, for an array of integers by completing the ArrayMethods class below. Public class
Write array methods that carry out the following tasks A and B, respectively, for an array of integers by completing the ArrayMethods class below. Public class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site