public static OrdArray merge(OrdArray arr1, OrdArray arr2) { int size = arr1.size() + arr2.size(); OrdArray arr = new OrdArray(size); int i = 0; int j = 0; int k = 0; while (j < arr1.size() && k < arr2.size()) { if (arr1.getVal(j) < arr2.getVal(k)) { arr.setVal(i, arr1.getVal(j)); j++; } else if (arr1.getVal(j) > arr2.getVal(k)) { arr.setVal(i, arr2.getVal(k)); k++; } else { arr.setVal(i, arr1.getVal(j)); arr.setVal(i, arr2.getVal(k)); j++; k++; } arr.nElems++; i++; } //ostatni warunek jeżeli któraś tablica jest dłuższa while (j < arr1.size()) { arr.setVal(i, arr1.getVal(j)); j++; i++; arr.nElems++; } while (k < arr2.size()) { arr.setVal(i, arr2.getVal(k)); k++; i++; arr.nElems++; } return(arr); }
static void Main(string[] args) { int maxSize = 100; // array size OrdArray arr1; // reference to array OrdArray arr2; arr1 = new OrdArray(maxSize); // create the array arr2 = new OrdArray(maxSize); arr1.insert(77); // insert 10 items arr1.insert(8); arr1.insert(44); arr1.insert(55); arr2.insert(22); arr2.insert(88); arr2.insert(11); arr2.insert(00); arr2.insert(66); arr2.insert(33); arr1.display(); arr2.display(); OrdArray arr3 = OrdArray.merge(arr1, arr2); Console.WriteLine("After merge: "); arr3.display(); int searchKey = 55; // search for item if (arr1.find(searchKey) != arr1.size()) { Console.WriteLine("Found " + searchKey); } else { Console.WriteLine("Can't find " + searchKey); } arr1.display(); // display items arr1.delete(00); // delete 3 items arr1.delete(55); arr1.delete(99); arr1.display(); // display items again }