首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >时髦的气泡排序(Java Eclipse)

时髦的气泡排序(Java Eclipse)
EN

Stack Overflow用户
提问于 2018-10-06 04:49:33
回答 1查看 147关注 0票数 1

我目前正在做一个基本的冒泡排序,但它使用的是可比较的,并且因为我不确定在哪里实现它的功能而放弃了我。

这是我得到的信息,无法更改

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; }

这是我在我的测试类中创建的

代码语言:javascript
复制
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); 
    } 

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-06 05:07:04

这里的关键是可比较的接口:

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

代码语言:javascript
复制
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是一个原语,不能实现可比较的,所以我把它改成了整数。

代码语言:javascript
复制
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));
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52673120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档