History

libavutil: add a merge sort.

compared to qsort this is slower but its stable and doesnt have a O(n^2) worst case

Michael Niedermayer committed 14 years ago

Commit f87dacb1 parent 096db651 file · +25 −0Browse files

Unified · Split

libavutil/qsort.h+25−0
@@ -90,3 +90,28 @@ · 89 unmodified lines
90 }\90 }\
91 }\91 }\
92}92}
93
94/**
95 * Merge sort, this sort requires a temporary buffer and is stable, its worst
96 * case time is O(n log n)
97 * @param p must be a lvalue pointer, this function may exchange it with tmp
98 * @param tmp must be a lvalue pointer, this function may exchange it with p
99 */
100#define AV_MSORT(p, tmp, num, type, cmp) {\
101 unsigned i, j, step;\
102 for(step=1; step<(num); step+=step){\
103 for(i=0; i<(num); i+=2*step){\
104 unsigned a[2] = {i, i+step};\
105 unsigned end = FFMIN(i+2*step, (num));\
106 for(j=i; a[0]<i+step && a[1]<end; j++){\
107 int idx= cmp(p+a[0], p+a[1]) < 0;\
108 tmp[j] = p[ a[idx]++ ];\
109 }\
110 if(a[0]>=i+step) a[0] = a[1];\
111 for(; j<end; j++){\
112 tmp[j] = p[ a[0]++ ];\
113 }\
114 }\
115 FFSWAP(type*, p, tmp);\
116 }\
117}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t