PROOF READ: Sort text file 3 different ways and compare
If you wouldn't mind could I have someone please proof read my code and give me pointers as to how I can complete my program, I am stuck as of right now. The purpose of my program is to basically utilize three sorting algorithms, bubblesort, insertion sort and selection sort. I am to sort a text file in ascending order. For example, apple should come before banana and banana before orange. Here is what I have so far.
Code:
public interface InterfaceStringArray
{
public void initialize(int arrSize);
//initializes an array of size=arrSize
public int getSize();
//returns the current array size
public void insert(String newItem);
//inserts newItem in the next available spot
public boolean delete(String deleteItem);
//returns true if all occurrences of deleteItem are deleted,
//false if no matching items found
public int search(String searchItem);
//returns the index of the first matching item, -1 if no match
public boolean setItem(int index, String itemValue);
//returns true if successful, false otherwise
public String getItem(int index);
//returns the item at the requested index
public boolean resize(int newSize);
//returns true if successful (widening), false if narrowing
public void swap(int one, int two);
//swaps strings
public void bubbleSort();
//sorts the array strings using Bubble Sort Algorithm (ascending)
public void selectionSort();
//sorts the array strings using Selection Sort Algorithm (ascending)
public void insertionSort();
//sorts the array strings using Insertion Sort Algorithm (ascending)
public String getStringArray();
//returns a String of all the array strings separated by a white space
}
The 4 methods that are currently blank I am unsure of, any pointers or peer edits that would help me see what statements would be used here would be greatly appreciated.
Code:
public class StringArray implements InterfaceStringArray
{
private String a[]; //reference to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public void initialize(int arrSize)
{
a = new String[arrSize]; //initializes an array of size=arrSize
nElems = 0; //sets the number of data items to 0
}
//--------------------------------------------------------------
public int getSize()
{
{ return nElems; } //returns the current array size
}
//--------------------------------------------------------------
public void insert(String newItem)
{
a[nElems] = newItem;
nElems++; //inserts newItem in the next available spot
}
//--------------------------------------------------------------
public boolean delete(String deleteItem)
{
int j;
for (j=0; j<nElems; j++)
if( deleteItem == a[j] )
break;
if(j==nElems)
return false; //false if no matching items found
else
{
for(int k=j; k<nElems; k++)
a[k] = a[k+1];
nElems--;
return true; //returns true if all occurrences of deleteItem are deleted
}
}//end delete()
//--------------------------------------------------------------
public int search(String searchItem)
{
int j;
for(j=0; j<nElems; j++)
if(a[j] == searchItem)
break;
if(j == nElems)
return -1;
else
return j;
}
//--------------------------------------------------------------
public boolean setItem(int index, String itemValue)
{
//returns true if successful, false otherwise
}
//--------------------------------------------------------------
public String getItem(int index)
{
//returns the item at the requested index
}
//--------------------------------------------------------------
public boolean resize(int newSize)
{
}
//--------------------------------------------------------------
public void swap(int one, int two)
{
String temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//--------------------------------------------------------------
public void bubbleSort()
{
int out, in;
for(out=nElems-1; out>1; out--) // outer loop (backward)
for(in=0; in<out; in++) // inner loop (forward)
if(a[in].compareTo(a[in+1]) < 0) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
//--------------------------------------------------------------
public void selectionSort()
{
int out, in, min;
for(out=0; out<nElems-1; out++) // outer loop
{
min = out; // minimum
for(in=out+1; in<nElems; in++) // inner loop
if(a[in].compareTo(a[in+1]) < 0 ) // if min greater,
min = in; // we have a new min
swap(out, min); // swap them
} // end for(out)
} // end selectionSort()
//--------------------------------------------------------------
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
String temp = a[out]; // remove marked item
in = out; // start shifts at out
while(a[in].compareTo(a[in+1]) < 0) // until one is smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
//--------------------------------------------------------------
public String getStringArray()
{
//returns a String of all the array strings separated by a white space
}
}
This is my driver .java file. Which will include the main method. Not sure of the entire structure of this .java file. I'm thinking I will need to use a bufferreader to read the .txt file. Then I should I create 3 tokenizers or is only one neccessary? I think I need to create 3 objects, and then sort each object using a different sorting algorithm. Once each has been sorted, I must compare all three then write a final method which says "gj they all match" which I will have no problem with.
Code:
public class StringArrayApp {
}