bubble-sort.java
public class SmallestInArrayExample{
public static int getSmallest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[0];
}
public static void main(String args[]){
int a[]={1,2,4,6,3,2};
int b[]={44,66,88,77,33,22,55};
System.out.println("Smallest: "+getSmallest(a,6));
System.out.println("Smallest: "+getSmallest(b,7));
} }
linear_search.java
class BinarySearchExample{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {1,2,3,4,5,6,7};
int key = 3;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}