java - Quick Sort using recursion on a linked list -
i have quick sort recursion on linked list.... far i've been ok, ran little problem can't see figure out why not working correctly.
here object node:
public class node { string name; node next; }
here program's code:
public class quicksortrecusionlinkedlist { public static void quicks(int start, int finish, node head, node tail) { int left = start; int right = finish; node pivot = head; for(int = 0; < ((left+right)/2); i++) { pivot = pivot.next; } node temp = new node(); node leftn = head; node rightn = head; while(right > left) { leftn = head; for(int = 0; < left; i++) { leftn = leftn.next; } while ((leftn.name).comparetoignorecase((pivot.name))<0) { left = left + 1; leftn = leftn.next; } rightn = head; for(int = 0; < right; i++) { rightn = rightn.next; } while ((pivot.name).comparetoignorecase((rightn.name))<0) { right = right - 1; rightn = head; for(int = 0; < right; i++) { rightn = rightn.next; } } if ( left <= right ) { temp.name = leftn.name; leftn.name = rightn.name; rightn.name = temp.name; left = left +1; leftn = leftn.next; right = right -1; rightn = head; for(int = 0; < right; i++) { rightn = rightn.next; } int size = 1; temp = head; while (temp!=tail) { temp = temp.next; size++; } temp = head; while(temp != tail) { system.out.print(temp.name + ", "); temp = temp.next; } system.out.println(tail.name + "."); } } if(start < right) quicks(start, right, head, tail); if(left < finish) quicks(left, finish, head, tail); } public static void main(string[] args) { node head = new node(); node tail = new node(); node = new node(); node b = new node(); node c = new node(); head.name = "r"; tail.name = "d"; a.name = "z"; b.name = "c"; c.name = "p"; head.next = a; a.next = b; b.next = c; c.next = tail; int size = 0; node temp = head; while (temp!= tail) { temp = temp.next; size++; } quicks(0,size,head,tail); } }
here printout:
c, z, r, p, d. c, z, r, p, d. c, d, r, p, z. c, d, p, r, r. c, d, p, r, r. c, d, p, r, r.
the end result should c, d, p, r, z
. reason program substituting z
r
. wrong code?
possible hint: careful temp
variable pointing when use swapping name.
Comments
Post a Comment