D:\Sam\InterviewsMaterial\SiteWorx\SiteWorxTest\src\java\services\BinarySearch_BubbleSortServices.java |
1
2
3
4
5 package services;
6
7 import java.util.Arrays;
8
9
10
11 @author
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
25 @param passedArray
26 @return
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
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 {
70 return true;
71 }
72 }
73 return false;
74 }
75
76
77
78 public static void BubbleSortAscending( int [ ] passedArray )
79 {
80 int index;
81 boolean foundFlag = true;
82 int temp;
83
84 while ( foundFlag )
85 {
86 foundFlag= false;
87 for( index = 0; index < passedArray.length -1; index++ )
88 {
89 if ( passedArray[ index ] > passedArray[index+1] )
90 {
91 temp = passedArray[ index ];
92 passedArray[ index ] = passedArray[ index+1 ];
93 passedArray[ index + 1 ] = temp;
94 foundFlag = true;
95 }
96 }
97 }
98 }
99
100
101
102 public static void BubbleSortDescending( int [ ] passedArray )
103 {
104 int index;
105 boolean foundFlag = true;
106 int temp;
107
108 while ( foundFlag )
109 {
110 foundFlag= false;
111 for( index = 0; index < passedArray.length -1; index++ )
112 {
113 if ( passedArray[ index ] < passedArray[index+1] )
114 {
115 temp = passedArray[ index ];
116 passedArray[ index ] = passedArray[ index+1 ];
117 passedArray[ index + 1 ] = temp;
118 foundFlag = true;
119 }
120 }
121 }
122 }
123
124
125
126
127
128
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 }
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
158
159 BubbleSortAscending(localArray);
160 for(index = 0; index < localArray.length; index++)
161 System.out.print( localArray[index]+" ");
162 System.out.println("==================================\n");
163 }
164 }