当前位置:编程学习 > JAVA >>

MP3歌曲排序算法问题

我用quickSort 已经可以成功按照歌曲时间排序,请问如果遇到了时间相同的歌曲如何按照歌曲名排列呢?(如果歌曲名称还相同的话就按照演唱者排列)
这是算法部分的代码:

public static int partition(Song array[], int left, int right)
{
      int i = left, j = right;
      Song tmp;
      int pivot = array[(left + right) / 2].time;
     
      while (i <= j) {
            while (array[i].time < pivot)
                  i++;
            while (array[j].time > pivot)
                  j--;
            if (i <= j) {
                  tmp = array[i];
                  array[i] = array[j];
                  array[j] = tmp;
                  i++;
                  j--;
            }
      };
     
      return i;
}
 
public static void quickSort(Song array[], int left, int right) {
      int index = partition(array, left, right);
      if (left < index - 1)
            quickSort(array, left, index - 1);
      if (index < right)
            quickSort(array, index, right);
}

希望有高手提供一个可行而且能够保证运算速度的方法,谢谢 --------------------编程问答-------------------- 我觉得吧,最好 能在排序的时候就 记录已排好序的时间重复的歌曲的序号,
比如 1,2,2,2,9,4,7,5
       ------
       已排好序,在发现第四个值‘9’已经不是重复值得时候,对以重复的歌曲进行歌曲名排序,排好之后,继续按照时间往下排序
感觉用插入排序比较好 --------------------编程问答-------------------- 引用 《算法导论》
8.3 基数排序 
“我们可以采用另一种方法,即用一种稳定的排序方法对所给信息进行三次排序:先以日,其次对月,再对年”

--------------------编程问答-------------------- java.util.Collections.sort(); --------------------编程问答-------------------- 大家有人用过java调用extmail的userctl.pl脚本 往extmail中添加用户吗,我自己写了代码执行了 但是不会向extmail中插入数据.我的代码如下
String comm = "perl /var/www/extsuite/extman/tools/userctl.pl--mod=add -username="+uname +"-password="+pass;
runLinuxCmd(String cmd) //本类调用

public String runLinuxCmd(String cmd) {
BufferedReader bf = null;
try {
Process process = Runtime.getRuntime().exec(cmd);
bf = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
String resutl="";
while ((line = bf.readLine()) != null) {
resutl =resutl+ line.trim()+"\n";
}
System.out.println("---------------try result------------------"+resutl);
return resutl;
} catch (java.io.IOException e) {
e.printStackTrace();
System.out.println("------------error--------------");
return null;
} finally {
if (bf != null) {
try {
bf.close();
bf = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

单独在shell环境下执行perl命令是没有问题,能够插入数据。 我在网上看到有人讲过把perl命令写进sh脚本就可以了,我也不知道这是为什么按理说java能够调用shell命令也能调用perl脚本命令。非要包装一下的话我也写了一个shell脚本单独运行shell脚本也能添加数据就是java调用又没有添加数据了 我想要的是验证过的结果 大道理就不用讲了直接来真工夫啊 我写的shell脚本代码如下:

#!/bin/sh
perl /var/www/extsuite/extman/tools/userctl.pl--mod=add -username="$1" -password="$2";

这个做了两天都没有做好 如果能有满意的结果我会加分的谢谢大家 欢迎大家来这里回帖

http://topic.csdn.net/u/20110821/20/ae1e4815-1c81-4c75-88ae-db263128f963.html?61772
--------------------编程问答-------------------- google提供的 guava里有个 
引用
public abstract class ComparisonChain
extends Object

A utility for performing a "lazy" chained comparison statement, which performs comparisons only until it finds a nonzero result. For example:

   public int compareTo(Foo that) {
     return ComparisonChain.start()
         .compare(this.aString, that.aString)
         .compare(this.anInt, that.anInt)
         .compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
         .result();
   }

The value of this expression will have the same sign as the first nonzero comparison result in the chain, or will be zero if every comparison result was zero.

Once any comparison returns a nonzero value, remaining comparisons are "short-circuited". 
--------------------编程问答-------------------- 每首歌曲一个对象,然后用collections.sort();自己定义comparator,这样就很好办了。
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,