Example Includes: Sequential Search, Finding Maximum Value, Insertion Sort, Binary Search

Example Includes: Sequential Search, Finding Maximum Value, Insertion Sort, Binary Search

<p>ARRAYS Example includes: sequential search, finding maximum value, insertion sort, binary search package packArraySeqSearch; public class TestSeqSearch {</p><p> public static void main(String[] args) { int[] A = { 2, 4, 7, 8, 2, 1, 5}; showArray(A); int answer = seqSearch(A, 11); System.out.println("answer is: " + answer);</p><p> int theBigGuy = locateMax(A); System.out.println("position of max is: " + theBigGuy + " and the value is: " + A[theBigGuy] );</p><p> sortByInsertion(A);</p><p> int sol = binarySearch(A, 8); System.out.println(sol);</p><p>}//end main</p><p> public static void showArray (int[] A) { for(int n=0; n<A.length; n++) { System.out.print(" A[" + n + "]= " + A[n]); } System.out.println("\n"); }//end showArary</p><p> public static int seqSearch(int[] B, int searchValue) { int i=0; boolean found= false; int pos = -1; while ((found == false) && (i < B.length)) { if (B[i]==searchValue) { found = true; pos = i; } i++; } return pos; }</p><p> public static int locateMax(int[] B) { int pos = 0; for (int i=1; i<B.length; i++){ if (B[pos] < B[i]) { pos = i; } } return pos; }</p><p> public static int locateMax2(int[] B, int size) { int pos = 0; for (int i=0; i < size; i++){ if (B[pos] < B[i]) { pos = i; } } return pos; } public static void sortByInsertion(int[] A) { //use insertion sort (max goes to last pos.) int posMax=0; int temp; for (int theLast= A.length-1; theLast>1; theLast--) { posMax = locateMax2(A, theLast); temp = A[theLast]; A[theLast] = A[posMax]; A[posMax] = temp; showArray(A);</p><p>} }</p><p> public static int binarySearch(int[] A, int searchValue) { int first=0, last=A.length-1; int middle=0; int result=0; boolean found= false;</p><p> while ((first <= last) && (found==false)) { middle =(int) (first + last)/2; if (A[middle] == searchValue){ found = true; } else if (A[middle] < searchValue){ first = middle + 1; } else { last = middle - 1; }//end else------</p><p>}//end while ------</p><p> if (found == false) result = -1; else result = middle;</p><p> return result; }</p><p>}</p>

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    2 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us