Lab 1 Merge and Binary
//Sorts strings in alphabetical order
class MergeSort {
void merge(String arr[], int l, int m, int r)
{
// Find the sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
String[] L = new String[n1];
String[] R = new String[n2];
/* Copy data to temp arrays */
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray array
int k = l;
while (i < n1 && j < n2) {
if (L[i].compareTo(R[j]) > 0) {
arr[k] = R[j];
j++;
}
else {
arr[k] = L[i];
i++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void sort(String arr[], int l, int r)
{
if (l < r) {
// Find the middle point
int m = l + (r - l) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void print(String arr[])
{
for (String word : arr){
System.out.print(word + " ");
}
}
//tester method
public static void main(String args[])
{
String[] arr = new String[]{"Cow", "Dog", "Bird", "Sheep", "Goat", "Pig", "Quail", "Crow" };
System.out.println("Given Array:");
print(arr);
MergeSort ob = new MergeSort();
ob.sort(arr, 0, arr.length - 1);
System.out.println("\nSorted Array:");
print(arr);
}
}
MergeSort.main(null);
//Sorts strings in alphabetical order
class MergeSortOthers {
static void merge(Comparable arr[], int l, int m, int r)
{
// Find the sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
Comparable[] L = new Comparable[n1];
Comparable[] R = new Comparable[n2];
/* Copy data to temp arrays */
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray array
int k = l;
while (i < n1 && j < n2) {
if (L[i].compareTo(R[j]) > 0) {
arr[k] = R[j];
j++;
}
else {
arr[k] = L[i];
i++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
static void sort(Comparable arr[], int l, int r)
{
if (l < r) {
// Find the middle point
int m = l + (r - l) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void print(String arr[])
{
for (String word : arr){
System.out.print(word + " ");
}
}
//tester method
public static void main(String args[])
{
Integer[] arr = {1, 4, 2, 5, 3};
sort(arr, 0, (arr.length - 1));
System.out.println(Arrays.toString(arr));
}
}
MergeSortOthers.main(null);
class Binary{
public static int recursive(int[] arr, int key) {
return recursive(arr, key, 0, arr.length - 1);
}
private static int recursive(int[] arr, int key, int low, int high) {
if (low > high) {
return -1; // target not found
}
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid;
}
// search right half
else if (arr[mid] < key) {
return recursive(arr, key, mid + 1, high);
}
// search left half
else {
return recursive(arr, key, low, mid - 1);
}
}
//tester method
public static void main(String[] args){
int[] arr = new int[]{1, 3, 5, 7, 9, 23, 45, 67};
int result = recursive(arr, 45);
System.out.println("Number is at " + result + " index");
}
}
Binary.main(null);
class mergeAndSearch {
static void merge(int arr[], int l, int m, int r)
{
// Find the 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 < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarray array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= (R[j])) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
static void sort(int arr[], int l, int r)
{
if (l < r) {
// Find the middle point
int m = l + (r - l) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void print(int arr[])
{
for (int num : arr){
System.out.print(num + " ");
}
}
public static int recursive(int[] arr, int key) {
return recursive(arr, key, 0, arr.length - 1);
}
private static int recursive(int[] arr, int key, int low, int high) {
if (low > high) {
return -1; // target not found
}
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid;
}
// search right half
else if (arr[mid] < key) {
return recursive(arr, key, mid + 1, high);
}
// search left half
else {
return recursive(arr, key, low, mid - 1);
}
}
//tester method
public static void main(String args[])
{
int[] arr = {5, 6, 3, 1, 8, 9, 4, 7, 2};
System.out.println("Given Array:");
print(arr);
sort(arr, 0, arr.length - 1);
System.out.println("\nSorted Array:");
print(arr);
System.out.println(" ");
int result = recursive(arr, 7);
System.out.println("Number is at " + result + " index");
}
}
mergeAndSearch.main(null);