【问题标题】:HeapSort to sort array of Strings in JavaHeapSort 对 Java 中的字符串数组进行排序
【发布时间】:2022-06-15 08:08:54
【问题描述】:

我想创建一个 HeapSort 变体,使我能够按字典顺序对字符串数组进行排序。有人可以解释为什么这只适用于前 6 个元素和后 5 个元素吗?我尝试了其他输入并得到了类似的结果,只要数组最多有 11 个元素它可以工作,否则只有前 6 个和最后 5 个工作。我真的不明白为什么会发生这种情况,希望能得到帮助。

public class HeapSortFix {

    static int x = -1;
    static String []heap = new String[1000];

    static void heapForm(String k){
        String tmp;

        x++;
        heap[x] = k;
        int child = x;
        int index = x / 2;

        while (index >= 0) {
            if (heap[index].compareTo(heap[child]) > 0){    // Just swapping if the element is smaller than already stored element

                tmp = heap[index];                          // Swapping the current index with its child
                heap[index] = heap[child];
                heap[child] = tmp;
                child = index;

                index = index / 2;                          // Moving upward in the heap
            }
            else
            {
                break;
            }
        }
    }


    static void heapSort(){
        int left1, right1;

        while (x >= 0){
            String k;
            k = heap[0];
            System.out.print(k + " ");
            heap[0] = heap[x];
            x = x - 1;
            String tmp;
            int index = 0;
            int length = x;
            left1 = 1;
            right1 = left1 + 1;
            while (left1 <= length) {
                if (heap[index].compareTo(heap[left1]) <= 0 &&
                        heap[index].compareTo(heap[right1]) <= 0){
                    break;
                }

                else{
                    if (heap[left1].compareTo(heap[right1])< 0){
                        tmp = heap[index];
                        heap[index] = heap[left1];
                        heap[left1] = tmp;
                        index = left1;
                    }

                    else{
                        tmp = heap[index];
                        heap[index] = heap[right1];
                        heap[right1] = tmp;
                        index = right1;
                    }
                }
                left1 = 2 * left1;
                right1 = left1 + 1;
            }
        }
    }


    static void sort(String k[], int n){
        for (int i = 0; i < n; i++){
            heapForm(k[i]);
        }
        heapSort();
    }
    
    public static void main(String[] args) {
        String array[] = {"n","f","x","a","q","w","e","m","i","y","t","c","l","r","u","h","p","z","b","k","g","d","j","o","s","v" };
        int n = array.length;
        sort(array, n);
    }
}

这将是运行 main 时的输出: a b c d e f i k o p q m s r n j g t u h l v w x y z

【问题讨论】:

    标签: java string heapsort


    【解决方案1】:

    你的代码太混乱了,我无法理解,但如果我没听错的话, 要按字母顺序对字母进行排序,您可以循环字母数组并按 .compare(cell1,cell2) 每两个单元格进行冒泡排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-02
      • 2021-03-21
      • 2017-06-07
      • 1970-01-01
      • 2012-10-14
      • 2014-11-14
      相关资源
      最近更新 更多