Phew, I have problem..How to sort number in my files..I have these in my input files...: I need to sort the line in array [1] from 12, 64, 8, 128 etc.
3 12
4 64
7 8
10 128
...
I just wanna sort and number out them :
1 8
2 12
3 64
....
And my work...(I have tried to use bubbleSort() that I copied from people's ..)
3 12
4 64
7 8
10 128
...
I just wanna sort and number out them :
1 8
2 12
3 64
....
And my work...(I have tried to use bubbleSort() that I copied from people's ..)
Code:
public int[] bubbleSort(int array[]) {
boolean swappedOnPrevRun = true;
while(swappedOnPrevRun)
{
swappedOnPrevRun = false;
for(int i = 0; i < array.length - 1; i++){
if(array[i] > array[i + 1])
{
swappedOnPrevRun = true;
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp; }
}
}
return array;
}
private static String[] readLines (String fileName, int m) throws IOException
{
List<String> lines = new ArrayList<String>();
String lineFRST = "";
Boolean readable = true;
BufferedReader in1 = new BufferedReader (new FileReader( m + "File.txt"));
while (readable)
{
lineFRST = in1.readLine();
if (lineFRST != null)
{
lines.add(lineFRST);
readable = true;
}
else
readable = false;
}
return lines.toArray(new String[lines.size()]);
}
public static void main(String[] args) throws IOException
{
PrintWriter printWriter = null;
BufferedWriter bufferWriter = null;
for (int m=1; m<=40; m++)
{
String[] lines = readLines (m + "Out.txt", m); //read all lines from your file
printWriter = new PrintWriter(new BufferedWriter(new FileWriter(m +"SORTED.txt", true)));
for(int i = 0; i < lines.length; i++)
{
String[] theline = lines[i].split(" ");
/*String[] num = theline[0];
String[] toSort = theline[1];*/
// need to convert string to int first....
printWriter.println( (i+1) + " : " + lines[i]);
}
\\ bubbleSort (); \\need to parse the int
printWriter.close();
}
}
Comment