浏览 3568 次
锁定老帖子 主题:一个关于字串的排序算法~~
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-03-19
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; } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-08-04
最后修改:2009-08-04
package test; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class StringSort { public static void main(String[] args){ StringSort ss = new StringSort(); //you'd better judge the null List<String> number = ss.sortString(ss.readString()); ss.printResult(number); } //read string from file private List<String> readString(){ List<String> initString = new ArrayList<String>(); try { BufferedReader readFile = new BufferedReader(new FileReader("C://string.txt")); String tempstring; while(null != (tempstring = readFile.readLine())){ initString.add(ConvertString(tempstring)); } return initString; }catch (Exception e) { e.printStackTrace(); return null; } } // reverse and delete '-' from string private String ConvertString(String number){ String[] tempSting = number.split("-"); List<String> tempList = new ArrayList<String>(); for(int i=tempSting.length-1;i>=0;i--){ tempList.add(tempSting[i]); } return tempList.toString(); } // sort the List private List<String> sortString(List<String> list){ List<String> result = new ArrayList<String>(); //sort Collections.sort(list); //reverse Collections.reverse(list); // result collection for(int i=0;i<list.size();i++){ String temp = list.get(i).replaceAll("\\D", ""); StringBuffer sb= new StringBuffer(); for(int index = temp.length()-1; index>=0;index--){ sb.append(temp.charAt(index)); sb.append(0 == index ?"":"-"); } result.add(sb.toString()); } return result; } //print the result private void printResult(List<String> number){ Iterator i = number.iterator(); while(i.hasNext()){ System.out.println(i.next()); } } }
|
|
返回顶楼 | |
发表时间:2009-09-04
我觉得有一个最好的办法:
思路:1.先将每个字符串赋值给一个String; 、 2.讲String集体给byte[] 4.根据byte的最后一个字符的值来比较;这样很方便; |
|
返回顶楼 | |
发表时间:2009-12-15
我想到一个方法不知道可以不?
把数字补齐,然后反方向变成long,比较大小,按照比较后的顺序把原文排序。 说明: 1-2-3-4-5 2-3-2 2-4-4-2 1-6 补齐: 1-2-3-4-5① 0-0-2-3-2② 0-2-4-4-2③ 0-0-0-1-6④ 反向变成long: 54321 23200 24420 61000 排序: 61000④ 54321① 24420③ 23200② |
|
返回顶楼 | |