Question: I need code for counting the array for the isSorted part. 3.1: Write a noDups() method for the HighArray class. This method should remove all
I need code for counting the array for the "isSorted" part.
3.1: Write a noDups() method for the HighArray class. This method should remove all duplicates from the array. That is, if three items with the key 17 appear in the array, noDups() should remove two of them. Dont worry about maintaining the order of the items. Of course, the array size will be reduced. Finally, in the answer section of your lab report, please analyze the time complexity of your method.
3.2: Write a isSorted() method for the HighArray class. This method should check if the array is sorted or not. if the array is sorted in ascending order, return 1; If the array is sorted in descending order, return -1; For all other situations (such as array is not sorted, array is empty, array has only 1 item, or all items in the array are equal), return 0. Finally, in the answer section of your lab report, please analyze the time complexity of your method.
// highArray.java // demonstrates array class with high-level interface // to run this program: C>java HighArrayApp //////////////////////////////////////////////////////////////// class HighArray { private long[] a; // ref to array a private int nElems; // number of data items public int[] removeDupe; //----------------------------------------------------------- public HighArray(int max) // constructor { a = new long[max]; // create the array nElems = 0; // no items yet } //----------------------------------------------------------- public boolean find(long searchKey) { // find specified value int j; for(j=0; j } //----------------------------------------------------------- public boolean isSorted() { for(int i = 0; i < nElems - 1; i++) { if (a[i] > a[i+1]) { return false; } } return true; } //----------------------------------------------------------- public void display() // displays array contents { for(int j=0; j arr.insert(99); arr.insert(77); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(77); arr.insert(11); arr.insert(00); arr.insert(77); arr.insert(66); arr.insert(33); arr.display(); // display items System.out.println("Remove all but one of 77"); long key = 77; arr.removeDupe(key); arr.display(); System.out.println("Sorted Array"); arr.isSorted(); arr.display(); // display items again } // end main() } // end class HighArrayApp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
