ZealOS/docs/Kernel/QuickSort.CC.html

151 lines
14 KiB
HTML
Raw Normal View History

2021-07-03 05:07:57 +01:00
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
2021-09-22 04:45:45 +01:00
<meta name="generator" content="ZealOS V1.01">
2021-07-03 05:07:57 +01:00
<style type="text/css">
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
2021-07-03 05:07:57 +01:00
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
2021-07-03 05:07:57 +01:00
<a name="l1"></a><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>QuickSortI64</span><span class=cF0>(</span><span class=cF9>I64</span><span class=cF0> *base, </span><span class=cF9>I64</span><span class=cF0> num, </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF9>I64</span><span class=cF0> e1, </span><span class=cF9>I64</span><span class=cF0> e2</span><span class=cF7>)</span><span class=cF0>)
<a name="l2"></a>{</span><span class=cF2>/*Quick Sort for width==8.</span><span class=cF0>
<a name="l3"></a></span><span class=cF2>fp_compare() passes by value instead of ref.</span><span class=cF0>
<a name="l4"></a>
<a name="l5"></a></span><span class=cF2>For ascending strings: return StrCompare(e1,e2);</span><span class=cF0>
<a name="l6"></a></span><span class=cF2>For ascending ints</span><span class=cF0> </span><span class=cF2> : return e1-e2;</span><span class=cF0>
2021-07-03 05:07:57 +01:00
<a name="l7"></a>
<a name="l8"></a></span><span class=cF2>Maybe, look at </span><a href="https://zeal-operating-system.github.io/ZealOS/Demo/MultiCore/MPRadix.CC.html#l1"><span class=cF4>::/Demo/MultiCore/MPRadix.CC</span></a><span class=cF2>.</span><span class=cF0>
2021-07-03 05:07:57 +01:00
<a name="l9"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l10"></a> </span><span class=cF9>I64</span><span class=cF0> i, *left, *right, pivot;
2021-07-03 05:07:57 +01:00
<a name="l11"></a>
<a name="l12"></a> </span><span class=cF1>if</span><span class=cF0> (num &gt; </span><span class=cFE>1</span><span class=cF0>)
<a name="l13"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l14"></a> left = base;
<a name="l15"></a> right = base + num - </span><span class=cFE>1</span><span class=cF0>;
<a name="l16"></a> pivot = base[num / </span><span class=cFE>2</span><span class=cF0>];
<a name="l17"></a> </span><span class=cF1>do</span><span class=cF0>
<a name="l18"></a> {
<a name="l19"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>*left, pivot</span><span class=cF7>)</span><span class=cF0> &lt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l20"></a> left++;
<a name="l21"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>*right, pivot</span><span class=cF7>)</span><span class=cF0> &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l22"></a> right--;
<a name="l23"></a> </span><span class=cF1>if</span><span class=cF0> (left &lt;= right)
<a name="l24"></a> </span><span class=cF5>SwapI64</span><span class=cF0>(left++, right--);
<a name="l25"></a> }
<a name="l26"></a> </span><span class=cF1>while</span><span class=cF0> (left &lt;= right);
2021-07-03 05:07:57 +01:00
<a name="l27"></a>
<a name="l28"></a> i = right + </span><span class=cFE>1</span><span class=cF0> - base;
<a name="l29"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> &lt; i &lt; num)
<a name="l30"></a> </span><span class=cF5>QuickSortI64</span><span class=cF0>(base, i, fp_compare);
<a name="l31"></a> i = base + num - left;
<a name="l32"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> &lt; i &lt; num)
<a name="l33"></a> </span><span class=cF5>QuickSortI64</span><span class=cF0>(left, i, fp_compare);
<a name="l34"></a> </span><span class=cF7>}</span><span class=cF0>
2021-07-03 05:07:57 +01:00
<a name="l35"></a>}
<a name="l36"></a>
<a name="l37"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>QuickSort2a</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> **base, </span><span class=cF9>I64</span><span class=cF0> num, </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF1>U8</span><span class=cF0> **_e1, </span><span class=cF1>U8</span><span class=cF0> **_e2</span><span class=cF7>)</span><span class=cF0>)
<a name="l38"></a>{</span><span class=cF2>//Not public.For case of width==size(U8 *)==8.</span><span class=cF0>
<a name="l39"></a></span><span class=cF2>//fp_compare() passes by ref.</span><span class=cF0>
<a name="l40"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l41"></a> </span><span class=cF1>U8</span><span class=cF0> **left, **right, *pivot;
2021-07-03 05:07:57 +01:00
<a name="l42"></a>
<a name="l43"></a> left = base;
<a name="l44"></a> right = base + num - </span><span class=cFE>1</span><span class=cF0>;
<a name="l45"></a> pivot = base[num / </span><span class=cFE>2</span><span class=cF0>];
<a name="l46"></a> </span><span class=cF1>do</span><span class=cF0>
<a name="l47"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l48"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>left, &amp;pivot</span><span class=cF7>)</span><span class=cF0> &lt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l49"></a> left++;
<a name="l50"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>right, &amp;pivot</span><span class=cF7>)</span><span class=cF0> &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l51"></a> right--;
<a name="l52"></a> </span><span class=cF1>if</span><span class=cF0> (left &lt;= right)
<a name="l53"></a> </span><span class=cF5>SwapI64</span><span class=cF0>(left++, right--);
<a name="l54"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l55"></a> </span><span class=cF1>while</span><span class=cF0> (left &lt;= right);
2021-07-03 05:07:57 +01:00
<a name="l56"></a>
<a name="l57"></a> i = right + </span><span class=cFE>1</span><span class=cF0> - base;
<a name="l58"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> &lt; i &lt; num)
<a name="l59"></a> </span><span class=cFD>QuickSort2a</span><span class=cF0>(base, i, fp_compare);
<a name="l60"></a> i = base + num - left;
<a name="l61"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> &lt; i &lt; num)
<a name="l62"></a> </span><span class=cFD>QuickSort2a</span><span class=cF0>(left, i, fp_compare);
2021-07-03 05:07:57 +01:00
<a name="l63"></a>}
<a name="l64"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>QuickSort2b</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *base, </span><span class=cF9>I64</span><span class=cF0> num, </span><span class=cF9>I64</span><span class=cF0> width, </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF1>U8</span><span class=cF0> *e1, </span><span class=cF1>U8</span><span class=cF0> *e2</span><span class=cF7>)</span><span class=cF0>, </span><span class=cF1>U8</span><span class=cF0> *tmp)
<a name="l65"></a>{</span><span class=cF2>//Not public</span><span class=cF0>
<a name="l66"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l67"></a> </span><span class=cF1>U8</span><span class=cF0> *left, *right, *pivot = tmp + width;
2021-07-03 05:07:57 +01:00
<a name="l68"></a>
<a name="l69"></a> left = base;
<a name="l70"></a> right = base + (num - </span><span class=cFE>1</span><span class=cF0>) * width;
<a name="l71"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(pivot, base + num / </span><span class=cFE>2</span><span class=cF0> * width, width);
<a name="l72"></a> </span><span class=cF1>do</span><span class=cF0>
<a name="l73"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l74"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>left, pivot</span><span class=cF7>)</span><span class=cF0> &lt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l75"></a> left += width;
<a name="l76"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF0>right, pivot</span><span class=cF7>)</span><span class=cF0> &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l77"></a> right -= width;
<a name="l78"></a> </span><span class=cF1>if</span><span class=cF0> (left &lt;= right)
<a name="l79"></a> {
<a name="l80"></a> </span><span class=cF1>if</span><span class=cF0> (left != right)
<a name="l81"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l82"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(tmp, right, width);
<a name="l83"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(right, left, width);
<a name="l84"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(left, tmp, width);
<a name="l85"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l86"></a> left += width;
<a name="l87"></a> right -= width;
<a name="l88"></a> }
<a name="l89"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l90"></a> </span><span class=cF1>while</span><span class=cF0> (left &lt;= right);
2021-07-03 05:07:57 +01:00
<a name="l91"></a>
<a name="l92"></a> i = </span><span class=cFE>1</span><span class=cF0> + (right - base) / width;
<a name="l93"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> &lt; i &lt; num)
<a name="l94"></a> </span><span class=cFD>QuickSort2b</span><span class=cF0>(base, i, width, fp_compare, tmp);
<a name="l95"></a> i = num + (base - left) / width;
<a name="l96"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFE>1</span><span class=cF0> &lt; i &lt; num)
<a name="l97"></a> </span><span class=cFD>QuickSort2b</span><span class=cF0>(left, i, width, fp_compare, tmp);
2021-07-03 05:07:57 +01:00
<a name="l98"></a>}
<a name="l99"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>QuickSort</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *base, </span><span class=cF9>I64</span><span class=cF0> num, </span><span class=cF9>I64</span><span class=cF0> width, </span><span class=cF9>I64</span><span class=cF0> </span><span class=cF7>(</span><span class=cF0>*fp_compare</span><span class=cF7>)(</span><span class=cF1>U8</span><span class=cF0> *e1, </span><span class=cF1>U8</span><span class=cF0> *e2</span><span class=cF7>)</span><span class=cF0>)
<a name="l100"></a>{</span><span class=cF2>/*Quick Sort: fp_compare() passes by ref.</span><span class=cF0>
<a name="l101"></a>
<a name="l102"></a></span><span class=cF2>For ascending strings: return StrCompare(*e1,*e2);</span><span class=cF0>
<a name="l103"></a></span><span class=cF2>For ascending ints</span><span class=cF0> </span><span class=cF2> : return *e1-*e2;</span><span class=cF0>
2021-07-03 05:07:57 +01:00
<a name="l104"></a></span><span class=cF2>Don't return e1-e2 if numbers can overflow, return -1,0 or 1.</span><span class=cF0>
<a name="l105"></a>
<a name="l106"></a></span><span class=cF2>Maybe, look at </span><a href="https://zeal-operating-system.github.io/ZealOS/Demo/MultiCore/MPRadix.CC.html#l1"><span class=cF4>::/Demo/MultiCore/MPRadix.CC</span></a><span class=cF2>.</span><span class=cF0>
2021-07-03 05:07:57 +01:00
<a name="l107"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l108"></a> </span><span class=cF1>U8</span><span class=cF0> *tmp;
2021-07-03 05:07:57 +01:00
<a name="l109"></a>
<a name="l110"></a> </span><span class=cF1>if</span><span class=cF0> (width &amp;&amp; num &gt; </span><span class=cFE>1</span><span class=cF0>)
<a name="l111"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l112"></a> </span><span class=cF1>if</span><span class=cF0> (width == </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF7>)</span><span class=cF0>) </span><span class=cF2>//assign instead of MemCopy for width 8</span><span class=cF0>
<a name="l113"></a> </span><span class=cFD>QuickSort2a</span><span class=cF0>(base, num, fp_compare);
<a name="l114"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l115"></a> {
<a name="l116"></a> tmp = </span><span class=cF5>MAlloc</span><span class=cF0>(width * </span><span class=cFE>2</span><span class=cF0>);
<a name="l117"></a> </span><span class=cFD>QuickSort2b</span><span class=cF0>(base, num, width, fp_compare, tmp);
<a name="l118"></a> </span><span class=cF5>Free</span><span class=cF0>(tmp);
<a name="l119"></a> }
<a name="l120"></a> </span><span class=cF7>}</span><span class=cF0>
2021-07-03 05:07:57 +01:00
<a name="l121"></a>}
</span></pre></body>
</html>