Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chapter 22 23 24 25 answers #28

Merged
merged 2 commits into from
Dec 29, 2021
Merged
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
90 changes: 90 additions & 0 deletions ch_23/Exercise23_02.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package ch_23;

import java.util.Arrays;
import java.util.Comparator;

/**
* 23.2 (Generic merge sort) Write the following two generic methods using merge sort.
* The first method sorts the elements using the Comparable interface and the
* second uses the Comparator interface.
* public static <E extends Comparable<E>>
* void mergeSort(E[] list)
* public static <E> void mergeSort(E[] list,
* Comparator<? super E> comparator)
*/
public class Exercise23_02 {
public static void main(String[] args) {
Integer[] arr = new Integer[]{23, 25, 7, 14, 85, 65};
mergeSort(arr);
System.out.println(Arrays.toString(arr));

Integer[] arr2 = new Integer[]{3, 2, 7, 4, 8, 6, 1, 7};
mergeSort(arr2, Integer::compareTo);
System.out.println(Arrays.toString(arr2));

}

public static <E> void mergeSort(E[] list,
Comparator<? super E> comparator) {
if (list.length > 1) {
E[] firstHalf = Arrays.copyOf(list, list.length / 2);
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
mergeSort(firstHalf, comparator);

int secondHalfLength = list.length - list.length / 2;
E[] secondHalf = Arrays.copyOf(list, secondHalfLength);
System.arraycopy(list, list.length / 2,
secondHalf, 0, secondHalfLength);
mergeSort(secondHalf, comparator);

int current1, current2, current3;
current1 = current2 = current3 = 0;

while (current1 < firstHalf.length && current2 < secondHalf.length) {
if (comparator.compare(firstHalf[current1], secondHalf[current2]) < 0)
list[current3++] = firstHalf[current1++];
else
list[current3++] = secondHalf[current2++];
}

while (current1 < firstHalf.length)
list[current3++] = firstHalf[current1++];

while (current2 < secondHalf.length)
list[current3++] = secondHalf[current2++];
}
}


public static <E extends Comparable<E>> void mergeSort(E[] list) {
if (list.length > 1) {

E[] firstHalf = Arrays.copyOf(list, list.length / 2);
System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
mergeSort(firstHalf);

int secondHalfLength = list.length - list.length / 2;
E[] secondHalf = Arrays.copyOf(list, secondHalfLength);
System.arraycopy(list, list.length / 2,
secondHalf, 0, secondHalfLength);
mergeSort(secondHalf);

int current1, current2, current3;
current1 = current2 = current3 = 0;

while (current1 < firstHalf.length && current2 < secondHalf.length) {
if (firstHalf[current1].compareTo(secondHalf[current2]) < 0)
list[current3++] = firstHalf[current1++];
else
list[current3++] = secondHalf[current2++];
}

while (current1 < firstHalf.length)
list[current3++] = firstHalf[current1++];

while (current2 < secondHalf.length)
list[current3++] = secondHalf[current2++];
}
}

}
124 changes: 124 additions & 0 deletions ch_23/Exercise23_03.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package ch_23;

import java.util.Arrays;
import java.util.Comparator;

/**
* 23.3 (Generic quick sort) Write the following two generic methods using quick sort.
* The first method sorts the elements using the Comparable interface and the
* second uses the Comparator interface.
* public static <E extends Comparable<E>>
* void quickSort(E[] list)
* public static <E> void quickSort(E[] list,
* Comparator<? super E> comparator)
*/
public class Exercise23_03 {

public static void main(String[] args) {
Integer[] arr = new Integer[]{23, 25, 7, 14, 85, 65};
quickSort(arr);
System.out.println(Arrays.toString(arr));

Integer[] arr2 = new Integer[]{3, 2, 7, 4, 8, 6, 1, 7};
quickSort(arr2, Integer::compareTo);
System.out.println(Arrays.toString(arr2));

}

// Using Comparable
public static <E extends Comparable<E>> void quickSort(E[] list) {
quickSort(list, 0, list.length - 1);
}

public static <E extends Comparable<E>> void quickSort(E[] list, int first, int last) {
if (last > first) {
int pivotIndex = partition(list, first, last);
quickSort(list, first, pivotIndex - 1);
quickSort(list, pivotIndex + 1, last);
}
}

public static <E extends Comparable<E>> int partition(E[] list, int first, int last) {
E pivot = list[first]; // Choose the first element as the pivot
int low = first + 1; // Index for forward search
int high = last; // Index for backward search

while (high > low) {
// Search forward from left
while (low <= high && list[low].compareTo(pivot) <= 0) low++;

// Search backward from right
while (low <= high && list[high].compareTo(pivot) > 0) high--;

// Swap two elements in the list
if (high > low) {
E temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}

while (high > first && list[high].compareTo(pivot) >= 0)
high--;

// Swap pivot with list[high]
if (pivot.compareTo(list[high]) > 0) {
list[first] = list[high];
list[high] = pivot;
return high;
} else {
return first;
}
}

// Using Comparator
public static <E> void quickSort(E[] list,
Comparator<? super E> comparator) {
quickSort(list, 0, list.length - 1, comparator);


}

// Using Comparator
public static <E> void quickSort(E[] list, int first, int last,
Comparator<? super E> comparator) {
if (last > first) {
int pivotIndex;
// find pivot index
E pivot = list[first]; // Choose the first element as the pivot
int low = first + 1; // Index for forward search
int high = last; // Index for backward search

while (high > low) {
// Search forward from left
while (low <= high && comparator.compare(list[low], pivot) <= 0) low++;

// Search backward from right
while (low <= high && comparator.compare(list[high], pivot) > 0) high--;

// Swap two elements in the list
if (high > low) {
E temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}

while (high > first && comparator.compare(list[high], pivot) >= 0)
high--;

// Swap pivot with list[high]
if (comparator.compare(pivot, list[high]) > 0) {
list[first] = list[high];
list[high] = pivot;
pivotIndex = high;
} else {
pivotIndex = first;
}

quickSort(list, first, pivotIndex - 1, comparator);
quickSort(list, pivotIndex + 1, last, comparator);
}
}

}