Started implementing Merge/Quicksort.

This commit is contained in:
Jonathan Turner 2024-04-05 09:34:42 -04:00
parent d0a47f7b64
commit 8d916ed661
2 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,87 @@
package Assignments.A4;
public class Mergesort {
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int[] list, int left, int mid, int right) {
// Create temp arrays
int[] sub1 = new int[mid - left + 1];
int[] sub2 = new int[right - mid];
// Copy data to temp arrays
System.arraycopy(list, left, sub1, 0, sub1.length);
System.arraycopy(list, mid + 1, sub2, 0, sub2.length);
int l1 = 0;
int l2 = 0;
// Initial index of merged subarray array
int k = left;
while (l1 < sub1.length && l2 < sub2.length) {
if (sub1[l1] <= sub2[l2]) {
list[k] = sub1[l1];
l1++;
} else {
list[k] = sub2[l2];
l2++;
}
k++;
}
// Copy remaining elements of L[] if any
while (l1 < sub1.length) {
list[k] = sub1[l1];
l1++;
k++;
}
// Copy remaining elements of R[] if any
while (l2 < sub2.length) {
list[k] = sub2[l2];
l2++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int[] list, int left, int right) {
if (left < right) {
// Find the middle point
int m = left + (right - left) / 2;
// Sort first and second halves
sort(list, left, m);
sort(list, m + 1, right);
// Merge the sorted halves
merge(list, left, m, right);
}
}
// A utility function to print array of size n
static void printArray(int[] list) {
int n = list.length;
for (int i = 0; i < n; ++i)
System.out.print(list[i] + " ");
System.out.println();
}
// Driver code
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
System.out.println("Given array is");
printArray(arr);
Mergesort ob = new Mergesort();
ob.sort(arr, 0, arr.length - 1);
System.out.println("\nSorted array is");
printArray(arr);
}
}

View File

@ -0,0 +1,71 @@
package Assignments.A4;
public class Quicksort {
/** Number of comparisons for the sorting alg. */
public int comps = 0;
/**
* Sorts the provided list using quicksort.
* @param list the list to be sorted.
*/
public void sort(int[] list) {
if (list != null && list.length > 0) {
this.comps = 0;
quicksort(list, 0, list.length - 1);
}
}
/**
* This functions sorts the provided array (by reference) using quicksort algorithm.
* <p>
* It does this by while high is greater than low, it finds the partitioning index and then
* calls quicksort again on the lower half and upper half.
* </p>
* @param list the list to be sorted.
* @param low the lower bound of the list.
* @param high the upper bound of the list.
*/
private void quicksort(int[] list, int low, int high) {
if (low < high) {
this.comps++;
int pi = partition(list, low, high);
quicksort(list, low, pi - 1);
quicksort(list, pi + 1, high);
}
}
/**
* This function takes the last element as pivot, places the pivot element at its sorted position.
* @param values the array to be sorted.
* @param low the lower bound of the array.
* @param high the upper bound of the array.
* @return the partition index.
*/
private int partition(int[] values, int low, int high) {
int pivot = values[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (values[j] < pivot) {
i++;
swap(values, i, j);
}
this.comps++;
}
swap(values, i + 1, high);
return (i + 1);
}
/**
* Abstracts teh swap function to make the code easier to follow.
* @param values the array to be sorted.
* @param i the first index to be swapped.
* @param j the second index to be swapped.
*/
private void swap(int[] values, int i, int j) {
int temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}