PHP sorting algorithm class Instance _php tutorial

Source: Internet
Author: User

PHP Sorting Algorithm class instance


The example in this paper describes the PHP sorting algorithm class. Share to everyone for your reference. Specific as follows:

PHP implementations of the four sorting algorithms:

1) The basic idea of inserting a sort (insertion sort) is:

Insert a record to be sorted each time, by its keyword size, into the appropriate location in the previously sorted sub-file until all records are inserted.

2) The basic idea of selecting sort (Selection sort) is:

Each trip selects the smallest record of the keyword from the record to be sorted, placing the order at the end of the ordered sub-file until all records have been sorted.

3) The basic idea of bubble sorting is:

22 Compare the keywords for the records to be sorted, and find that the two records are exchanged in reverse order until there are no reversed records.

4) Fast sorting is essentially the same as bubbling sort, which is an application that belongs to the Exchange sort. So the basic idea is the same as the bubble sort above.

1. sort.php files are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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;

/**

* Constructor function

*

* @param array For example:

$config = Array (

' arr ' = = Array (22,3,41,18),//arrays value to sort

' Sort ' = ' insert ',//Possible values: Insert, select, Bubble, quick

' Debug ' = = TRUE//Possible value: true, FALSE

)

*/

Public function __construct ($config = Array ()) {

if (count ($config) > 0) {

$this->_init ($config);

}

}

/**

* Get sort results

*/

Public Function display () {

return $this->arr;

}

/**

* Initialization

*

* @param array

* @return BOOL

*/

Private Function _init ($config = Array ()) {

Parameter determination

if (!is_array ($config) OR count ($config) = = 0) {

if ($this->debug = = = TRUE) {

$this->_log ("Sort_init_param_invaild");

}

return FALSE;

}

Initializing member variables

foreach ($config as $key = = $val) {

if (Isset ($this-$key)) {

$this $key = $val;

}

}

Call the corresponding Member method to complete the sort

$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;

}

/**

* Insert Sort

*

* @param array

* @return BOOL

*/

Private Function Insert_sort ($arr) {

Parameter determination

if (! Is_array ($arr) OR count ($arr) = = 0) {

if ($this->debug = = = TRUE) {

$this->_log ("Sort_array (insert) _invaild");

}

return FALSE;

}

Specific implementation

$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;

}

/**

* Select sort

*

* @param array

* @return BOOL

*/

Private Function Select_sort ($arr) {

Parameter determination

if (! Is_array ($arr) OR count ($arr) = = 0) {

if ($this->debug = = = TRUE) {

$this->_log ("Sort_array (SELECT) _invaild");

}

return FALSE;

}

Specific implementation

$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;

}

/**

* Bubble Sort

*

* @param array

* @return BOOL

*/

Private Function Bubble_sort ($arr) {

Parameter determination

if (! Is_array ($arr) OR count ($arr) = = 0) {

if ($this->debug = = = TRUE) {

$this->_log ("Sort_array (Bubble) _invaild");

}

return FALSE;

}

Specific implementation

$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;

}

/**

* Quick Sort

*

* @param array

* @return BOOL

*/

Private Function Quick_sort ($arr) {

Specific implementation

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

}

/**

* Log records

*/

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 files are as follows:

?

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),

Array values that need to be sorted

' Sort ' = ' select ',

Possible values: Insert, select, Bubble, quick

' Debug ' = TRUE

Possible values: TRUE, FALSE

);

$sort = new sort ($config);

Var_dump ($config [' arr ']);

Var_dump ($sort->display ());

/*end of php*/

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1018376.html www.bkjia.com true http://www.bkjia.com/PHPjc/1018376.html techarticle PHP Sorting Algorithm Class Example This article describes the PHP sorting algorithm class. Share to everyone for your reference. The following: Four kinds of sorting algorithm PHP implementation: 1) Insert sort (Insertion sort ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.