diff --git a/Java/MergeSort.java b/Java/MergeSort.java new file mode 100644 index 000000000..147fab61a --- /dev/null +++ b/Java/MergeSort.java @@ -0,0 +1,61 @@ +import java.util.Arrays; + +public class MergeSort { + public static void mergeSort(int[] array, int left, int right) { + if (left < right) { + int middle = (left + right) / 2; + + // Sort first and second halves + mergeSort(array, left, middle); + mergeSort(array, middle + 1, right); + + // Merge the sorted halves + merge(array, left, middle, right); + } + } + + // Merging two subarrays of array[] + public static void merge(int[] array, int left, int middle, int right) { + int n1 = middle - left + 1; + int n2 = right - middle; + + int[] leftArray = new int[n1]; + int[] rightArray = new int[n2]; + + System.arraycopy(array, left, leftArray, 0, n1); + System.arraycopy(array, middle + 1, rightArray, 0, n2); + + int i = 0, j = 0, k = left; + while (i < n1 && j < n2) { + if (leftArray[i] <= rightArray[j]) { + array[k] = leftArray[i]; + i++; + } else { + array[k] = rightArray[j]; + j++; + } + k++; + } + + while (i < n1) { + array[k] = leftArray[i]; + i++; + k++; + } + + while (j < n2) { + array[k] = rightArray[j]; + j++; + k++; + } + } + + public static void main(String[] args) { + int[] array = { 12, 11, 13, 5, 6, 7 }; + System.out.println("Original array: " + Arrays.toString(array)); + + mergeSort(array, 0, array.length - 1); + + System.out.println("Sorted array: " + Arrays.toString(array)); + } +} diff --git a/Java/Quicksort.java b/Java/Quicksort.java new file mode 100644 index 000000000..61133b8a6 --- /dev/null +++ b/Java/Quicksort.java @@ -0,0 +1,55 @@ +import java.util.Arrays; + +public class QuickSort { + + // Function to perform the quicksort + public static void quicksort(int[] array, int low, int high) { + if (low < high) { + // Partition the array and get the pivot index + int pivotIndex = partition(array, low, high); + + // Recursively apply quicksort to the left and right partitions + quicksort(array, low, pivotIndex - 1); + quicksort(array, pivotIndex + 1, high); + } + } + + // Function to partition the array + private static int partition(int[] array, int low, int high) { + // Choose the last element as the pivot + int pivot = array[high]; + int i = low - 1; // Pointer for greater element + + // Traverse through all elements + for (int j = low; j < high; j++) { + // If current element is smaller than the pivot + if (array[j] < pivot) { + i++; + // Swap element at i with element at j + swap(array, i, j); + } + } + + // Swap the pivot element with the element at i + 1 + swap(array, i + 1, high); + return i + 1; // Return the pivot index + } + + // Function to swap two elements + private static void swap(int[] array, int i, int j) { + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + + // Main method to test the quicksort algorithm + public static void main(String[] args) { + int[] array = { 34, 7, 23, 32, 5, 62, 14 }; + System.out.println("Original array: " + Arrays.toString(array)); + + // Apply quicksort + quicksort(array, 0, array.length - 1); + + System.out.println("Sorted array: " + Arrays.toString(array)); + } +} diff --git a/QuickSort.class b/QuickSort.class new file mode 100644 index 000000000..7be1e34c7 Binary files /dev/null and b/QuickSort.class differ