/* XBN Java: Generically useful, non-GUI Java code. http://sourceforge.net/projects/xbnjava Copyright (C) 1997-2003, Jeff Epstein All rights reserved. Modifications: No Redistribution in binary form, with or without modifications, are permitted provided that the following conditions are met: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * If modifications are made to source code then this license should indicate that fact in the "Modifications" section above. * Neither the author, nor the contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [NOTE: This license contains NO advertising clause.] */ package xbn_examples.array; import xbn.programs.XBNStatic; import xbn.array.BinarySearchData; import xbn.string.padchop.PadChopString; import xbn.string.padchop.ConfigPad; /**

Example code for BinarySearchData and UtilArray, where the int array to search is descending.

Source code:  XmplBinarySearchData_desc.java

@version 0.9b @author Jeff Epstein, http://sourceforge.net/projects/xbnjava. **/ public class XmplBinarySearchData_desc extends XBNStatic { private static final PadChopString pcsPad2 = new PadChopString(new ConfigPad(2)); private static final int[] aiTO_SEARCH_DESCENDING = {50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0, -2, -4, -6, -8, -10, -12, -14, -16, -18, -20, -22, -24, -26, -28, -30, -32, -34, -36, -38, -40, -42, -44, -46, -48, -50}; private static final boolean bSORTED_DESCENDING = false; public static final void main(String[] as_cmdLineParams) { sopl("\n\nXmplBinarySearchData_desc...START\n"); search(-59); search(-30); search(0); search(1); search(103); sopl("\n\nXmplBinarySearchData_desc...END"); } private static void search(int i_toSearchFor) { sopl("\tSearching DESCENDING array for number " + i_toSearchFor + "..."); BinarySearchData bsd = new BinarySearchData(aiTO_SEARCH_DESCENDING.length, bSORTED_DESCENDING); //iPrevMiddle is only needed when you want the index at //which to insert a not-found number. int iPrevMiddle = -1; while(bsd.getIdxMiddle() != -1) { iPrevMiddle = bsd.getIdxMiddle(); int iNumberAtMiddle = aiTO_SEARCH_DESCENDING[bsd.getIdxMiddle()]; sopl("\t[" + getIdxs(bsd) + "] array[" + bsd.getIdxMiddle() + "]=" + iNumberAtMiddle + " "); if(i_toSearchFor == iNumberAtMiddle) { sopl("\t-------FOUND at index " + bsd.getIdxMiddle() + "-------\n\n"); return; } bsd.prepareForNextSearch(i_toSearchFor < iNumberAtMiddle); } //The BinarySearchData object will only return a number //that is a valid array index. If this number belongs //at the end (after the last item in the array)... if(iPrevMiddle == (aiTO_SEARCH_DESCENDING.length - 1)) { if((bSORTED_DESCENDING && aiTO_SEARCH_DESCENDING[iPrevMiddle] < i_toSearchFor) || (!bSORTED_DESCENDING && aiTO_SEARCH_DESCENDING[iPrevMiddle] > i_toSearchFor)) { iPrevMiddle++; } } sopl("\t-------not found (index at which to insert/add=" + iPrevMiddle + ")-------\n\n"); } private static final String getIdxs(BinarySearchData binary_searchData) { return pcsPad2.get(binary_searchData.getIdxLeft()) + " " + pcsPad2.get(binary_searchData.getIdxMiddle()) + " " + pcsPad2.get(binary_searchData.getIdxRight()); } }