Search code examples
javasortingbubble-sortcomparable

Funky Bubble Sort (Java Eclipse)


I am currently working on a basic bubble sort except it uses Comparable, and is throwing me off because I am not sure where to implement its function.

This is what I was given and cannot change

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

This is what I created in my testing class

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

}

Solution

  • The key here is the Comparable interface:

    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.
    

    once we know how to compare the object we can use it to perform this bubble sort. Also since int is a primitive and cannot implement Comparable I switched it to 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));
        }
    }