Adding in more Algorithms and Data Structures implemented in Java
This commit is contained in:
@@ -4,30 +4,109 @@ import java.util.Random;
|
||||
public class ArraySorting {
|
||||
public static void main(String[] args) {
|
||||
Random rand = new Random();
|
||||
int[] numbers = new int[100000000];
|
||||
int[] numbers = new int[10];
|
||||
int[] array = { 20, 30, 40, 50, 60, 70, 80, 90, 100 };
|
||||
|
||||
for(int i = 0; i < numbers.length; i++) {
|
||||
numbers[i] = rand.nextInt(10000);
|
||||
numbers[i] = rand.nextInt(10);
|
||||
}
|
||||
|
||||
System.out.println("Before: ");
|
||||
|
||||
|
||||
// System.out.println("Before: " + Arrays.toString(numbers));
|
||||
int result = binarySearch(array, 0, array.length - 1, 40);
|
||||
// quickSort(numbers);
|
||||
// mergeSort(numbers, 0, numbers.length - 1);
|
||||
// selectionSort(numbers);
|
||||
// bubbleSort(numbers);
|
||||
// Arrays.sort(numbers);
|
||||
// insertionSort(numbers, 4, 8);
|
||||
System.out.println("After: ");
|
||||
// insertionSort(numbers, 0, numbers.length);
|
||||
// System.out.println("After: " + Arrays.toString(numbers));
|
||||
if (result == -1) {
|
||||
System.out.println("Element not present");
|
||||
} else {
|
||||
System.out.println("Element found at index " + result);
|
||||
}
|
||||
}
|
||||
|
||||
public static void quickSort(int[] numbers) {
|
||||
private static int binarySearch(int[] array, int left, int right, int targetValue) {
|
||||
if (right >= 1) {
|
||||
int mid = 1 + (right - 1) / 2;
|
||||
|
||||
// If the element is present at the middle itself
|
||||
if (array[mid] == targetValue) {
|
||||
return mid;
|
||||
}
|
||||
|
||||
// If the element is smaller than mid, hen it can only be present in left subarray
|
||||
if (array[mid] > targetValue) {
|
||||
return binarySearch(array, 1, mid - 1, targetValue);
|
||||
}
|
||||
|
||||
// Else the element can only be present in the right subarray
|
||||
return binarySearch(array, mid + 1, right, targetValue);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* QUICK SORT ALGORITHM */
|
||||
|
||||
private static void swap(int[] array, int index1, int index2) {
|
||||
int temp = array[index1];
|
||||
array[index1] = array[index2];
|
||||
array[index2] = temp;
|
||||
}
|
||||
|
||||
private static int partition(int[] array, int lowIndex, int highIndex, int pivot) {
|
||||
int leftPointer = lowIndex;
|
||||
int rightPointer = highIndex;
|
||||
|
||||
while (leftPointer < rightPointer) {
|
||||
while (array[leftPointer] <= pivot && leftPointer < rightPointer) {
|
||||
leftPointer++;
|
||||
}
|
||||
|
||||
while (array[rightPointer] >= pivot && leftPointer < rightPointer) {
|
||||
rightPointer--;
|
||||
}
|
||||
|
||||
swap(array, leftPointer, rightPointer);
|
||||
}
|
||||
|
||||
if (array[leftPointer] > array[highIndex]) {
|
||||
swap(array, leftPointer, highIndex);
|
||||
} else {
|
||||
leftPointer = highIndex;
|
||||
}
|
||||
|
||||
return leftPointer;
|
||||
}
|
||||
|
||||
private static void quickSort(int[] array) {
|
||||
quickSort(array, 0, array.length - 1);
|
||||
}
|
||||
|
||||
private static void quickSort(int[] array, int lowIndex, int highIndex) {
|
||||
|
||||
// Check to see if it's trying to sort a subarray of one item
|
||||
if (lowIndex >= highIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
int pivotIndex = new Random().nextInt(highIndex - lowIndex) + lowIndex;
|
||||
int pivot = array[pivotIndex];
|
||||
swap(array, pivotIndex, highIndex);
|
||||
|
||||
int leftPointer = partition(array, lowIndex, highIndex, pivot);
|
||||
|
||||
quickSort(array, lowIndex, leftPointer - 1);
|
||||
quickSort(array, leftPointer + 1, highIndex);
|
||||
}
|
||||
|
||||
/* END: QUICK SORT ALGORITHM */
|
||||
|
||||
/* MERGE SORT ALGORITHM */
|
||||
|
||||
public static void mergeSort(int[] array, int left, int right) {
|
||||
private static void mergeSort(int[] array, int left, int right) {
|
||||
if(left < right) {
|
||||
int mid = (left+right) / 2; // Find the middle element
|
||||
mergeSort(array, left, mid); // Sort the first half
|
||||
@@ -35,7 +114,8 @@ public class ArraySorting {
|
||||
merge(array, left, mid, right); // Merge the sorted halves
|
||||
}
|
||||
}
|
||||
public static void merge(int[] array, int left, int mid, int right) {
|
||||
|
||||
private static void merge(int[] array, int left, int mid, int right) {
|
||||
// Find sizes of two subarrays to be merged
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
Reference in New Issue
Block a user