PHP排序演算法類執行個體_PHP教程

來源:互聯網
上載者:User

PHP排序演算法類執行個體


  本文執行個體講述了PHP排序演算法類。分享給大家供大家參考。具體如下:

  四種排序演算法的PHP實現:

  1) 插入排序(Insertion Sort)的基本思想是:

  每次將一個待排序的記錄,按其關鍵字大小插入到前面已經排好序的子檔案中的適當位置,直到全部記錄插入完成為止。

  2) 選擇排序(Selection Sort)的基本思想是:

  每一趟從待排序的記錄中選出關鍵字最小的記錄,順序放在已排好序的子檔案的最後,直到全部記錄排序完畢。

  3) 冒泡排序的基本思想是:

  兩兩比較待排序記錄的關鍵字,發現兩個記錄的次序相反時即進行交換,直到沒有反序的記錄為止。

  4) 快速排序實質上和冒泡排序一樣,都是屬於交換排序的一種應用。所以基本思想和上面的冒泡排序是一樣的。

  1. sort.php檔案如下:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179



/**

*

* @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);

}

/**

* 日誌記錄

*/

private function _log($msg) {

$msg = 'date[' . date('Y-m-d H:i:s') . '] ' . $msg . '\n';

return @file_put_contents('sort_err.log', $msg, FILE_APPEND);

}

}

/*End of file sort.php*/

/*Location htdocs/sort.php */

  2. sort_demo.php檔案如下:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14



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*/

  希望本文所述對大家的php程式設計有所協助。

http://www.bkjia.com/PHPjc/1018376.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1018376.htmlTechArticlePHP排序演算法類執行個體 本文執行個體講述了PHP排序演算法類。分享給大家供大家參考。具體如下: 四種排序演算法的PHP實現: 1) 插入排序(Insertion Sort...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.