Java data structures and algorithms (Robert lafore); Chapter 2
/* Programming job 2.1 to higharray. add a method named getmax () to the higharray class of the Java program (listing 2.3). It returns the value of the maximum keyword in the array and returns-1 if the array is empty. Add some code to main () to use this method. It can be assumed that all keywords are positive numbers. 2.2 modify the method in programming job 2.1 so that not only the maximum keyword is returned, but also the keyword is deleted from the array. Name this method removemax (). The removemax () method in 2.3 programming job 2.2 provides a way to sort arrays by keyword values. To implement a sorting scheme, you only need to modify the code in main () without modifying the higharray class. This method requires the second array. The group data items are sorted in reverse order at the end of sorting. (This method is used to select a variant of sorting in "simple sorting" in Chapter 3rd .) 2.4 modify the orderedarray. Java program (list 2.4) so that the insert (), delete () and find () methods use binary lookup, as is recommended in the book. 2.5 Add a merge () method to the ordarray class of the orderedarray. Java program (listing 2.4) to combine two ordered source numbers into an ordered destination array. Add code in main (), insert a random number to the two source arrays, call the Merge () method, and display the result array. The number of data items in the two source arrays may be different. In the algorithm, you need to first compare the keywords in the source array, and select the smallest data item from it to copy it to the destination array. At the same time, we also need to consider how to solve the problem that when the data items in the source array have been obtained and the other has some data items left. 2.6 Add a nodup () method to the higharray class of the higharray. Java program (listing 2.3) so that all repeated data items in the array can be deleted. That is, if the keyword of three data items in the array is 17, the nodup () method deletes two of them. You do not have to consider maintaining the order of data items. One way is to first compare each data item with other data items, and overwrite duplicate data items with null (or a special value that is not used in a real keyword. Delete all the null values, and reduce the size of the array. */Package chap02; // higharray. java // demonstrates array class with high-level interface // to run this program: c> JAVA higharrayapp //////////////////////////////////// /// // class higharray {private long []; // ref to Array Using vate int nelems; // number of data items // ------------------------------------------------------------- public higharray (INT max) // constructor {A = new Long [Max]; // create the arraynelems = 0; // No items yet} // specify public Boolean find (long searchkey) {// find specified valueint J; for (j = 0; j <nelems; j ++) // for each element, if (a [J] = searchkey) // found item? Break; // exit loop before endif (j = nelems) // gone to end? Return false; // yes, can't find itelsereturn true; // No, found it} // end find () // revoke public void insert (long value) // put element into Array {A [nelems] = value; // insert itnelems ++; // increment size} // specify public Boolean Delete (long value) {Int J; for (j = 0; j <nelems; j ++) // look fo R ITIF (value = A [J]) break; If (j = nelems) // can't find itreturn false; else // found it {for (int K = J; k <nelems; k ++) // move higher ones downa [k] = A [k + 1]; // here is not a move more once when k = nElems-1, a [nElems-1] = A [nelems]; at this time a [nelems] is empty nelems --; // decrement sizereturn true;} // end Delete () // ----------------------------------------------------------- public void display () // displays array contents {(Int J = 0; j <nelems; j ++) // for each element, system. out. print (A [J] + ""); // display itsystem. out. println ("");} // ----------------------------------------------------------- // ============================================== ========================================/// p50 (69) programming job 2.1 Public long getmax () {long max =-1; // maximum element value for (Int J = 0; j <nelems; j ++) {if (a [J]> MAX) {max = A [J] ;}} return Max ;} // ================================ ===================================================/ /P50 (69) programming job 2.2 Public long removemax () {long max =-1; // The maximum element value int Index =-1; // Index Number of the largest element for (Int J = 0; j <nelems; j ++) {if (a [J]> MAX) {max = A [J]; index = J ;}} if (index! =-1) {// find the maximum element for (INT I = index + 1; I <nelems; I ++) {A [I-1] = A [I];} nelems --;} return Max ;} // ================================================ ==================================/// p50 (69) programming job 2.6 // first method public void nodup () {int null =-1; // use-1 as a special value for (Int J = 0; j <nelems; j ++) {for (INT I = J + 1; I <nelems; I ++) {if (a [J]! = NULL & A [J] = A [I]) {A [I] = NULL ;}}for (INT I = 0; I <nelems ;) {if (a [I] = NULL) {// Note: Do not use I ++ after moving. Check whether the current position is nullfor (Int J = I + 1; j <nelems; j ++) {A [J-1] = A [J];} nelems --;} else {I ++; // not null, direct I ++ ;}}// method 2 Public void nodup1 () {int null =-1; // use-1 as a special value, assume that the user does not enter a negative element for (Int J = 0; j <nelems; j ++) {for (INT I = J + 1; I <nelems; I ++) {if (a [J]! = NULL & A [J] = A [I]) {A [I] = NULL ;}} int order = 0; For (INT temp = 0; temp <nelems; temp ++) {if (a [temp]! = NULL) {// This method can be used if (temp> order) {A [order] = A [temp];} because a [0] cannot be null. order ++ ;}} nelems = order ;} // ================================================ =============================} // end class higharray ////////////// //////////////////////////////////////// //// // public class higharrayapp {public static void main (string [] ARGs) {int maxsize = 100; // array sizehigharray arr; // reference to arrayarr = new high Array (maxsize); // create the arrayarr. insert (77); // Insert 10 itemsarr. insert (99); arr. insert (44); arr. insert (55); arr. insert (22); arr. insert (88); arr. insert (11); arr. insert (00); arr. insert (66); arr. insert (33); arr. display (); // display itemsint searchkey = 35; // search for itemif (ARR. find (searchkey) system. out. println ("found" + searchkey); elsesystem. out. println ("can't find" + searchkey); arr. delete (0 0); // delete 3 itemsarr. delete (55); arr. delete (99); arr. display (); // display items again // ======================================== ======================================/// p50 (69) programming job 2.1 Long max = arr. getmax (); system. out. println ("found Max is" + max ); // ================================================ ==========================/// p50 (69) programming job 2.2arr.removemax (); arr. display (); // ================================================ ============================/// P50 (69) programming job 2.3 higharray sortedarr = new higharray (maxsize); int I = 0; max = arr. removemax (); While (max! =-1) {sortedarr. insert (max); max = arr. removemax ();} system. out. println ("reverse order:"); sortedarr. display (); // ================================================ ======================== arr. insert (77); // Insert 10 itemsarr. insert (99); arr. insert (44); arr. insert (55); arr. insert (22); // duplicate value arr. insert (44); arr. insert (77); arr. insert (44); arr. insert (66); arr. insert (88); arr. insert (11); arr. insert (00); arr. insert (66); arr. insert (33); system. out. println ("after repeated values are added:"); arr. display (); // arr. nodup (); arr. nodup1 (); system. out. println ("Remove duplicate values:"); arr. display (); // ================================================ =======================} // end main ()} // end class higharrayapp
Package chap02; // orderedarray. java // demonstrates ordered array class // to run this program: c> JAVA orderedapp //////////////////////////////////// /// // class ordarray {private long []; // ref to Array Using vate int nelems; // number of data items // using public ordarray (INT max) // constructor {A = new long [Max]; // create Rraynelems = 0;} // returns public int size () {return nelems;} // ------------------------------------------------------- public int find (long searchkey) {int lowerbound = 0; int upperbound = nelems-1; int curin; while (true) {curin = (lowerbound + upperbound)/2; if (a [curin] = searchkey) return curin; // found itelse if (lowerbound> upperbound) Return nelems; // can't find itelse // divide range {if (a [curin] <searchkey) lowerbound = curin + 1; // It's in upper halfelseupperbound = curin-1; // It's in lower half} // end else divide range} // end while} // end find () // define public void insert (long value) // put element into Array {Int J; For (j = 0; j <nelems; j ++) // find where it goes If (A [J]> value) // (linear search) break; For (int K = nelems; k> J; k --) // move bigger ones upa [k] = A [k-1]; A [J] = value; // insert itnelems ++; // increment size} // end insert () // ----------------------------------------------------------- // ============================================== ====================================/// programming job 2.4 p50 (69) public void insert1 (long value) {If (nelems = 0) {// No element, insert a [0] = value; ne directly Lems ++; return;} int lowerbound = 0; int upperbound = nelems-1; int curin; while (true) {curin = (lowerbound + upperbound)/2; if (lowerbound> upperbound) {break;} if (a [curin] = value) break; // found itelse if (a [curin] <value) // divide range {If (curin = nelems-1) {curin = curin + 1; break;} else if (a [curin + 1]> = value) {curin = curin + 1; break;} else {lowerbound = curin + 1; // note that this is + 1} el Se {If (curin = 0) {break;} else if (a [curin-1] <= value) {break;} else {upperbound = curin-1; // Note: here is-1 ;}}for (int K = nelems; k> curin; k --) // move bigger ones upa [k] = A [k-1]; A [curin] = value; // insert itnelems ++; // increment size} // ====================================== ========================================/// p50 (69) programming job 2.4 Delete () binary Search is used. ====================================== Public Boolean Delete (long value) {Int J = find (value); // Delete () has used binary search if (j = nelems) // can't find itreturn false; else // found it {for (int K = J; k <nelems; k ++) // move bigger ones downa [k] = A [k + 1]; nelems --; // decrement sizereturn true ;}// end Delete () // ------------------------------------------------------------- public void display () // displays array contents {for (Int J = 0; j <n Elems; j ++) // for each element, system. out. print (A [J] + ""); // display itsystem. out. println ("");} // ----------------------------------------------------------- // ============================================== ====================/// p50 (69) programming job 2.5 Public ordarray Merge (ordarray orderarr) {// assume that the array space is always sufficient ordarray Dist = new ordarray (this. nelems + orderarr. nelems); int Index = 0; For (INT I = 0; I <orderarr. size (); I ++) {Dist. insert (orderarr. A [I]) ;}for (INT I = 0; I <this. size (); I ++) {Dist. insert (this. A [I]);} return Dist ;} // ================================================ =============} // end class ordarray ////////////////////// //////////////////////////////////////// // public class orderedapp {public static void main (string [] ARGs) {int maxsize = 100; // array sizeordarray arr; // reference to arrayarr = new ordarray (M Axsize); // create the arrayarr. insert (77); // Insert 10 itemsarr. insert (99); arr. insert (44); arr. insert (55); arr. insert (22); arr. insert (88); arr. insert (11); arr. insert (00); arr. insert (66); arr. insert (33); int searchkey = 55; // search for itemif (ARR. find (searchkey )! = Arr. size () system. out. println ("found" + searchkey); elsesystem. out. println ("can't find" + searchkey); arr. display (); // display itemsarr. delete (00); // delete 3 itemsarr. delete (55); arr. delete (99); arr. display (); // display items again // ======================================/// programming job 2.4 p50 (69) arr = new ordarray (maxsize); // create the arrayarr. insert1 (4); // Insert 10 itemsarr. insert1 (3); arr. insert1 (2); arr. insert1 (1); arr. display (); // display items again // programming job 2.5 p50 (69) system. out. println ("second array:"); ordarray arr1 = new ordarray (maxsize); // create the arrayarr1.insert (10); arr1.insert (20); arr1.insert (30 ); arr1.insert (40); arr1.insert (50); arr1.insert (60); arr1.insert (70); arr1.display (); system. out. println ("merge two arrays to generate a new array:"); ordarray arr2 = arr. merge (arr1); arr2.display (); // ===========================} // end main ()} // end class orderedapp