Yaroslavskiy’s dual pivot quick sort algorithm

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

I am working on dual pivot quick sort I found here (page no-20 in slide)

Comparisons:

Yaroslavskiy needs = 1.9 n ln n on average.

Classic Quicksort needs = 2 n ln n comparisons!

Swaps:

Swaps for Yaroslavskiy’s algorithm = 0.6 n ln n

Swaps for classic Quicksort=0.3 n ln n

Results

Data type-----comp-------swap

int -------------591ns---------802ns

float-----------838ns----------873ns

double -------873ns----------1047ns

char ----------593ns-----------837ns

/* note :- above results in nanosecond and performed in java lang using intel core 2 duo */

if we combine the cost of swap and comparison than Classic Quicksort beats Yaroslavskiy Quicksort except in case of string where we use array of pointer to swap which require 88 nanosecond.Here Yaroslavskiy’s algorithm take advantage of 1.9 n ln n comparison because comparison is too much expensive compare to swap in case of string.

i want to know why java uses Yaroslavskiy Quicksort ? is main focus of inbuilt library sort are string what if it is not good on others data type?

回答1:

I don't know where you got your numbers from. According to this page:

It is proved that for the Dual-Pivot Quicksort the average number of comparisons is 2*n*ln(n), the average number of swaps is 0.8*n*ln(n), whereas classical Quicksort algorithm has 2*n*ln(n) and 1*n*ln(n) respectively.

It looks like dual-pivot is always better.



回答2:

I tested dual java's pivot quicksort against many other quicksorts. It was 10~15% faster at minimum. Besides doublepivotness, it uses a lot of other optimisations:

  • 5-point pivot selection
  • insertion sort degration for small subarrays
  • data properties detection (instead of shuffling)


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!