Java Interview Programs - Part 2

14) Bubble sort
public class BubbleSort {

 public static void main(String[] args) {

  int[] arr = { 6, 5, 4, 3, 2, 1 };
  int temp;

  boolean flag = false;

  for (int i = 0; i < arr.length - 1; i++) {

   for (int j = 0; j < arr.length - 1; j++) {
    if (arr[j] > arr[j + 1]) {
     temp = arr[j];
     arr[j] = arr[j + 1];
     arr[j + 1] = temp;
     flag = true;
    }
   }

   if (flag == false)
    break;
  }

  for (int i = 0; i < arr.length; i++)// length is the property of array
   System.out.print(arr[i] + " ");
 }
}
Output:
Array Before Bubble Sort
6 5 4 3 2 1 
Array After Bubble Sort
1 2 3 4 5 6

15) Selection sort
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class SelectionSortExample {

 public static void selectionSort(int[] arr) {
  for (int i = 0; i < arr.length - 1; i++) {
   int index = i;
   for (int j = i + 1; j < arr.length; j++) {
    if (arr[j] < arr[index]) {
     index = j;// searching for lowest index
    }
   }
   int smallerNumber = arr[index];
   arr[index] = arr[i];
   arr[i] = smallerNumber;
  }
 }

 public static void main(String a[]) {
  int[] arr1 = { 9, 14, 3, 2, 43, 11, 58, 22 };
  System.out.println("Before Selection Sort");
  for (int i : arr1) {
   System.out.print(i + " ");
  }
  System.out.println();

  selectionSort(arr1);// sorting array using selection sort

  System.out.println("After Selection Sort");
  for (int i : arr1) {
   System.out.print(i + " ");
  }
 }
}
Output:
Before Selection Sort
9 14 3 2 43 11 58 22 
After Selection Sort
2 3 9 11 14 22 43 58 

16) Insertion Sort
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class InsertionSortExample {
 public static void insertionSort(int array[]) {
  int n = array.length;
  for (int j = 1; j < n; j++) {
   int key = array[j];
   int i = j - 1;
   while ((i > -1) && (array[i] > key)) {
    array[i + 1] = array[i];
    i--;
   }
   array[i + 1] = key;
  }
 }

 public static void main(String a[]) {
  int[] arr1 = { 9, 14, 3, 2, 43, 11, 58, 22 };
  System.out.println("Before Insertion Sort");
  for (int i : arr1) {
   System.out.print(i + " ");
  }
  System.out.println();

  insertionSort(arr1);// sorting array using insertion sort

  System.out.println("After Insertion Sort");
  for (int i : arr1) {
   System.out.print(i + " ");
  }
 }
}
Output:
Before Insertion Sort
9 14 3 2 43 11 58 22 
After Insertion Sort
2 3 9 11 14 22 43 58 

17) Binary Search
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class BinarySearch {

 public int fbinarySearch(int[] arr, int num) {

  int start = 0;
  int end = (arr.length - 1);

  while (start <= end) {

   int mid = (start + end) / 2;
   System.out.println("Index examined :- " + arr[mid]);

   if (num == arr[mid]) {
    return mid;
   }

   if (num < arr[mid]) {
    end = mid - 1;
   } else {
    start = mid + 1;
   }
  }
  return -1;
 }

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  int[] arr = { 3, 8, 22, 29, 43, 55, 61, 74, 78, 95, 96 };
  int numToSearch = 29;

  BinarySearch bs = new BinarySearch();

  int result = bs.fbinarySearch(arr, numToSearch);

  if (result == -1) {
   System.out.println("Number is not found in the array");
  } else {
   System.out.println("The index of element " + numToSearch + " is " + result);
  }
 }
}
Output:
Index examined :- 55
Index examined :- 22
Index examined :- 29
The index of element 29 is 3


<-- Previous || Next -->

1 comment: