# What is the problem (quicksort)?

• 10-28-2012, 06:44 PM
TheElder777
What is the problem (quicksort)?
Hi all. I am currently working on quicksort and have written the following code and gives "almost" good results. I am not too fond of the data structure I have chosen but it is too late for me to change. Lesson learned. Can anyone help me debug this thing. I have been at it for a while. Anyway here is the code:

Code:

```        static Random myGen = new Random();         public static void quickSort(DLinkedList unsortedList, int start, int finish){                 int randpiv;                 int offset = 0;                 if(finish == -1){                         randpiv = 0;                 }else{                         randpiv = myGen.nextInt(finish+1);                 }                                 if(finish - start <= 1){                         return;                 }                 for(int i = start; i < randpiv; i++){                                        if(unsortedList.distofElem(i) >= unsortedList.distofElem(randpiv)){                                 offset++;                                 DLNode moveMe = unsortedList.removeDist(unsortedList.distofElem(i));                                 unsortedList.insertAfter(moveMe, randpiv);                         }                 }                 for(int i = randpiv + offset + 1; i <= finish; i++){                         if(unsortedList.distofElem(i) < unsortedList.distofElem(randpiv)){                                 DLNode moveMe = unsortedList.removeDist(unsortedList.distofElem(i));                                 unsortedList.insertBefore(moveMe, randpiv);                         }                 }                 quickSort(unsortedList, start, randpiv - 1);                 quickSort(unsortedList, randpiv + 1, finish);                        } //here are insert after and insert before// //insert before an index//         public void insertBefore(DLNode putMe, int index){                 listLength++;                 int intTemp = 0;                 for(temp = myRoot; intTemp < index; temp = temp.next){                         intTemp++;                 }                 System.out.println("Inserting: " + putMe.getDistance() + " BEFORE: " + temp.getDistance());                 if(temp == myRoot){                         myRoot.prev = putMe;                         putMe.next = myRoot;                         putMe.prev = null;                         myRoot = myRoot.prev;                 }else{                         temp.prev.next = putMe;                         putMe.prev = temp.prev;                                temp.prev = putMe;                         putMe.next = temp;                 }         }         //insert after an index//         public void insertAfter(DLNode putMe, int index){                 listLength++;                 int intTemp = 0;                 for(temp = myRoot; intTemp < index; temp = temp.next){                         intTemp++;                 }                 System.out.println("Inserting: " + putMe.getDistance() + " AFTER: " + temp.getDistance());                 if(temp == myTail){                         myTail.next = putMe;                         putMe.prev = myTail;                         putMe.next = null;                         myTail = myTail.next;                 }else{                         temp.next.prev = putMe;                         putMe.next = temp.next;                                temp.next = putMe;                         putMe.prev = temp;                 }         }```
here is the input i feed it:
29403 97291 7211 487075 10000000 9902 89600 1620 22500 180000 152 10 14700 1026046 6646 19769 17708 10201 16250 533026 1424561 880025 12930 91210 25004 580800 926718

Here is the output i get:
10
152
1620
6646
7211
9902
10201
12930
14700
16250
17708
19769
22500
25004
29403
89600
97291
180000
487075
880025
533026
1424561
91210
10000000
1026046
580800
926718

which is close, but not quite.