Last active
November 10, 2017 03:38
-
-
Save ConorOBrien-Foxx/c39198bd2c1d46b236b08ef5d9364162 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class Main { | |
public static String convert(String name) { | |
String head = "" + name.charAt(0); | |
head = head.toUpperCase() + name.substring(1).toLowerCase(); | |
return head; | |
} | |
public static void swap(String[] array, int i, int j) { | |
String temp = array[i]; | |
array[i] = array[j]; | |
array[j] = temp; | |
} | |
public static int findPartition(String[] array, int start, int end) { | |
String pivotPoint = array[end]; | |
int i = start - 1; | |
for(int j = start; j < end; j++) { | |
if(array[j].compareTo(pivotPoint) < 0) { | |
i++; | |
swap(array, i, j); | |
} | |
} | |
i++; | |
if(array[end].compareTo(array[i]) < 0) { | |
swap(array, i, end); | |
} | |
return i; | |
} | |
public static void quicksort(String[] array, int start, int end) { | |
if(start >= end) | |
return; | |
int partitionIndex = findPartition(array, start, end); | |
quicksort(array, start, partitionIndex - 1); | |
quicksort(array, partitionIndex + 1, end); | |
} | |
public static void sort(String[] array) { | |
quicksort(array, 0, array.length - 1); | |
} | |
public static String[] readArray(Scanner scan) { | |
String[] temp = new String[10000]; | |
int count = 0; | |
String input = scan.nextLine(); | |
while(!input.toLowerCase().equals("end")) { | |
temp[count++] = convert(input); | |
input = scan.nextLine(); | |
} | |
String[] res = new String[count]; | |
for(int i = 0; i < count; i++) { | |
res[i] = temp[i]; | |
} | |
return res; | |
} | |
public static void printArray(String[] array) { | |
for(int i = 0; i < array.length; i++) { | |
System.out.print(array[i]); | |
if(i != array.length - 1) { | |
System.out.print(" "); | |
} | |
} | |
} | |
public static void printArrayln(String[] array) { | |
printArray(array); | |
System.out.println(); | |
} | |
public static boolean isSorted(String arr[]) { | |
for(int i = 1; i < arr.length; i++) { | |
if(arr[i - 1].compareTo(arr[i]) >= 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean arrayEqual(String[] a, String[] b) { | |
if(a.length != b.length) | |
return false; | |
for(int i = 0; i < a.length; i++) { | |
if(!a[i].equals(b[i])) | |
return false; | |
} | |
return true; | |
} | |
public static String[] concatArray(String[] a, String[] b) { | |
String[] res = new String[a.length + b.length]; | |
for(int i = 0; i < a.length; i++) { | |
res[i] = a[i]; | |
} | |
for(int j = a.length; j < res.length; j++) { | |
res[j] = b[j - a.length]; | |
} | |
return res; | |
} | |
public static void main(String args[]) { | |
Scanner scan = new Scanner(System.in); | |
System.out.println("Enter the values for the first array, up to 10000 values, enter 'End' to quit"); | |
String[] first = readArray(scan); | |
boolean firstIsSorted = isSorted(first); | |
System.out.println(); | |
System.out.println("Enter the values for the second array, up to 10000 values, enter 'End' to quit"); | |
String[] second = readArray(scan); | |
boolean secondIsSorted = isSorted(second); | |
System.out.println(); | |
sort(first); | |
sort(second); | |
System.out.println("First Array"); | |
printArrayln(first); | |
System.out.println(); | |
System.out.println("Second Array"); | |
printArrayln(second); | |
System.out.println(); | |
if(firstIsSorted && secondIsSorted) { | |
String[] concat = concatArray(first, second); | |
sort(concat); | |
System.out.println("Merged Array"); | |
printArrayln(concat); | |
} | |
else { | |
System.out.println("Error: Arrays not in correct order"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment