# New portion of improvements for Dual-Pivot Quicksort

Dmytro Sheyko dmytro_sheyko at hotmail.com
Tue Jun 8 05:41:42 PDT 2010

```

Hi,

Coming back to NaN processing.
It appeared that current code unnecessarily stirs up NaNs in the end of array even when they are just on their places.
So I propose to replace these code
/*
* Phase 1: Move NaNs to the end of the array.
*/
for (int k = left; k <= right; k++) {
float ak = a[k];
if (ak != ak) { // a[k] is NaN
a[k--] = a[right];
a[right--] = ak;
}
}
with following
/*
* Phase 1: Move NaNs to the end of the array.
*/
while (left <= right && Float.isNaN(a[right])) {
right--;
}
for (int k = right - 1; k >= left; k--) {
float ak = a[k];
if (Float.isNaN(ak)) {
a[k] = a[right];
a[right] = ak;
right--;
}
}

Also I would like to note that while we are processing negative zeros, condition (k != p) is unnecessary.

for (int k = left + 1, p = left; k <= right; k++) {
float ak = a[k];
if (ak != 0.0f) {
return;
}
if (Float.floatToRawIntBits(ak) < 0) { // ak is -0.0f
if (k != p) { // !!! always true
a[k] = +0.0f;
a[p] = -0.0f;
}
p++;
}
}

Here k is strictly greater than p initially and then grows faster than p.

> From: iaroslavski at mail.ru
> To: dmytro_sheyko at hotmail.com
> CC: core-libs-dev at openjdk.java.net; iaroslavski at mail.ru
> Subject: Re[4]: New portion of improvements for Dual-Pivot Quicksort
> Date: Sat, 5 Jun 2010 23:40:31 +0400
>
> I tried with separate method sortPivotCandidates(...), no changes in behaviour,
> but at the same time I don't see that the method makes sources much cleaner,
>
> Fri, 4 Jun 2010 14:21:58 +0700 письмо от Dmytro Sheyko <dmytro_sheyko at hotmail.com>:
>
> > Seems good,
> >
> > One note. Since we gave up to sort pivot candidates in local variables, maybe we can move this out to separate procedure (in order to make sources cleaner a bit), e.g.
> >
> > private static void sortPivotCandidates(double[] a, int ae1, int ae2, int ae3, int ae4, int ae5)
> >
> > Hope the compiler is able to inline it without extra cost.
> >
> > Thanks,
> > Dmytro Sheyko
> >
> > > From: iaroslavski at mail.ru
> > > To: dmytro_sheyko at hotmail.com
> > > CC: core-libs-dev at openjdk.java.net; iaroslavski at mail.ru
> > > Subject: Re[2]: New portion of improvements for Dual-Pivot Quicksort
> > > Date: Fri, 4 Jun 2010 01:17:57 +0400
> > >
> > > Hello,
> > >
> > > I tried your case (which is selection sort) and it works as expected: not worse
> > > than "network" or "bubble" sorting. But nevertheless, the best choice is to use
> > > insertion sort, I wrote more elegant implementation, see:
> > >
> > > ///int ae1 = a[e1], ae3 = a[e3], ae5 = a[e5], ae2 = a[e2], ae4 = a[e4];
> > >
> > > // Sort these elements using insertion sort
> > > if (a[e2] < a[e1]) { int t = a[e2]; a[e2] = a[e1]; a[e1] = t; }
> > >
> > > if (a[e3] < a[e2]) { int t = a[e3]; a[e3] = a[e2]; a[e2] = t;
> > > if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
> > > }
> > > if (a[e4] < a[e3]) { int t = a[e4]; a[e4] = a[e3]; a[e3] = t;
> > > if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
> > > if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
> > > }
> > > }
> > > if (a[e5] < a[e4]) { int t = a[e5]; a[e5] = a[e4]; a[e4] = t;
> > > if (t < a[e3]) { a[e4] = a[e3]; a[e3] = t;
> > > if (t < a[e2]) { a[e3] = a[e2]; a[e2] = t;
> > > if (t < a[e1]) { a[e2] = a[e1]; a[e1] = t; }
> > > }
> > > }
> > > }
> > >
> > > ///a[e1] = ae1; a[e3] = ae3; a[e5] = ae5; a[e2] = ae2; a[e4] = ae4;
> > >
> > > Note that this implementation doesn't use local variables ae1, .. , ae5
> > > at all, and without variables it works faster. This code is not too long,
> > > extra 4 lines only. And if on client VM it works as other "network"
> > > implementations, but on server VM it wins 1.2%.
> > >
> > > In compare with first implementation of Dual-Pivot Quicksort, which
> > > is used now in JDK 7, suggested version wins ~15% and 6% for client
> > > and server modes.
> > >
> > > Updated version of the class I will send tomorrow.
> > >
> > > Dmytro,
> > > could you please look at suggested insertion sort for 5 elements?
> > >
> > > Do you have any comments/improvements? One place to be improved
> > > is last two ifs "if (a[e4] < ..." and "if (a[e5] < ..." where
> > > element is compared with all sorted elements, whereas we can save
> > > comparisons by binary fork. But implementation becomes too complex
> > > and long.
> > >
> > > As it can be expected, the best sorting for small arrays is insertion,
> > > then selection and then only bubble sort, even for 5 elements.
> > >
> > > Best regards,

_________________________________________________________________
Hotmail: Powerful Free email with security by Microsoft.
https://signup.live.com/signup.aspx?id=60969
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20100608/e7ffa0d7/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DualPivotQuicksort.java.diff
Type: application/octet-stream
Size: 2164 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20100608/e7ffa0d7/attachment-0001.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DualPivotQuicksort.java
Type: text/x-java
Size: 111391 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20100608/e7ffa0d7/attachment-0001.bin
```