最近对集合排序迷糊,希望朋友解答,我好学好一个最优化的排序即可。不知道我的想法可取不?还是特殊情况特殊对待?

解决方案 »

  1.   

    集合不是有一个sort();方法吗。。
      

  2.   

    用冒泡吧:
    public void sort(int[] data){ 
       int temp;
       for(int i=0;i<data.length;i++){ //冒泡,降序:
        for(int j=data.length-1;j>i;j--){
         if(data[i]<data[j]){ //每个数都比较n次,如果data[i]>data[j]成立,则交换两个数
            temp =data[i];
          data[i] = data[j];
          data[j] = temp;
         }
        } 
      }
    }
    没记错的话,Collections.sort(List<T> list)是用冒泡实现的
      

  3.   

    晕,误人子弟啊,冒泡效率太差了,n2啊,java的sort是改进的快速排序。
      

  4.   

    6种主要排序方式 /**
     * 插入排序法 ---简单插入排序排序 整体思路--后面得元素向前搜索 其插入位置
     */
    static void InsertSort(int[] sqList, boolean is_smallTbig) { // is_smallTbig排练方式 //不放入for中是因为考虑算法运行速度
    int sentinel = 0;// 哨兵
    int j, i;
    if (is_smallTbig) {
    for (i = 1; i < sqList.length; i++) {
    sentinel = sqList[i];
    for (j = i - 1; j >= 0; j--) {
    if (sqList[j] > sentinel) // 小到大顺序
    sqList[j + 1] = sqList[j];// 比sentinel大则向后移动 直到空出j的位置
    else {
    break;
    }
    }
    sqList[j + 1] = sentinel; // 把sentinel 放到 空出的j的位置 因为for出来j-1了
    // 所以j+1
    }
    } else {
    for (i = 1; i < sqList.length; i++) {
    sentinel = sqList[i];
    for (j = i - 1; j >= 0; j--) {
    if (sqList[j] < sentinel) // 大到小顺序
    sqList[j + 1] = sqList[j];// 比sentinel大则向后移动 直到空出j的位置
    else {
    break;
    }
    }
    sqList[j + 1] = sentinel; // 把sentinel 放到 空出的j的位置 因为for出来j-1了
    // 所以j+1
    }
    } } /**
     * 插入排序法 ---shell(希尔)排序(不稳定)
     */
    static void ShellSort(int[] sqList, boolean is_smallTbig) {
    // 使数组部分有序 整体上 大(小)的在后面小(大)的在前面 时间复杂度为O(n^(3/2))
    int[] step = { 9, 5, 3, 1 };// 最后一位必须为1
    int s, t;
    int sentinel = 0;// 哨兵
    if (is_smallTbig) {
    for (int i = 0; i < step.length; i++) {
    s = step[i]; // 取步长
    for (int j = s; j < sqList.length; j++) {
    // 分组进行排序 组的步长就是S
    sentinel = sqList[j];
    t = j - s;
    // 对步长为s的组进行排序 使之有序
    for (; t >= 0 && (t <= sqList.length); t = t - s) {
    if (sentinel < sqList[t])// 小到大排序
    {
    sqList[t + s] = sqList[t];
    } else {
    break;
    }
    }
    sqList[t + s] = sentinel;
    }
    }
    } else {
    for (int i = 0; i < step.length; i++) {
    s = step[i]; // 取步长
    for (int j = s; j < sqList.length; j++) {
    // 分组进行排序 组的步长就是S
    sentinel = sqList[j];
    t = j - s;
    // 对步长为s的组进行排序 使之有序
    for (; t >= 0 && (t <= sqList.length); t = t - s) {
    if (sentinel > sqList[t]) // 大到小排序
    {
    sqList[t + s] = sqList[t];
    } else {
    break;
    }
    }
    sqList[t + s] = sentinel;
    }
    }
    } } /**
     * 交换排序法-----冒泡排序 整体思路(已两两交换) 先找出数组中最大(小)的元素放在其位置
     */
    static void BubbleSort(int[] sqList, boolean is_smallTbig) {
    int sentinel = 0;
    int i = sqList.length, j = 0;
    if (is_smallTbig) {
    for (boolean change = true; i > 0 && change; i--) {
    change = false;// 如何已经有序则不用拍
    for (j = 1; j < i; j++) {
    sentinel = sqList[j];
    if (sentinel < sqList[j - 1]) {
    change = true;
    sqList[j] = sqList[j - 1];
    sqList[j - 1] = sentinel;
    } }
    }
    } else {
    for (boolean change = true; i > 0 && change; i--) {
    change = false;// 如何已经有序则不用拍
    for (j = 1; j < i; j++) {
    sentinel = sqList[j];
    if (sentinel > sqList[j - 1]) {
    sqList[j] = sqList[j - 1];
    sqList[j - 1] = sentinel;
    change = true;
    } }
    }
    } } /**
     * 交换排序法----- 快速排序 没有检测如入的合法性(不稳定)
     */
    int myQuicksort(int a[], int indexI, int indexJ) {
    int tempindex, start = indexI, end = indexJ, res;
    tempindex = indexI;
    if (indexI < 0 || indexJ < 0) {
    res = 0;
    }
    if (indexI >= indexJ) {
    res = 0;
    } else {
    boolean swich = true;
    while (true) {
    if (swich) {
    if (a[indexJ] < a[tempindex]) {
    int t;
    t = a[indexJ];
    a[indexJ] = a[tempindex];
    a[tempindex] = t;
    tempindex = indexJ;
    swich = false;
    } else {
    indexJ--;
    } } else {
    if (a[indexI] > a[tempindex]) {
    int t;
    t = a[indexI];
    a[indexI] = a[tempindex];
    a[tempindex] = t;
    tempindex = indexI;
    swich = true;
    } else {
    indexI++;
    }
    }
    if (indexJ <= indexI)
    break;
    }
    res = myQuicksort(a, start, tempindex - 1);
    res = myQuicksort(a, tempindex + 1, end);
    }
    return res;
    } /**
     * 选择排序法-----简单选择排序(不稳定)
     */
    static void selectSort(int[] sqList, boolean is_smallTbig) {
    int sentinel = 0, tempIndex = 0;
    if (is_smallTbig) {
    for (int i = 0; i < sqList.length; i++) {
    tempIndex = i;
    sentinel = sqList[i];
    for (int j = i + 1; j < sqList.length; j++) {
    sentinel = (sentinel < sqList[j]) ? sentinel
    : sqList[tempIndex = j];// 记录最小值的index
    }
    sqList[tempIndex] = sqList[i];
    sqList[i] = sentinel;
    }
    } else {
    for (int i = 0; i < sqList.length; i++) {
    sentinel = sqList[i];
    tempIndex = i;
    for (int j = i + 1; j < sqList.length; j++) {
    sentinel = (sentinel > sqList[j]) ? sentinel
    : sqList[tempIndex = j];
    }
    sqList[tempIndex] = sqList[i];
    sqList[i] = sentinel;
    }
    } } /**
     * 选择排序法-----堆排(不稳定)
     */
    public class HeapSort {
    public void sort(int[] number) {
    int[] tmp = new int[number.length + 1]; // 配合說明,使用一個有徧移的暫存陣列
    for (int i = 1; i < tmp.length; i++) {
    tmp[i] = number[i - 1];
    }
    createHeap(tmp);
    int m = number.length;
    while (m > 1) {
    swap(tmp, 1, m);
    m--;
    int p = 1;
    int s = 2 * p;
    while (s <= m) {
    if (s < m && tmp[s + 1] < tmp[s])
    s++;
    if (tmp[p] <= tmp[s])
    break;
    swap(tmp, p, s);
    p = s;
    s = 2 * p;
    }
    } // 這邊將排序好的暫存陣列設定回原陣列
    for (int i = 0; i < number.length; i++) {
    number[i] = tmp[i + 1];
    }
    } private void createHeap(int[] tmp) {
    int[] heap = new int[tmp.length];
    for (int i = 0; i < heap.length; i++)
    heap[i] = -1;
    for (int i = 1; i < heap.length; i++) {
    heap[i] = tmp[i];
    int s = i;
    int p = i / 2;
    while (s >= 2 && heap[p] > heap[s]) {
    swap(heap, p, s);
    s = p;
    p = s / 2;
    }
    }
    for (int i = 1; i < tmp.length; i++)
    tmp[i] = heap[i];
    } private void swap(int[] number, int i, int j) {
    int t;
    t = number[i];
    number[i] = number[j];
    number[j] = t;
    }
    }
      

  5.   

      还是我来帮你解答一下吧,虽然你没有把你自己的需求说明白,但是我懂你的意思:
    实现对对象的排序: 
    这个很简单的:正如前面所说会用到Arrays.sort()方面,但是参数是Arrays.sort(object[]),请参考api文档:public static void sort(Object[] a)根据元素的自然顺序对指定对象数组按升序进行排序。数组中的所有元素都必须实现 Comparable 接口。此外,数组中的所有元素都必须是可相互比较的(也就是说,对于数组中的任何 e1 和 e2 元素而言,e1.compareTo(e2) 不得抛出 ClassCastException)。
    保证此排序是稳定的:不会因调用 sort 方法而对相等的元素进行重新排序。该排序算法是一个经过修改的合并排序算法(其中,如果低子列表中的最高元素小于高子列表中的最低元素,则忽略合并)。此算法提供可保证的 n*log(n) 性能。 
    参数:
    a - 要排序的数组 eg:请楼主看清楚了,我实现的需求是:对一个学生对象的排序,按照成绩排序,要是成绩相等就按照年龄排序:
    package api.com;import java.util.Arrays;class Student implements Comparable<Student>{
    private String name;
    private int age;
    private float score;
    public Student(String name, int age, float score) {
    super();
    this.name = name;
    this.age = age;
    this.score = score;
    }
    public String toString(){
    return this.name + "\t\t" + this.age + "\t\t" + this.score;
    }
    @Override
    public int compareTo(Student stu) {
    if(this.score>stu.score){
    return -1;
    }else if(this.score<stu.score){
    return 1;
    }else{
    if(this.age>stu.age){
    return 1;
    }else if(this.age < stu.age){
    return -1;
    }else{
    return 0;
    }
    }
    }
    }public class TestComparable {
    public static void main(String[] args) {
    Student stu[] = {new Student("张三",20,50),new Student("李四",50,90),
    new Student("王五",40,45),new Student("麻六",35,80)};
    Arrays.sort(stu);

    System.out.println(Arrays.toString(stu));
    }
    };
      

  6.   


      java有那么多工具,还用得着自己来写这些见到你的排序方法吗