一个关于字串的排序算法~~

有如下一些数据:
1-2-3-4-5
2-3-2
2-4-4-2
1-6
保存在一个文本文档中(ANSI编码格式)。要求排序成如下结果:

1-6
1-2-3-4-5
2-4-4-2
2-3-2

即以每一行的最后一个元素进行排序,如果相同再以之前一个排序。都是降序。。

有兴趣的可以试试。

package com.watersoft.file;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Sort {
private int lineCount;
private int numberCount;
private String path;
private int[][] matrix;

/**
* *
*
* @param args
* contains the path of a text file which include the data being
* sorted
*/
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("Specific a file please!");
return;
}
System.out.println(">>> Information: sort start ..\n");
Sort s = new Sort();
s.setPath(args[0]);
s.doSort();
s.display();
System.out.println("\n>>> Information: sort end ..");
}

/** * do sort: initialize, parse, then sort. */
public void doSort() {
init();
parse();
for (int index = 0; index < this.lineCount - 1; index++)
for (int innerIndex = 0; innerIndex < this.lineCount - 1; innerIndex++)
sort(innerIndex, this.numberCount - 1);
}

/**
* * sort the given data according to the given row index and column index *
*
* @param rowIndex
* current row *
* @param colIndex
* current column
*/
private void sort(int rowIndex, int colIndex) {
int aInt = this.matrix[rowIndex][colIndex];
int bInt = this.matrix[rowIndex + 1][colIndex];
int temp = 0;
if (aInt < bInt) { /* * swap two rows */
for (int index = 0; index < this.numberCount; index++) {
temp = this.matrix[rowIndex][index];
this.matrix[rowIndex][index] = this.matrix[rowIndex + 1][index];
this.matrix[rowIndex + 1][index] = temp;
}
} else if (aInt == bInt) { /* * compare previous elements of two rows */
colIndex--;
sort(rowIndex, colIndex);
}
}

/**
* * read file to make sure the dimension of matrix *
*
* @return boolean value, true if successfully done, else false
*/
private boolean init() {
try {
FileReader fr;
fr = new FileReader(this.path);
BufferedReader br = new BufferedReader(fr);
String tempString;
int lc = 0;
int nc = 0;
while ((tempString = br.readLine()) != null) {
lc++;
nc = (nc < tempString.split("-").length) ? tempString
.split("-").length : nc;
}
this.lineCount = lc;
this.numberCount = nc;
} catch (FileNotFoundException e) {
System.err.println("Error: cannot find file:[" + this.path + "]");
return false;
} catch (IOException e) {
System.err.println("Error: cannot read file..");
return false;
}
return true;
}

private boolean parse() { /* * initialize matrix */
this.matrix = new int[this.lineCount][this.numberCount];
for (int index = 0; index < this.lineCount; index++)
for (int innerIndex = 0; innerIndex < this.numberCount; innerIndex++)
this.matrix[index][innerIndex] = Integer.MAX_VALUE;
try {
FileReader fr;
fr = new FileReader(this.path);
BufferedReader br = new BufferedReader(fr);
String tempString;
String[] tempArray;
int lc = 0;
int baseIndex = 0;
while ((tempString = br.readLine()) != null) {
tempArray = tempString.split("-");
baseIndex = this.numberCount - tempArray.length;
for (int index = baseIndex; index < this.numberCount; index++)
this.matrix[lc][index] = Integer.parseInt(tempArray[index
- baseIndex]);
lc++;
}
} catch (FileNotFoundException e) {
System.err.println("Error: cannot find file:[" + this.path + "]");
return false;
} catch (IOException e) {
System.err.println("Error: cannot read file..");
return false;
}
return true;
}

/** * display sorted result */
public void display() {
for (int index = 0; index < this.lineCount; index++) {
for (int innerIndex = 0; innerIndex < this.numberCount; innerIndex++) {
if (this.matrix[index][innerIndex] != Integer.MAX_VALUE)
if (innerIndex + 1 != this.numberCount)
System.out.print(this.matrix[index][innerIndex] + "-");
else
System.out.print(this.matrix[index][innerIndex]);
}
System.out.println();
}
}

public void setPath(String path) {
this.path = path;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值