diff --git a/BianrySearch.java b/BianrySearch.java new file mode 100644 index 0000000..615d34c --- /dev/null +++ b/BianrySearch.java @@ -0,0 +1,33 @@ +public class BinarySearch { + + public static void main(String[] args){ + int[] dataSet = { 1, 2, 3, 5, 13, 15, 18, 29, 31, 36, 40, 47, 51, 59, 72 }; + + int UP = dataSet.length; + int LOW = 0; + + int x = 31; + + int result = binarySearch(dataSet, LOW, UP - 1, x); + if (result == -1) + System.out.println("Element not found"); + else + System.out.printf("Element found at index : %d", result); + } + + static int binarySearch(int array[], int low, int up, int x){ + if (up >= low) { + int mid = (low + up) / 2; + + if (x == array[mid]) + return mid; + else if (x < array[mid]) + return binarySearch(array, low, mid - 1, x); + else{ + return binarySearch(array, mid + 1, up, x); + } + } + return -1; + } + +} diff --git a/BubbleSort.java b/BubbleSort.java new file mode 100644 index 0000000..0d995cf --- /dev/null +++ b/BubbleSort.java @@ -0,0 +1,27 @@ +import java.util.Arrays; + +public class BubbleSort { + public static void main(String []args) { + int[] dataSet = { 54, 26, 95, 11, 48 }; + bubbleSort(dataSet); + + System.out.println("Sorted Array : "); + System.out.println(Arrays.toString(dataSet)); + } + + static void bubbleSort(int array[]){ + int size = array.length; + + for (int i = 0; i < size - 1 ; i++) { + for (int j = 0; j < size - 1 - i; j++) { + if (array[j+1] < array[j]) + { + int temp = array[j]; + array[j] = array[j+1]; + array[j+1] = temp; + } + } + } + } + +} diff --git a/InsertionSort.java b/InsertionSort.java new file mode 100644 index 0000000..768bbce --- /dev/null +++ b/InsertionSort.java @@ -0,0 +1,27 @@ +import java.util.Arrays; + +public class InsertionSort { + public static void main(String[] args) { + int[] dataSet = { 6, 3, 8, 7, 2, 4, 1, 5 }; + insertionSort(dataSet); + + System.out.println("Sorted Array: "); + System.out.println(Arrays.toString(dataSet)); + } + + static void insertionSort(int[] array){ + int size = array.length; + + for(int i = 1; i < size; i++){ + int temp = array[i]; + int j = i - 1 ; + + while( j >= 0 && temp < array[j] ){ + array[j+1] = array[j]; + j--; + } + array[j+1] = temp; + } + } + +}