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
33 changes: 33 additions & 0 deletions BianrySearch.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
27 changes: 27 additions & 0 deletions BubbleSort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}

}
27 changes: 27 additions & 0 deletions InsertionSort.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

}