From d7567ff3341608a9e6b75bd14e36fdb89ab21391 Mon Sep 17 00:00:00 2001 From: Shivakant Vishwakarma <30767792+shivakant1999@users.noreply.github.com> Date: Fri, 11 Oct 2019 12:37:46 +0530 Subject: [PATCH 1/2] create binary_search.cpp --- CPP/Data Structure/binary_search.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 CPP/Data Structure/binary_search.cpp diff --git a/CPP/Data Structure/binary_search.cpp b/CPP/Data Structure/binary_search.cpp new file mode 100644 index 0000000..9719c11 --- /dev/null +++ b/CPP/Data Structure/binary_search.cpp @@ -0,0 +1,25 @@ +#include +using namespace std; +int binarySearch(int arr[], int p, int r, int num) { + if (p <= r) { + int mid = (p + r)/2; + if (arr[mid] == num) + return mid ; + if (arr[mid] > num) + return binarySearch(arr, p, mid-1, num); + if (arr[mid] > num) + return binarySearch(arr, mid+1, r, num); + } + return -1; +} +int main(void) { + int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40}; + int n = sizeof(arr)/ sizeof(arr[0]); + int num = 33; + int index = binarySearch (arr, 0, n-1, num); + if(index == -1) + cout<< num <<" is not present in the array"; + else + cout<< num <<" is present at index "<< index <<" in the array"; + return 0; +} From 7fd4af69cff3d9e6d0dfb7229f5b2146de44cf8e Mon Sep 17 00:00:00 2001 From: antoniospataro Date: Fri, 11 Oct 2019 10:05:19 +0200 Subject: [PATCH 2/2] add bubble sort in java --- Java/Algorithm/bubblesort.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Java/Algorithm/bubblesort.java diff --git a/Java/Algorithm/bubblesort.java b/Java/Algorithm/bubblesort.java new file mode 100644 index 0000000..582bd49 --- /dev/null +++ b/Java/Algorithm/bubblesort.java @@ -0,0 +1,30 @@ +public class BubbleSort +{ + public static void main(String[] args) + { + Integer[] array = new Integer[] { 1, 5, 22, 2, 8, 7, 50, 30 }; + bubbleSort(array, 0, array.length); + System.out.println(Arrays.toString(array)); + } + + public static void bubbleSort(Object[] array, int m, int n) + { + Object d; + for (int i = n - 1; i > m; i--) + { + boolean isSorted = true; + for (int j = m; j < i; j++) + { + if (((Comparable) array[j]).compareTo(array[j + 1]) > 0) + { + isSorted =n + d = array[j + 1]; + array[j + 1] = array[j]; + array[j] = d; + } + } + if (isSorted) + break; + } + } +}