Sorting arrays with quickSort (JAVA) -


okay, need sort array in both ascending , descending order using quick sort. have code works ascending, , most of time descending, randomly not work descending... when smallest number in original first position. might have array of (261, 940, 604, 655) means should output (940, 655, 604, 261) (655,940, 604, 261). however, cannot figure out why array not sort properly. hints?

private void sortbuttonactionperformed(java.awt.event.actionevent evt) {               // how array filled         (int =0; i<numnum; i++)         {              numlist [4] = generator.nextint(1000);         }             // call on quicksort "decending" method         quicksort_highlow(numlist, 0, numlist.length - 1);         for( int x=0; x <= numlist.length-1; x++)         {             sortedoutput.settext((sortedoutput.gettext())+ "\n" + numlist[x] );         } } public static int partitionhighlow(int[] numbers, int left, int right) {     int pivot = numbers[left];     while (true)     {         while (numbers[left] > pivot)             left ++ ;          while (numbers[right] < pivot)             right --;              if (left < right)             {                 int temp = numbers[left];                 numbers[left] = numbers[right];                 numbers[right] = temp;             }             else             {                 return right;             }     } }     public static void quicksort_highlow(int[] arr, int left, int right) {     // recusrion     if(left < right)     {         int pivot = partitionhighlow(arr, left, right);          if(pivot < 1)             quicksort_highlow(arr, left, pivot );          if(pivot + 1 < right)             quicksort_highlow(arr, pivot + 1, right);     } } 

one problem implementation has won't work if have equal numbers (you keep switching them each other if chosen pivot).

the other in recursion:

if(pivot > left + 1)     quicksort_highlow(arr, left, pivot - 1);  if(pivot < right - 1)     quicksort_highlow(arr, pivot + 1, right);