PHPソートアルゴリズム類【共有】


4種類の並べ替えアルゴリズムのPHP実現
1)挿入ソート(Insertion Sort)の基本思想は次のとおりである.
ソートされるレコードを1つずつ、すべてのレコードの挿入が完了するまで、前に並べられたサブファイルの適切な位置にキーワードサイズで挿入します.
2)選択ソート(Selection Sort)の基本思想は次のとおりである.
各ソート対象レコードからキーワードの最小レコードを選択し、すべてのレコードがソートされるまで、ソートされたサブファイルの最後に順番に配置します.
3)泡立ちソートの基本的な考え方は:
並べ替えられるレコードのキーワードを2つ比較し,2つのレコードの順序が逆であることが分かった場合,逆順序のレコードがないまで交換する.
4)高速ソートは実質的にバブルソートと同様に,交換ソートの応用である.だから基本思想は上の泡の順序と同じです. 
<?php
/**
 *         (PHP)
 *
 * 1)     (Insertion Sort)      :
	             ,                           ,            。
   2)     (Selection Sort)      :
                           ,               ,          。
   3)           :
                   ,                 ,           。
   4)               ,             。                  。
 * 
 * @author quanshuidingdang
 */
class Sort {
	private $arr 	= array();	
	private $sort	= 'insert';
	private $marker = '_sort';
	
	private $debug = TRUE;
	
	/**
	 *     
	 *
	 * @param	array	  : $config = array (
									'arr' => array(22,3,41,18) , 	//        
									'sort' => 'insert',			  	//   : insert, select, bubble, quick
									'debug' => TRUE	    			//   : TRUE, FALSE
									)
	 */
	public function __construct($config = array()) {
		if ( count($config) > 0) {
			$this->_init($config);
		}
	}
	
	/**
	 *       
	 */
	public function display() {
		return $this->arr;
	}
	
	/**
	 *    
	 *
	 * @param	array
	 * @return 	bool
	 */
	private function _init($config = array()) {
		//    
		if ( !is_array($config) OR count($config) == 0) {
			if ($this->debug === TRUE) {
				$this->_log("sort_init_param_invaild");
			}
			return FALSE;
		}
		
		//       
		foreach ($config as $key => $val) {
			if ( isset($this->$key)) {
				$this->$key = $val;
			}
		}
		
		//             
		$method = $this->sort . $this->marker;
		if ( ! method_exists($this, $method)) {
			if ($this->debug === TRUE) {
				$this->_log("sort_method_invaild");
			}
			return FALSE;
		}
		
		if ( FALSE === ($this->arr = $this->$method($this->arr)))
			return FALSE;
		return TRUE;
	}
	
	/**
	 *     
	 * 
	 * @param	array
	 * @return	bool
	 */
	private function insert_sort($arr) {
		//    
		if ( ! is_array($arr) OR count($arr) == 0) {
			if ($this->debug === TRUE) {
				$this->_log("sort_array(insert)_invaild");
			}
			return FALSE;
		}
		
		//    
		$count = count($arr);
		for ($i = 1; $i < $count; $i++) {
			$tmp = $arr[$i];//         
			for($j = $i-1; $j >= 0; $j--) {	
				if($arr[$j] > $tmp) {//            ,        
					$arr[$j+1] = $arr[$j];//           ,          ,       
					$arr[$j] = $tmp;
				}
			}
		}
		return $arr;
	}
	
	/**
	 *     
	 *                  (  )     ,              ,              。 
	 * @param	array
	 * @return	bool
	 */
	private function select_sort($arr) {
		//    
		if ( ! is_array($arr) OR count($arr) == 0) {
			if ($this->debug === TRUE) {
				$this->_log("sort_array(select)_invaild");
			}
			return FALSE;
		}
		
		//    
		$count = count($arr);
		for ($i = 0; $i < $count-1; $i++) {
			$min = $i;//      
			for ($j = $i+1; $j < $count; $j++) {
				if ($arr[$min] > $arr[$j])  $min = $j;
			}
			if ($min != $i) {
				$tmp = $arr[$min];
				$arr[$min] = $arr[$i];
				$arr[$i] = $tmp;
			}
		}
		return $arr;
	}
	
	/**
	 *     
	 *               ,                  ,              
	 * @param	array
	 * @return	bool
	 */
	private function bubble_sort($arr) {
		//    
		if ( ! is_array($arr) OR count($arr) == 0) {
			if ($this->debug === TRUE) {
				$this->_log("sort_array(bubble)_invaild");
			}
			return FALSE;
		}
		
		//    
		$count = count($arr);
		for ($i = 0; $i < $count; $i++) {
			for ($j = $count-1; $j > $i; $j--) {
				if ($arr[$j] < $arr[$j-1]) {/           
					$tmp = $arr[$j];
					$arr[$j] = $arr[$j-1];
					$arr[$j-1] = $tmp;
				}
			}
		}
		return $arr;	
	}
	
	/**
	 *     
	 * 
	 * @param	array
	 * @return	bool
	 */
	private function quick_sort($arr) {
		//    
		if (count($arr) <= 1) return $arr; 
		$key = $arr[0];
		$left_arr = array();
		$right_arr = array();
		for ($i = 1; $i < count($arr); $i++){
			if ($arr[$i] <= $key)
				$left_arr[] = $arr[$i];
			else
				$right_arr[] = $arr[$i];
		}
		$left_arr = $this->quick_sort($left_arr);
		$right_arr = $this->quick_sort($right_arr); 

		return array_merge($left_arr, array($key), $right_arr);
	}
	
/**
  *           
  * strOrder        asc     desc   
  */
function sortByVal($arr,$strOrder='asc')
{
	if(!is_array($arr) || count($arr)==0)
	{
		return $arr;
	}

	$arrReturn = array();
	foreach($arr as $key=>$val)
	{
		$arrKey[] = $key;
		$arrVal[] = $val;
	}

	$count = count($arrVal);
	if($count)
	{
		//  key     
		for($key=0;$key<$count;$key++)
		{
			$arrKeyMap[$key] = $key;  
		}
		//      
		for($i=0;$i<$count;$i++)
		{	
			
			for($j = $count-1; $j>$i;$j--)
			{
				//<             
				$bol = $strOrder == 'asc' ? $arrVal[$j]<$arrVal[$j-1] : $arrVal[$j]>$arrVal[$j-1];
				if($bol){
					$tmp = $arrVal[$j];
					$arrVal[$j] = $arrVal[$j-1];
					$arrVal[$j-1] = $tmp;
					//      ,  key      	
					$keytmp = $arrKeyMap[$j];
					$arrKeyMap[$j] = $arrKeyMap[$j-1];
					$arrKeyMap[$j-1] = $keytmp;
				}
			}
		}
		if(count($arrKeyMap))
		{
			foreach ($arrKeyMap as $val)
			{
					$arrReturn[] = $arrKey[$val];
			}
		}
		return $arrReturn;
	}
}
/**ログ*/private function_log($msg) {$msg = 'date[' . date('Y-m-d H:i:s') . '] ' . $msg . '';return @file_put_contents('sort_err.log', $msg, FILE_APPEND);}}/*End of file sort.php*//*Location htdocs/sort.php */

<?php

require_once('sort.php');

$config = array (
			'arr' => array(23, 22, 41, 18, 20, 12, 200303,2200,1192) , 	//        
			'sort' => 'select',			  				//   : insert, select, bubble, quick
			'debug' => TRUE	    						//   : TRUE, FALSE
		);

$sort = new Sort($config);
//var_dump($config['arr']);
var_dump($sort->display());
		
/*End of php*/