D:\Sam\InterviewsMaterial\SiteWorx\SiteWorxTest\src\java\services\BinarySearch_BubbleSortServices.java
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 package services;
  6 
  7 import java.util.Arrays;
  8 
  9 /**
 10  *
 11  * @author sameldin
 12  */
 13 public class BinarySearch_BubbleSortServices {
 14 
 15     /**
 16      * This class should not be instantiated.
 17      */
 18     private BinarySearch_BubbleSortServices() 
 19     { 
 20     }
 21 
 22     /**
 23      * Searches for the integer key in the sorted array passedArray[].
 24      * @param key the search key
 25      * @param passedArray the array of integers, must be sorted in ascending order
 26      * @return index of key in array passedArray[] if present; -1 if not present
 27      */
 28     public static int getIndexOfValue(int key, int[] passedArray) 
 29     {
 30         int low = 0;
 31         int high = passedArray.length - 1;
 32         while (low <= high) {
 33             // Key is in a[lo..hi] or not present.
 34             int mid = low + (high - low) / 2;
 35             if(key < passedArray[mid]) 
 36                 high = mid - 1;
 37             else 
 38                 if (key > passedArray[mid]) 
 39                     low = mid + 1;
 40                 else 
 41                     return mid;
 42         }
 43         return -1;
 44     }
 45     /*
 46      * 
 47      */
 48     public static boolean contains(int[] passedArray, int value) 
 49     {
 50         if (passedArray.length == 0) 
 51         {
 52             return false;
 53         }
 54         int low = 0;
 55         int high = passedArray.length-1;
 56 
 57         while(low <= high) 
 58         {
 59             int middle = (low+high) /2; 
 60             if (value > passedArray[middle])
 61             {
 62                 low = middle +1;
 63             } 
 64             else if (value < passedArray[middle])
 65             {
 66                 high = middle -1;
 67             } 
 68             else 
 69             { // The element has been found
 70                 return true; 
 71             }
 72         } // end while loop
 73         return false;
 74     }
 75     /*
 76      * 
 77      */
 78     public static void BubbleSortAscending( int [ ] passedArray )
 79     {
 80         int index;
 81         boolean foundFlag = true;   // set flag to true to begin first pass
 82         int temp;   //holding variable
 83 
 84         while ( foundFlag )
 85         {
 86             foundFlag= false;    //set flag to false awaiting a possible swap
 87             for( index = 0;  index < passedArray.length -1;  index++ )
 88             {
 89                    if ( passedArray[ index ] > passedArray[index+1] )   // change to > for ascending sort
 90                    {
 91                            temp = passedArray[ index ];                //swap elements
 92                            passedArray[ index ] = passedArray[ index+1 ];
 93                            passedArray[ index + 1 ] = temp;
 94                           foundFlag = true;              //shows a swap occurred  
 95                   } 
 96             } 
 97         } 
 98     } 
 99     /*
100      * 
101      */
102     public static void BubbleSortDescending( int [ ] passedArray )
103     {
104         int index;
105         boolean foundFlag = true;   // set flag to true to begin first pass
106         int temp;   //holding variable
107 
108         while ( foundFlag )
109         {
110             foundFlag= false;    //set flag to false awaiting a possible swap
111             for( index = 0;  index < passedArray.length -1;  index++ )
112             {
113                    if ( passedArray[ index ] < passedArray[index+1] )   // change to > for ascending sort
114                    {
115                            temp = passedArray[ index ];                //swap elements
116                            passedArray[ index ] = passedArray[ index+1 ];
117                            passedArray[ index + 1 ] = temp;
118                           foundFlag = true;              //shows a swap occurred  
119                   } 
120             } 
121         } 
122     } 
123     /* arrayLength
124      * In insertion sorting take the element form left assign value into a variable. 
125      * Then compare the  value with  previous values. 
126      * Put  value so that values must be lesser than the previous values. 
127      * Then assign  next  value to a variable and follow the same steps 
128      * relatively until the comparison not reached to end of array.  
129      * 
130      */
131     public static void insertion_srt(int passedArray[])
132     {
133         for (int count = 1; count < passedArray.length; count++)
134         {
135             int loop = count;
136             int value = passedArray[count];
137             while (     (loop > 0) 
138                    &&   (passedArray[loop - 1] > value)
139                   )
140             {
141                 passedArray[loop] = passedArray[loop - 1];
142                 loop--;
143             }
144             passedArray[loop] = value;
145         }// end of for loop
146     }    
147     /**
148      * 
149      */
150     public static void main(String[] args) 
151     {
152         int index;
153         int localArray[] = {12,19,4,89,125,1,3,7};
154         for(index = 0; index < localArray.length; index++)
155             System.out.print( localArray[index]+"  ");
156         System.out.println("==================================\n");
157         //insertion_srt(localArray);
158         //BubbleSortDescending(localArray);
159         BubbleSortAscending(localArray);
160         for(index = 0; index < localArray.length; index++)
161             System.out.print( localArray[index]+"  ");
162         System.out.println("==================================\n");
163     }
164 }