java資料結構和演算法程式設計作業系列篇-陣列
阿新 • • 發佈:2018-11-15
/** * 程式設計作業 2.1 向highArray.java程式(清單2.3)的HighArray類新增一個名為getMax()的方法,它返回 陣列中最大關鍵字的值,當陣列為空時返回-1。向main()中新增一些程式碼來使用這個方法。 可以假設所有關鍵字都是正數。 2.2 修改程式設計作業2.1中的方法,使之不僅返回最大的關鍵字,而且還將該關鍵字從陣列中刪除。 將這個方法命名為removeMax()。 2.3 程式設計作業2.2中的removeMax()方法提供了一種通過關鍵字值進行陣列排序的方法。實現一個 排序方案,要求不修改HighArray類,只需對main()中的程式碼進行修改。這個方法需要第二個 陣列,在排序結束時陣列資料項是逆序排列的。(這個方法是第3章“簡單排序”中選擇排序的 一個變體。) 2.4 修改orderedArray.java程式(清單2.4)使insert()、delete()與find()方法一樣都使用 二分查詢,正如書中所建議的那樣。 2.5 向orderedArray.java程式(清單2.4)的OrdArray類加入一個merge()方法,使之可以將兩個 有序的源數組合併成一個有序的目的陣列。在main()中新增程式碼,向兩個源陣列中插入隨機數, 呼叫merge()方法,並將結果目的陣列顯示出來。兩個源陣列的資料項個數可能不同。在演算法中 需要先比較源陣列中的關鍵字,從中選出最小的一個數據項複製到目的陣列。同時還要考慮如何 解決當一個源陣列的資料項已經取完而另一個還剩一些資料項情況。 2.6 向highArray.java程式(清單2.3)的HighArray類中加入一個noDup()方法,使之可以將陣列中 的所有重複資料項刪除。即如果陣列中有三個資料項的關鍵字為17,noDup()方法會刪除其中的 兩個。不必考慮保持資料項的順序。一種方法是先用每一個數據項同其他資料項比較,並用null (或是一個不會用在真正的關鍵字中的特殊值)將重複的資料項覆蓋掉。然後將所有的null刪除, 當然還要縮小陣列的大小。*/
package chap2; /** * 程式設計作業 2.1 向highArray.java程式(清單2.3)的HighArray類新增一個名為getMax()的方法,它返回 陣列中最大關鍵字的值,當陣列為空時返回-1。向main()中新增一些程式碼來使用這個方法。 可以假設所有關鍵字都是正數。 2.2 修改程式設計作業2.1中的方法,使之不僅返回最大的關鍵字,而且還將該關鍵字從陣列中刪除。 將這個方法命名為removeMax()。 2.3 程式設計作業2.2中的removeMax()方法提供了一種通過關鍵字值進行陣列排序的方法。實現一個 排序方案,要求不修改HighArray類,只需對main()中的程式碼進行修改。這個方法需要第二個 陣列,在排序結束時陣列資料項是逆序排列的。(這個方法是第3章“簡單排序”中選擇排序的 一個變體。) 2.4 修改orderedArray.java程式(清單2.4)使insert()、delete()與find()方法一樣都使用 二分查詢,正如書中所建議的那樣。 2.5 向orderedArray.java程式(清單2.4)的OrdArray類加入一個merge()方法,使之可以將兩個 有序的源數組合併成一個有序的目的陣列。在main()中新增程式碼,向兩個源陣列中插入隨機數, 呼叫merge()方法,並將結果目的陣列顯示出來。兩個源陣列的資料項個數可能不同。在演算法中 需要先比較源陣列中的關鍵字,從中選出最小的一個數據項複製到目的陣列。同時還要考慮如何 解決當一個源陣列的資料項已經取完而另一個還剩一些資料項情況。 2.6 向highArray.java程式(清單2.3)的HighArray類中加入一個noDup()方法,使之可以將陣列中 的所有重複資料項刪除。即如果陣列中有三個資料項的關鍵字為17,noDup()方法會刪除其中的 兩個。不必考慮保持資料項的順序。一種方法是先用每一個數據項同其他資料項比較,並用null (或是一個不會用在真正的關鍵字中的特殊值)將重複的資料項覆蓋掉。然後將所有的null刪除, 當然還要縮小陣列的大小。*/ public class HighArray { private long[]a; private int nElems; public HighArray(int max){ a = new long[max]; nElems = 0; } public boolean find(long searchKey){ int j; for (j=0;j<nElems;j++) { if (a[j] == searchKey) {break; } } if (j == nElems) { return false; } else { return true; } } public void insert(long value){ a[nElems] = value; nElems++; } public boolean delete(long value){ int j; for (j = 0;j<nElems;j++) { if (value == a[j]) { break; } } if (j == nElems) { return false; } else { for (int k=j;k<nElems;k++) { a[k] = a[k+1]; } nElems--; return true; } } public void display() { for (int j = 0; j < nElems; j++) System.out.print(a[j] + " "); System.out.println(""); } //程式設計作業2.1 public long getMax(){ long max = -1; for (int j = 0;j<nElems;j++) { if (a[j]>max) { max = a[j]; } } return max; } //程式設計作業2.2 public long removeMax(){ long max = -1;//最大元素值 int index = -1;//最大元素的索引號 for (int j=0;j<nElems;j++) { if (a[j]>max) { max = a[j]; index = j; } } if (index != -1) { for (int i=index+1;i<nElems;i++) { a[i-1] = a[i]; } nElems--; } return max; } //程式設計作業2.6 public void noDup() { int NULL = -1; // 用-1作特殊值 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) {// 注意:移動完成後不要i++,再次檢查當前位置是否為NULL for (int j = i + 1; j < nElems; j++) { a[j - 1] = a[j]; } nElems--; } else { i++; // 不是NULL,直接i++; } } } }
package chap2; /** * Created by admin on 2018/11/15. */ public class HighArrayApp { public static void main(String[] args) { int maxSize = 100; // array size HighArray arr; // reference to array arr = new HighArray(maxSize); // create the array arr.insert(77); // insert 10 items arr.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 items int searchKey = 35; // search for item if (arr.find(searchKey)) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); arr.delete(00); // delete 3 items arr.delete(55); arr.delete(99); arr.display(); // display items again // ======================================================= // p50(69) 程式設計作業2.1 long max = arr.getMax(); System.out.println("Found max is " + max); // ======================================================= // p50(69) 程式設計作業2.2 arr.removeMax(); arr.display(); // ======================================================= // p50(69) 程式設計作業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("逆序排列:"); sortedArr.display(); // ======================================================= arr.insert(77); // insert 10 items arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); // 重複值 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("加入重複值後:"); arr.display(); arr.noDup(); //arr.noDup1(); System.out.println("去掉重複值後:"); arr.display(); } }
package chap2; /** * Created by admin on 2018/11/15. */ public class OrdArray { private long[]a; private int nElems; public OrdArray(int max){ a = new long[max]; nElems = 0; } 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; } else if (lowerBound > upperBound){//沒有找到 return nElems; } else { if (a[curIn] < searchKey) { lowerBound = curIn+1;// it's in upper half } else { upperBound = curIn-1;// it's in lower half } } } } public void insert(long value){ int j; for (j = 0;j<nElems;j++) { if (a[j]>value) { break; } } for (int k=nElems;k>j;k--) { a[k] = a[k-1]; } a[j] = value; nElems++; } //程式設計作業2.4 public void insert1(long value){ if (nElems == 0) { // 沒有元素,直接插入 a[0] = value; nElems++; 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; } else if (a[curIn] < value) { if (curIn == nElems - 1) { curIn = curIn + 1; break; } else if (a[curIn + 1] >= value) { curIn = curIn + 1; break; } else { lowerBound = curIn + 1; // 注意這裡是+1 } } else { if (curIn == 0) { break; } else if (a[curIn - 1] <= value) { break; } else { upperBound = curIn - 1; // 注意這裡是-1; } } } for (int k = nElems; k > curIn; k--){ // move bigger ones up a[k] = a[k - 1]; } a[curIn] = value; // insert it nElems++; // increment size } public boolean delete(long value){ int j = find(value); if (j == nElems) { return false; } else { for (int k=j;k<nElems;k++) { a[k] = a[k+1]; } nElems--; return true; } } public void display() // displays array contents { for (int j = 0; j < nElems; j++) // for each element, System.out.print(a[j] + " "); // display it System.out.println(""); } //程式設計作業2.5 public OrdArray merge(OrdArray orderArr){ 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; } }
package chap2; /** * Created by admin on 2018/11/15. */ public class OrderedApp { public static void main(String[] args) { int maxSize = 100; // array size OrdArray arr; // reference to array arr = new OrdArray(maxSize); // create the array arr.insert(77); // insert 10 items arr.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 item if (arr.find(searchKey) != arr.size()) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); arr.display(); // display items arr.delete(00); // delete 3 items arr.delete(55); arr.delete(99); arr.display(); // display items again // ============================ // 程式設計作業2.4 p50(69) arr = new OrdArray(maxSize); // create the array arr.insert1(4); // insert 10 items arr.insert1(3); arr.insert1(2); arr.insert1(1); arr.display(); // display items again // 程式設計作業2.5 p50(69) System.out.println("第二個陣列:"); OrdArray arr1 = new OrdArray(maxSize); // create the array arr1.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("合併兩個陣列,生成新的陣列:"); OrdArray arr2 = arr.merge(arr1); arr2.display(); // ============================ } // end main() }