Implementing a SelectionSort capable of handling array list, linked list, and doubly linked list in java -


background: want implement selection sort capable of handling array list, linked list, , doubly linked list. each list type have position{} , list{} class. position{} class holds list, (i.e. class position{int head; list tail;}). while list{} contains next(), previous(), end(), insert(), first() methods , uses position{}, not contain list class itself, list class constructs list called class position{}.

question: problem not making selection sort compatible, have achieved using commands common between 3 list abstract data types. my problem selection sort returns same list , no sort of sorting. list printed after sort same list printed before sort. appreciated.

output before selection sort 2 879 621 229 702 40 243 312 247 46 711 241 after selection sort 2 879 621 229 702 40 243 312 247 46 711 241

my list adt's correct, problem lies in poor selectionsort.

public static void selectionsort(list a) {          position i, j, maxp, temp;         for(i = a.previous(a.end()); !i.isequal(a.first()); = a.previous(i)) {             maxp = i;             for(j = a.first(); !j.isequal(i); j = a.next(j))  {                 if((integer)a.select(j) > (integer)a.select(maxp)) {                     maxp = j;                 }                  }                temp = i;             a.insert(i, a.select(maxp));             a.insert(maxp, a.select(temp));             a.delete(maxp);             a.delete(i);         }     } 

you're first inserting @ index i, inserting @ index maxp, deleting thing @ index maxp , deleting @ index i. assuming insert() , delete() names imply, sequence of operations leaves list in initial state after each iteration of loop since deletions undo insertions.

as side note, code doesn't follow java's naming conventions makes harder read needs be.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -