c# - quicksort algorithm not sorting final element? -


i have code wrote using algorithm found on wikipedia:

    public static void quicksort(int[] arr, int low, int hi)     {         if(low < hi)         {             int pivot = part( arr, low, hi);             quicksort( arr, low, pivot - 1);             quicksort( arr, pivot + 1, hi);         }     }     public static int part(int[] arr, int low, int hi)     {         int pivot = arr[hi];          int = low;         for(int j = low; j < hi - 1; j++)         {             if(arr[j] <= pivot)             {                 swap(arr, i, j);                 i++;             }         }         swap(arr, i, hi);         return i;     }      public static void swap(int[] ar, int a, int b)     {         int temp = ar[a];         ar[a] = ar[b];         ar[b] = temp;     } 

given input:

31, 5, 5, 5, 5, 4, 4, 4, 5, 4 

one should expect get:

4, 4, 4, 4, 5, 5, 5, 5, 5, 31 

but instead get:

4, 4, 4, 4, 5, 5, 5, 5, 31, 5 

what gives?

i call initial sort with: quicksort(array, 0, array.length - 1);

if call using length - 1, loop:

 (int j = low; j < hi - 1; j++) 

..should be:

 (int j = low; j <= hi ; j++) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -