Common bubble problems

I know that there are built-in procedures, but as a student, I want to sort using my own devices, and since sorting is an old hat, I decided to try to create my own regular sorting procedure that I could use for numbers or strings and, maybe even dates if I ever figure out how they work in Java.

So, here's what I have, trading one error for another for another, so far I only have errors in two places (enclosed in markers "**"), with the need to figure out how to compare.

package sort;
import java.util.ArrayList;

public  abstract class Sort<E> implements Comparable<E> {

   public void swap(ArrayList<E> a, int i, int j) {
    E c = a.get(i);
    a.set(i,a.get(j));// = a[j];
    a.set(j, c);
  }

  public void bubbleSort(ArrayList<E> a) {
    boolean inOrder = false;
    while (!inOrder) {
      inOrder = true;
      for (int i = 1; i < a.size(); i++) {
        **if( a.get(i - 1).compareTo(a.get(i)) > 0 )** {
//cannot find symbol: method compareTo(E); location: class Object
//where E is a type-variable: E extends Object declared in class Sort                 
      inOrder = false;
          swap(a, i, i - 1);
        } 
      }
    }
  }

  public static void main(String args[]) //hadda lose 'static' for 'setLayout' to work
  {
    ArrayList<Integer> ary = new ArrayList<>();
    ary.add(2); ary.add(4); ary.add(7); ary.add(3);
    **bubbleSort(ary)**;
//method bubbleSort in class Sort<E> cannot be applied to given types; 
//required: ArrayList<E>
//found: ArrayList<Integer>
//reason: actual argument ArrayList<Integer> cannot be converted to ArrayList<E> 
//by method invocation conversion where E is a type-variable:
//E extends Object declared in class Sort
    for (int i = 0; i < ary.size(); i++) {
      System.out.println(ary.get(i));
    }
  }

  @Override
  public int compareTo(E o) {
    **return 0;** // fixing errors above may help this fall into place
  }
}

I am trying to learn things that I think are ready only to find that I am not quite ready; close without a cigar.

+4
3

:

public  abstract class Sort<E> implements Comparable<E> {

, E - , Sort<E> E. ( , , E.compareTo , Object .) :

public abstract class Sort<E extends Comparable<E>> {

, E , .


: , SLaks, Sort ; bubbleSort. , MadProgrammer, Sort abstract ( ), bubbleSort static ( Sort) , :

public class Sort {
    private static <E> void swap(ArrayList<E> a, int i, int j) {
        ...
    }

    private static <E extends Comparable<E>> void bubbleSort(ArrayList<E> a) {
        ...
    }

    ...
}

, Sort Sort, BubbleSort.sort(...) ( , Sort bubbleSort).

+4

, .

BubbleSort - Java. , BubbleSort, , "StoogeSort".

, , , , , (, ) . . Bubblesort , .

, - " ", " ". ​​ , BubbleSort (O (n ^ 2)), BubbleSort, - . , , , Selection Sort - , swap .

, , , , BubbleSort:

Begin DELAYEDSORT
 For ITEM=1 to maximum number of items in list-1
    LOWEST=ITEM
    For N=ITEM+1 to maximum number of items in list
       Is entry at position N lower than entry at position LOWEST?
          If so, LOWEST=N
    Next N
    Is ITEM different from LOWEST
       If so, swap entry at LOWEST with entry in ITEM
 Next ITEM
End DELAYEDSORT

50% , BubbleSort, .

+1

SLaks ( ), ( private public "bubblesort" ), main - ( -- Sort.bubbleSort(ary);), , :

(1) <E> /do/ :

private static <E> void swap(ArrayList<E> a, int i, int j) {

(2) ... <E extends Comparable<E>> :

private static <E extends Comparable<E>> void bubbleSort(ArrayList<E> a) {

: ", ?"... " ?"

EDIT: “parameterized” or “generic” methods are legal, like parameterized / generic types, and why not?

But I still don't know why, “obviously legal” or not, I would think of a “trick”.

EDIT: one word: experience

0
source

All Articles