Heim  >  Artikel  >  php教程  >  php 冒泡排序 快速排序,php冒泡排序

php 冒泡排序 快速排序,php冒泡排序

WBOY
WBOYOriginal
2016-06-13 08:49:331450Durchsuche

php 冒泡排序 快速排序,php冒泡排序

/******
1)冒泡排序:两两交换数值,最小的值在最左边,就如最轻的气泡在最上边。
2)对整列数两两交换一次,最小的数在最左边,每次都能得一个在剩下的数中的最小 的数,“冒”出来的数组成一个有序区间,剩下的值组成一无序区间,且有序区间中每一元素值都比无序区间的小。
3)快速排序:基准数,左右二个数组,递归调用,合并。
4)插入排序:排序区间分成二部分,左边有序,右边无序,从右区间取第一个元素插入左区间,若此元素比左边区间最右边的元素大,留在原处,若此元素比左 边区间最右边的元素小,则插在最右边5)元素的原位置,同时最右边元素右移一位,计算器减一,重新和前面的元素比较,直到前面的元素比要插入元素小为止,重复 上述步骤。
6)注意区间端点值的处理,及数组的第一个元素下标为0.
***/

<span><br />$a</span>=<span>array</span>('3','8','1','4','11','7'<span>);
</span><span>print_r</span>(<span>$a</span><span>);
</span><span>$len</span> = <span>count</span>(<span>$a</span><span>);
</span><span>//</span><span>从小到大</span>
<span>for</span>(<span>$i</span>=1;<span>$i</span><<span>$len</span>;<span>$i</span>++<span>)
{
</span><span>for</span>(<span>$j</span>=<span>$len</span>-1;<span>$j</span>>=<span>$i</span>;<span>$j</span>--<span>)
</span><span>if</span>(<span>$a</span>[<span>$j</span>]<<span>$a</span>[<span>$j</span>-1<span>])
{</span><span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了</span>
 <span>$x</span>=<span>$a</span>[<span>$j</span><span>];
 </span><span>$a</span>[<span>$j</span>]=<span>$a</span>[<span>$j</span>-1<span>];
 </span><span>$a</span>[<span>$j</span>-1]=<span>$x</span><span>;
}
}
</span><span>print_r</span>(<span>$a</span><span>);
</span><span>//</span><span>另一种方法 从小到大</span>
<span>$b</span>=<span>array</span>('4','3','8','9','2','1'<span>);
</span><span>$len</span>=<span>count</span>(<span>$b</span><span>);
</span><span>for</span>(<span>$k</span>=1;<span>$k</span><<span>$len</span>;<span>$k</span>++<span>)
{
</span><span>for</span>(<span>$j</span>=<span>$len</span>-1,<span>$i</span>=0;<span>$i</span><<span>$len</span>-<span>$k</span>;<span>$i</span>++,<span>$j</span>--<span>)
</span><span>if</span>(<span>$b</span>[<span>$j</span>]<<span>$b</span>[<span>$j</span>-1<span>]){
</span><span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($b[$j]>$b[$j-1])就可以了</span>
 <span>$tmp</span>=<span>$b</span>[<span>$j</span><span>];
 </span><span>$b</span>[<span>$j</span>]=<span>$b</span>[<span>$j</span>-1<span>];
 </span><span>$b</span>[<span>$j</span>-1]=<span>$tmp</span><span>;
}
</span><span>print_r</span>(<span>$b</span><span>);
</span><span>echo</span> "
"<span>;
}
</span><span>//</span><span>下面的这个执行效率更高</span>
<span>function</span> maopao(<span>$arr</span><span>)
{
 </span><span>$len</span> = <span>count</span>(<span>$arr</span><span>);
 </span><span>for</span>(<span>$i</span>=1; <span>$i</span><<span>$len</span>; <span>$i</span>++)<span>//</span><span>最多做n-1趟排序</span>
<span> {
  </span><span>$flag</span> = <span>false</span>;    <span>//</span><span>本趟排序开始前,交换标志应为假</span>
  <span>for</span>(<span>$j</span>=<span>$len</span>-1;<span>$j</span>>=<span>$i</span>;<span>$j</span>--<span>)
  {
   </span><span>if</span>(<span>$arr</span>[<span>$j</span>]<<span>$arr</span>[<span>$j</span>-1])<span>//</span><span>交换记录</span>
   {<span>//</span><span>如果是从大到小的话,只要在这里的判断改成if($arr[$j]>$arr[$j-1])就可以了</span>
     <span>$x</span>=<span>$arr</span>[<span>$j</span><span>];
     </span><span>$arr</span>[<span>$j</span>]=<span>$arr</span>[<span>$j</span>-1<span>];
     </span><span>$arr</span>[<span>$j</span>-1]=<span>$x</span><span>;
     </span><span>$flag</span> = <span>true</span>;<span>//</span><span>发生了交换,故将交换标志置为真</span>
<span>   }
  }
  </span><span>if</span>(! <span>$flag</span>)<span>//</span><span>本趟排序未发生交换,提前终止算法</span>
  <span>return</span> <span>$arr</span><span>;   
 }
}
</span><span>$shuz</span> = <span>array</span>('2','4','1','8','5'<span>);
</span><span>$bb</span> = maopao(<span>$shuz</span><span>);
</span><span>print_r</span>(<span>$bb</span><span>);
</span><span>//</span><span> 快速排序</span>
<span>function</span> kuaisu(<span>$arr</span><span>){
    </span><span>$len</span> = <span>count</span>(<span>$arr</span><span>);
    </span><span>if</span>(<span>$len</span> <= 1<span>){
        </span><span>return</span> <span>$arr</span><span>;
    }
    </span><span>$key</span> = <span>$arr</span>[0<span>];
    </span><span>$left_arr</span> = <span>array</span><span>();
    </span><span>$right_arr</span> = <span>array</span><span>();
    </span><span>for</span>(<span>$i</span>=1; <span>$i</span><<span>$len</span>;<span>$i</span>++<span>){
        </span><span>if</span>(<span>$arr</span>[<span>$i</span>] <= <span>$key</span><span>){
            </span><span>$left_arr</span>[] = <span>$arr</span>[<span>$i</span><span>];
        }</span><span>else</span><span>{
            </span><span>$right_arr</span>[] = <span>$arr</span>[<span>$i</span><span>];
        }
    }
    </span><span>$left_arr</span> = kuaisu(<span>$left_arr</span><span>);
    </span><span>$right_arr</span> = kuaisu(<span>$right_arr</span><span>);
    </span><span>return</span> <span>array_merge</span>(<span>$left_arr</span>, <span>array</span>(<span>$key</span>), <span>$right_arr</span><span>);
}
</span><span>$arr</span> = <span>array</span>(23,98,54,2,9,62,34<span>);
</span><span>print_r</span>(kuaisu(<span>$arr</span>));

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn