Sorting Out the Stringy Mess that is Java
Without Becoming Dis-Arrayed
By: Danny Khodabandeh
Purpose: to create a java application capable of sorting a list of strings input by a user. This will be the combination of two classes, one to construct a string sorter, and of course the other to test it.
Sorting in General:
The idea of taking elements with measurable aspects and putting them into an order descending from the greatest to the least or ascending vice versa has been . part of human the experience since pre-historic times. The process of sorting has been an integral part of human logic that it's very difficult to find any historical data on the process as whole.
Sorting even today can be easily done in a spreadsheet with just the click of the mouse, but the problem of getting a computer to perform this task is a little more daunting that it may seem.
Process:
Step 1: PseudoCode it
Step 2 - Construction?
/* StringSorter.java
* Danny Khodabandeh
* APCSA 2nd Hour
*
* D.A. Young
*
* I certify that I have neither given nor recieved unauthorized assistance on this test or assignment
*/
public class StringSorter{
public StringSorter(String[] anArray)
{
a = anArray;
}
/**
Sorts my array managed by my string sorter
*/
public void sort()
{
for(int i = 0; i < a.length - 1; i++){
int minPos = minimumPosition(i);
swap(minPos, i);
}
}
/** This finds the smallest element in a tail range of the array.
by comparing the strings lexographically
there is a natural ordering between all elements in the array.
*/
private int minimumPosition(int from)
{
int minPos = from;
for(int i = from + 1; i < a.length; i++)
if( a[i].compareTo(a[minPos]) < 0) minPos = i;
return minPos;
}
/** This actually swaps two entries of the array allowing it to be properly ordered */
private void swap(int i, int j)
{
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private String[] a;
}
And now that I have my class StringSorter done, I can test it:
/* Danny Khodabandeh
* Sort2.java
* APCSA 2nd Hour
* D.A. Young
* I Certify that i have neither given nor recieved unauthorized assistance on this assignment
*/
import javax.swing.JOptionPane;
public class Sort2{
public static void main (String args[])
{
String temp = JOptionPane.showInputDialog("How many strings will you input for this exercise?");
int elements = Integer.parseInt(temp);
String[] array = new String[elements];
/* When working with arrays the best way to fill each field of the array
* is to use a simple for loop for each slot of the array.
* Also you have to be very careful that your program knows not to try and use an array
* with a marker value higher than the bounds of it.
* Otherwise you'll get a nasty little run-time error message....
*And your program won't work.
*/
System.out.println("Initializing Program - Begin Data Below");
System.out.println("-------------------------------------------------");
for(int i = 0; i < elements; i++){ // <--- see? it's not <= or you'd have a bad error --------//
//
array[i] = JOptionPane.showInputDialog("Enter element number " + i + ":"); //
//
System.out.println(array[i]); //
//
} //
System.out.println("-------------------------------------------------"); //
System.out.println(""); //
System.out.println(""); //
System.out.println(""); //
System.out.println("Beginning Sort Routine - Final Outcome Below"); //
System.out.println("-------------------------------------------------"); //
//
//
//
//
StringSorter mySort = new StringSorter(array); //
mySort.sort(); //
//
for(int i = 0; i < elements; i++){ //<-- again -----------------------------------------------
System.out.println(array[i]);
}
System.out.println("-------------------------------------------------");
System.out.println("End Program...");
}
}
mmm......................
Smell that Java Brewing
Outcome:
See for Yourself --
Related Links: