Selection sort - arrayLists
Basically, I have data of several people contained in a .csv file. This spreadsheet file contains the name of people in one column, followed by their age and work experience in two other columns.
So far I have correctly managed to display the data in a text area. However, I wish to be able to organise the people alphabetically by their name, or by their age in ascending order when the user clicks one of two buttons. For this example, I have attempted to limit the complexity by just trying to sort by age.
I have been told in order to do this, I should use a selection sort algorithm. Unfortunately, I have only used selection sort with arrays, not arrayLists, and not with data stored in a .csv file.
The problem that I have encountered is that I do not how to go through the data (for arrayLists), and then reassign the minimum position to the lowest number. Please refer to the minimumPosition method.
public class Inputs extends JFrame
{
ArrayList <People> pList = new ArrayList <People>();
JButton NameSortButton;
JTextArea DisplayTextArea;
String outputText = "";
public Inputs()
{
// Construct the GUI
class innerListener implements ActionListener
{
public void actionPerformed (ActionEvent myActionEvent)
{
if (myActionEvent.getSource() == AgeSortButton)
{
sortByAge();
}
}
}
ActionListener inListener = new innerListener();
NameSortButton.addActionListener(inListener);
}
public void readPeopleData()
{
FileReader reader = null;
int lineNumber = 1;
try
{
reader = new FileReader("People.csv");
Scanner in = new Scanner(reader);
while (in.hasNextLine())
{
String input = in.nextLine();
String section[] = input.split(",");
pList.add(new People(section[0], section[1], Integer.parseInt(section[2]), Integer.parseInt(section[3])));
lineNumber++;
}
for (People p: pList)
{
String heading = "Name \tAge \tWork Experience";
outputText = outputText + p.getPersonName() +
"\t" + p.getAge() 开发者_开发知识库 +
"\t" + p.getExperience();
DisplayTextArea.setText(heading + outputText);
}
}
catch(IOException error)
{
JOptionPane.showMessageDialog(null, "File not Found","Error" , JOptionPane.ERROR_MESSAGE );
}
} // public void readPatientData()
private void sortByAge()
{
for (int i = 0; i < pList.size(); i++)
{
int minPos = minimumPosition(i);
swap(minPos, i);
}
displayOutputs();
}
private int minimumPosition(int from)
{
int minPos = from;
for (int i = from + 1; i < pList.size(); i++)
{
// ****** PROBLEM AREA ****** //
// if (pList.get(i).getAge() < // NO IDEA)
{
minPos = i;
}
/*
The text book sorted arrays by:
for (int i = from + 1; i < array.length; i ++)
if (array[i] < array[minPos])
{
minPos = i;
}
*/
}
return minPos;
}
private void swap(int i, int j)
{
People temp = pList.get(i);
pList.set(i, pList.get(j));
pList.set(j, temp);
}
private void displayOutputs ()
{
for(int j = 0; j < pList.size(); j++)
{
outputText = outputText + pList.get(j).getPersonName() +
"\t" + pList.get(j).getAge() +
"\t" + pList.get(j).getExperience() +
DisplayTextArea.setText(outputText);
System.out.println(outputText);
}
}
public static void main (String args[])
{
} // public static void main (String args[])
}
Can somebody please point me in the right direction? How can I compare the sorted data to the unsorted data. Furthermore, are my for loops correct?
Thank you in advance.
The whole program posted is not a good way to ask a question.
Look at what the textbook is doing. It's looping through the array, comparing each one with the "Array[minPos]" item. If the new element is actually smaller, it becomes the new minPos
. At the end, we know it's the smallest element, and shove it at the front of the list.
In your case, you're using an ArrayList, which will give you its stored object (a People
object) if you call get(index)
, which can then tell you its age
or name
, hopefully. (You didn't show us this People
class, so we don't know what its accessors look like.) And you can compare these. To determine which is the smallest.
Basically you could treat ArrayList
like an array, since it is a list backed by an array.
You're already on a good way with you implementation.
Instead of this:
for (int i = from + 1; i < array.length; i ++) {
if (array[i] < array[minPos]) {
minPos = i;
}
}
You'd replace the array access with list operations:
for (int i = from + 1; i < pList.size(); i ++) {
if ( pList.get(i).getAge() < pList.get(minPos ).getAge() ) {
minPos = i;
}
}
精彩评论