ZealOS/docs/Kernel/QuickSort.html

151 lines
15 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">
<meta name="generator" content="ZealOS V0.05">
<style type="text/css">
body {background-color:#000000;}
.cF0{color:#ffffff;background-color:#000000;}
.cF1{color:#3465a4;background-color:#000000;}
.cF2{color:#4e9a06;background-color:#000000;}
.cF3{color:#06989a;background-color:#000000;}
.cF4{color:#a24444;background-color:#000000;}
.cF5{color:#75507b;background-color:#000000;}
.cF6{color:#ce982f;background-color:#000000;}
.cF7{color:#bcc0b9;background-color:#000000;}
.cF8{color:#555753;background-color:#000000;}
.cF9{color:#729fcf;background-color:#000000;}
.cFA{color:#82bc49;background-color:#000000;}
.cFB{color:#34e2e2;background-color:#000000;}
.cFC{color:#ac3535;background-color:#000000;}
.cFD{color:#ad7fa8;background-color:#000000;}
.cFE{color:#fce94f;background-color:#000000;}
.cFF{color:#000000;background-color:#000000;}
</style>
</head>
<body>
<pre style="font-family:courier;font-size:10pt">
<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>
<a name="l7"></a>
2021-07-03 07:47:45 +01:00
<a name="l8"></a></span><span class=cF2>Maybe, look at </span><a href="https://www.tomawezome.github.io/ZealOS/Demo/MultiCore/MPRadix.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;
<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);
<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>
<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;
<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);
<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);
<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;
<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);
<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);
<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>
<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>
2021-07-03 07:47:45 +01:00
<a name="l106"></a></span><span class=cF2>Maybe, look at </span><a href="https://www.tomawezome.github.io/ZealOS/Demo/MultiCore/MPRadix.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;
<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>
<a name="l121"></a>}
</span></pre></body>
</html>