Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CPP/Data Structure/binary_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>
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;
}
30 changes: 30 additions & 0 deletions Java/Algorithm/bubblesort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}