templeos-info/public/Wb/Kernel/QSort.HC.HTML

136 lines
12 KiB
Plaintext
Raw Normal View History

2024-03-24 21:24:44 +00:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="generator" content="TempleOS V5.03">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="/style/templeos.css">
<script src="/script/templeos.js"></script>
<style type="text/css">
.cF0{color:#000000;background-color:#ffffff;}
.cF1{color:#0000aa;background-color:#ffffff;}
.cF2{color:#00aa00;background-color:#ffffff;}
.cF3{color:#00aaaa;background-color:#ffffff;}
.cF4{color:#aa0000;background-color:#ffffff;}
.cF5{color:#aa00aa;background-color:#ffffff;}
.cF6{color:#aa5500;background-color:#ffffff;}
.cF7{color:#aaaaaa;background-color:#ffffff;}
.cF8{color:#555555;background-color:#ffffff;}
.cF9{color:#5555ff;background-color:#ffffff;}
.cFA{color:#55ff55;background-color:#ffffff;}
.cFB{color:#55ffff;background-color:#ffffff;}
.cFC{color:#ff5555;background-color:#ffffff;}
.cFD{color:#ff55ff;background-color:#ffffff;}
.cFE{color:#ffff55;background-color:#ffffff;}
.cFF{color:#ffffff;background-color:#ffffff;}
</style>
</head>
<body>
<pre id="content">
<a name="l1"></a><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>QSortI64</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 StrCmp(e1,e2);</span><span class=cF0>
<a name="l6"></a></span><span class=cF2>For ascending ints : return e1-e2;</span><span class=cF0>
<a name="l7"></a>
<a name="l8"></a></span><span class=cF2>Maybe, look at </span><a href="/Wb/Demo/MultiCore/MPRadix.HC.HTML#l1"><span class=cF4>::/Demo/MultiCore/MPRadix.HC</span></a><span class=cF2>.</span><span class=cF0>
<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> </span><span class=cF1>if</span><span class=cF0> (num&gt;1) </span><span class=cF7>{</span><span class=cF0>
<a name="l12"></a> left =base;
<a name="l13"></a> right=base+num-1;
<a name="l14"></a> pivot=base[num/2];
<a name="l15"></a> </span><span class=cF1>do</span><span class=cF0> {
<a name="l16"></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;0)
<a name="l17"></a> left++;
<a name="l18"></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;0)
<a name="l19"></a> right--;
<a name="l20"></a> </span><span class=cF1>if</span><span class=cF0> (left&lt;=right)
<a name="l21"></a> </span><span class=cF5>SwapI64</span><span class=cF0>(left++,right--);
<a name="l22"></a> } </span><span class=cF1>while</span><span class=cF0> (left&lt;=right);
<a name="l23"></a> i=right+1-base;
<a name="l24"></a> </span><span class=cF1>if</span><span class=cF0> (1&lt;i&lt;num)
<a name="l25"></a> </span><span class=cF5>QSortI64</span><span class=cF0>(base,i,fp_compare);
<a name="l26"></a> i=base+num-left;
<a name="l27"></a> </span><span class=cF1>if</span><span class=cF0> (1&lt;i&lt;num)
<a name="l28"></a> </span><span class=cF5>QSortI64</span><span class=cF0>(left,i,fp_compare);
<a name="l29"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l30"></a>}
<a name="l31"></a>
<a name="l32"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>QSort2a</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="l33"></a>{</span><span class=cF2>//Not public.For case of width==size(U8 *)==8.</span><span class=cF0>
<a name="l34"></a></span><span class=cF2>//fp_compare() passes by ref.</span><span class=cF0>
<a name="l35"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l36"></a> </span><span class=cF1>U8</span><span class=cF0> **left,**right,*pivot;
<a name="l37"></a> left =base;
<a name="l38"></a> right=base+num-1;
<a name="l39"></a> pivot=base[num/2];
<a name="l40"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l41"></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;0)
<a name="l42"></a> left++;
<a name="l43"></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;0)
<a name="l44"></a> right--;
<a name="l45"></a> </span><span class=cF1>if</span><span class=cF0> (left&lt;=right)
<a name="l46"></a> </span><span class=cF5>SwapI64</span><span class=cF0>(left++,right--);
<a name="l47"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>while</span><span class=cF0> (left&lt;=right);
<a name="l48"></a> i=right+1-base;
<a name="l49"></a> </span><span class=cF1>if</span><span class=cF0> (1&lt;i&lt;num)
<a name="l50"></a> </span><span class=cFD>QSort2a</span><span class=cF0>(base,i,fp_compare);
<a name="l51"></a> i=base+num-left;
<a name="l52"></a> </span><span class=cF1>if</span><span class=cF0> (1&lt;i&lt;num)
<a name="l53"></a> </span><span class=cFD>QSort2a</span><span class=cF0>(left,i,fp_compare);
<a name="l54"></a>}
<a name="l55"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>QSort2b</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,
<a name="l56"></a> </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="l57"></a>{</span><span class=cF2>//Not public</span><span class=cF0>
<a name="l58"></a> </span><span class=cF9>I64</span><span class=cF0> i;
<a name="l59"></a> </span><span class=cF1>U8</span><span class=cF0> *left,*right,*pivot=tmp+width;
<a name="l60"></a> left =base;
<a name="l61"></a> right=base+(num-1)*width;
<a name="l62"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(pivot,base+num/2*width,width);
<a name="l63"></a> </span><span class=cF1>do</span><span class=cF0> </span><span class=cF7>{</span><span class=cF0>
<a name="l64"></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;0)
<a name="l65"></a> left+=width;
<a name="l66"></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;0)
<a name="l67"></a> right-=width;
<a name="l68"></a> </span><span class=cF1>if</span><span class=cF0> (left&lt;=right) {
<a name="l69"></a> </span><span class=cF1>if</span><span class=cF0> (left!=right) </span><span class=cF7>{</span><span class=cF0>
<a name="l70"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(tmp,right,width);
<a name="l71"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(right,left,width);
<a name="l72"></a> </span><span class=cF5>MemCpy</span><span class=cF0>(left,tmp,width);
<a name="l73"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l74"></a> left+=width;
<a name="l75"></a> right-=width;
<a name="l76"></a> }
<a name="l77"></a> </span><span class=cF7>}</span><span class=cF0> </span><span class=cF1>while</span><span class=cF0> (left&lt;=right);
<a name="l78"></a> i=1+(right-base)/width;
<a name="l79"></a> </span><span class=cF1>if</span><span class=cF0> (1&lt;i&lt;num)
<a name="l80"></a> </span><span class=cFD>QSort2b</span><span class=cF0>(base,i,width,fp_compare,tmp);
<a name="l81"></a> i=num+(base-left)/width;
<a name="l82"></a> </span><span class=cF1>if</span><span class=cF0> (1&lt;i&lt;num)
<a name="l83"></a> </span><span class=cFD>QSort2b</span><span class=cF0>(left,i,width,fp_compare,tmp);
<a name="l84"></a>}
<a name="l85"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>QSort</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="l86"></a>{</span><span class=cF2>/*Quick Sort: fp_compare() passes by ref.</span><span class=cF0>
<a name="l87"></a>
<a name="l88"></a></span><span class=cF2>For ascending strings: return StrCmp(*e1,*e2);</span><span class=cF0>
<a name="l89"></a></span><span class=cF2>For ascending ints : return *e1-*e2;</span><span class=cF0>
<a name="l90"></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="l91"></a>
<a name="l92"></a></span><span class=cF2>Maybe, look at </span><a href="/Wb/Demo/MultiCore/MPRadix.HC.HTML#l1"><span class=cF4>::/Demo/MultiCore/MPRadix.HC</span></a><span class=cF2>.</span><span class=cF0>
<a name="l93"></a></span><span class=cF2>*/</span><span class=cF0>
<a name="l94"></a> </span><span class=cF1>U8</span><span class=cF0> *tmp;
<a name="l95"></a> </span><span class=cF1>if</span><span class=cF0> (width &amp;&amp; num&gt;1) </span><span class=cF7>{</span><span class=cF0>
<a name="l96"></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 MemCpy for width 8</span><span class=cF0>
<a name="l97"></a> </span><span class=cFD>QSort2a</span><span class=cF0>(base,num,fp_compare);
<a name="l98"></a> </span><span class=cF1>else</span><span class=cF0> {
<a name="l99"></a> tmp=</span><span class=cF5>MAlloc</span><span class=cF0>(width*2);
<a name="l100"></a> </span><span class=cFD>QSort2b</span><span class=cF0>(base,num,width,fp_compare,tmp);
<a name="l101"></a> </span><span class=cF5>Free</span><span class=cF0>(tmp);
<a name="l102"></a> }
<a name="l103"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l104"></a>}
</span></pre></body>
</html>