Skip to main content
added link to full size image
Source Link
Celeritas
  • 911
  • 4
  • 14
  • 22
deleted 124 characters in body
Source Link
Celeritas
  • 911
  • 4
  • 14
  • 22

screenshot

Here is partition and a method for swapping

Here is partition and a method for swapping

screenshot

Here is partition and a method for swapping

added partition and swap method
Source Link
Celeritas
  • 911
  • 4
  • 14
  • 22

Can it be made any faster? FWIW I profiled the above code against regular (recursive) quicksort and it was usually faster (tested on randomly generated arrays).

Here is partition and a method for swapping

private static int partition(int[] a, int p, int r) {

    int x = a[p];
    int i = p-1 ;
    int j = r+1 ;

    while (true) {
        i++;
        while ( i< r && a[i] < x)
            i++;
        j--;
        while (j>p && a[j] > x)
            j--;

        if (i < j)
            swap(a, i, j);
        else
            return j;
    }
}

private static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

Can it be made any faster?

Can it be made any faster? FWIW I profiled the above code against regular (recursive) quicksort and it was usually faster (tested on randomly generated arrays).

Here is partition and a method for swapping

private static int partition(int[] a, int p, int r) {

    int x = a[p];
    int i = p-1 ;
    int j = r+1 ;

    while (true) {
        i++;
        while ( i< r && a[i] < x)
            i++;
        j--;
        while (j>p && a[j] > x)
            j--;

        if (i < j)
            swap(a, i, j);
        else
            return j;
    }
}

private static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
Loading
Post Reopened by 200_success
deleted 468 characters in body
Source Link
Celeritas
  • 911
  • 4
  • 14
  • 22
Loading
Post Closed as "Not suitable for this site" by 200_success
Source Link
Celeritas
  • 911
  • 4
  • 14
  • 22
Loading