From aaf10fdbb73f40d52697ff34a1a1518fca5f3610 Mon Sep 17 00:00:00 2001 From: Fahad Israr Date: Sat, 5 Oct 2019 12:03:07 +0530 Subject: [PATCH 1/2] Create MergeSort.java #2 --- MergeSort.java | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 MergeSort.java diff --git a/MergeSort.java b/MergeSort.java new file mode 100644 index 0000000..c61cb0b --- /dev/null +++ b/MergeSort.java @@ -0,0 +1,104 @@ +/* Java program for Merge Sort */ +class MergeSort +{ + // Merges two subarrays of arr[]. + // First subarray is arr[l..m] + // Second subarray is arr[m+1..r] + void merge(int arr[], int l, int m, int r) + { + // Find sizes of two subarrays to be merged + int n1 = m - l + 1; + int n2 = r - m; + + /* Create temp arrays */ + int L[] = new int [n1]; + int R[] = new int [n2]; + + /*Copy data to temp arrays*/ + for (int i=0; i Date: Sat, 5 Oct 2019 12:04:28 +0530 Subject: [PATCH 2/2] Create QuickSort.java #2 --- QuickSort.java | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 QuickSort.java diff --git a/QuickSort.java b/QuickSort.java new file mode 100644 index 0000000..60fd294 --- /dev/null +++ b/QuickSort.java @@ -0,0 +1,77 @@ +// Java program for implementation of QuickSort +class QuickSort +{ + /* This function takes last element as pivot, + places the pivot element at its correct + position in sorted array, and places all + smaller (smaller than pivot) to left of + pivot and all greater elements to right + of pivot */ + int partition(int arr[], int low, int high) + { + int pivot = arr[high]; + int i = (low-1); // index of smaller element + for (int j=low; j Array to be sorted, + low --> Starting index, + high --> Ending index */ + void sort(int arr[], int low, int high) + { + if (low < high) + { + /* pi is partitioning index, arr[pi] is + now at right place */ + int pi = partition(arr, low, high); + + // Recursively sort elements before + // partition and after partition + sort(arr, low, pi-1); + sort(arr, pi+1, high); + } + } + + /* A utility function to print array of size n */ + static void printArray(int arr[]) + { + int n = arr.length; + for (int i=0; i