После того как написал Ламуто стало ясно, что он безбожно медленный. Решил проверить насколько. Вот оригинал:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Partition(int *a, int l, int r) { | |
r--; | |
int pivot = a[r]; | |
int i = l - 1; | |
for (int j = l; j < r; j++) | |
if (a[j] <= pivot) | |
swap(a[++i], a[j]); | |
swap(a[++i], a[r]); | |
return i; | |
} |
Видно, что он плохо себя ведет на отсортированном массиве, постоянно вызывает swap на один и тот же индекс. Улучшим:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Partition(int *a, int l, int r) { | |
r--; | |
int pivot = a[r]; | |
int i = l - 1; | |
for (int j = l; j < r; j++) | |
if (a[j] <= pivot) | |
if (++i != j) | |
swap(a[i], a[j]); | |
swap(a[++i], a[r]); | |
return i; | |
} |
А теперь сравним с Хоаром:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Partition(int *a, int l, int r) { | |
int x = a[l], i = l - 1, j = r; | |
while (1) { | |
do j--; while (a[j] > x); | |
do i++; while (a[i] < x); | |
if (i < j) | |
swap(a[i],a[j]); | |
else | |
return j + 1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fryday@fryday-HP-ZBook-15:~/sorts$ time ./quick_sort_lamuto 5000000 1234 | |
OK | |
real 0m39.338s | |
user 0m39.365s | |
sys 0m0.000s | |
fryday@fryday-HP-ZBook-15:~/sorts$ time ./quick_sort_lamuto_modified 5000000 1234 | |
OK | |
real 0m16.236s | |
user 0m16.143s | |
sys 0m0.016s | |
fryday@fryday-HP-ZBook-15:~/sorts$ time ./quick_sort_hoare 5000000 1234 | |
OK | |
real 0m0.780s | |
user 0m0.770s | |
sys 0m0.004s |
No comments:
Post a Comment