From e445564530a61d26505121b6e7a5e736d1a8a818 Mon Sep 17 00:00:00 2001 From: Dasuni Udugama Sooriyage <58808249+DasuniMaheshika@users.noreply.github.com> Date: Fri, 15 Oct 2021 13:49:56 +0530 Subject: [PATCH 1/3] Create BianrySearch.java Binary Search using Java --- BianrySearch.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 BianrySearch.java 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; + } + +} From 1d46e2add7495b0372759617fd68b2dffc302937 Mon Sep 17 00:00:00 2001 From: Dasuni Udugama Sooriyage <58808249+DasuniMaheshika@users.noreply.github.com> Date: Fri, 15 Oct 2021 13:51:23 +0530 Subject: [PATCH 2/3] Create BubbleSort.java --- BubbleSort.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 BubbleSort.java 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; + } + } + } + } + +} From cceea96acbfa565a0fa31173412ce851ece98d62 Mon Sep 17 00:00:00 2001 From: Dasuni Udugama Sooriyage <58808249+DasuniMaheshika@users.noreply.github.com> Date: Fri, 15 Oct 2021 13:54:16 +0530 Subject: [PATCH 3/3] Create InsertionSort.java --- InsertionSort.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 InsertionSort.java 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; + } + } + +}