【问题标题】:Funky Bubble Sort (Java Eclipse)时髦的冒泡排序(Java Eclipse)
【发布时间】:2018-10-05 20:49:32
【问题描述】:

我目前正在研究一个基本的冒泡排序,除了它使用 Comparable,并且因为我不确定在哪里实现它的功能而让我失望。

这是我被赋予的,无法改变

public static <T extends Comparable<? super T>> void bubbleSort_Itr(T[] arr) { if(arr == null) throw new NullPointerException(); if(arr.length == 0) throw new IllegalArgumentException(); if(arr.length == 1) return; }

这是我在测试课中创建的

public class HubbaBubbaSort {

    public static <T extends Comparable<? super T>> void bubbleSort_Itr(T[] arr)
     {
        if(arr == null) throw new NullPointerException();
        if(arr.length == 0) throw new IllegalArgumentException();
        if(arr.length == 1) return;

        int n = arr.length; 
        for (int i = 0; i < n-1; i++) 
            for (int j = 0; j < n-i-1; j++) 
                if (arr[j] > arr[j+1]) 
                { 
                    // swap T temp and arr[i] 
                    T temp = arr[j]; 
                    arr[j] = arr[j+1]; 
                    arr[j+1] = temp; 
                    } 
     }
    /* Prints the array */
    void printArray(int arr[]) 
    { 
        int n = arr.length; 
        for (int i=0; i<n; ++i)
            System.out.print(arr[i] + " "); 
        System.out.println(); 
    } 

    // Driver method to test above 
    public static void main(String args[]) 
    { 
        HubbaBubbaSort ob = new HubbaBubbaSort(); 
        int arr[] = {64, 34, 25, 12, 22, 11, 90}; 
        ob.bubbleSort_Itr(arr); 
        System.out.println("Sorted array"); 
        ob.printArray(arr); 
    } 

}

【问题讨论】:

  • 您应该使用 Comparable 中定义的 int compareTo(T o) 方法,而不是使用 比较元素。该方法返回 -1 表示小于,0 表示等于,1 表示大于。
  • “我不确定在哪里实现它的功能”是什么意思?

标签: java sorting bubble-sort comparable


【解决方案1】:

这里的关键是Comparable接口:

https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

int compareTo(T o)

Parameters:
o - the object to be compared.

Returns:
a negative integer, zero, or a positive integer as this object is less than, 
equal to, or greater than the specified object.

一旦我们知道如何比较对象,我们就可以使用它来执行冒泡排序。此外,由于 int 是一个原始类型并且无法实现 Comparable,因此我将其切换为 Integers。

public class HubbaBubbaSort {

    public static <T extends Comparable<? super T>> void bubbleSort_Itr(T[] arr) {
        if (arr == null)
            throw new NullPointerException();
        if (arr.length == 0)
            throw new IllegalArgumentException();
        if (arr.length == 1)
            return;

        int n = arr.length;
        for (int i = 0; i < n - 1; i++)
            for (int j = 0; j < n - i - 1; j++)

                if (arr[j].compareTo(arr[j + 1])>0) {
                    // swap T temp and arr[i]
                    T temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
    }

    /* Prints the array */
    void printArray(int arr[]) {
        int n = arr.length;
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }

    // Driver method to test above
    public static void main(String args[]) {
        HubbaBubbaSort ob = new HubbaBubbaSort();
        Integer arr[] = { 64, 34, 25, 12, 22, 11, 90 };
        ob.bubbleSort_Itr(arr);
        System.out.println("Sorted array");
        System.out.println(Arrays.toString(arr));
    }
}

【讨论】:

  • 这很有帮助。谢谢!
猜你喜欢
  • 1970-01-01
  • 2014-06-25
  • 2017-06-17
  • 2013-09-04
  • 1970-01-01
  • 2011-06-25
  • 2018-08-19
  • 1970-01-01
  • 2019-01-07
相关资源
最近更新 更多