PHP sorting algorithm class _ PHP Tutorial

Source: Internet
Author: User
Tags array example
PHP sorting algorithm instance. PHP sorting algorithm class example this article describes the PHP sorting algorithm class. Share it with you for your reference. Specific steps are as follows: PHP implementation of the four sorting algorithms: 1) Insertion sorting (InsertionSort PHP sorting algorithm class example)

This example describes the PHP sorting algorithm class. Share it with you for your reference. The details are as follows:

PHP implementation of the four sorting algorithms:

1) The basic idea of Insertion Sort is:

Each time you insert a record to be sorted, insert it to the appropriate position in the subfile according to its keyword size until all records are inserted.

2) The basic idea of Selection Sort is:

Select the record with the smallest keyword from the record to be sorted, and put it at the end of the sorted subfile until all records are sorted.

3) The basic idea of bubble sorting is:

Compare the keywords of the records to be sorted. if the order of the two records is the opposite, the records are exchanged until there is no reverse order.

4) fast sorting is essentially the same as bubble sorting. it is an application of exchange sorting. So the basic idea is the same as the above bubble sorting.

1. the sort. php file is as follows:

?

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

Bytes

/**

*

* @ Author quanshuidingdang

*/

Class Sort {

Private $ arr = array ();

Private $ sort = 'insert ';

Private $ marker = '_ sort ';

Private $ debug = TRUE;

/**

* Constructor

*

* @ Param array example:

$ Config = array (

'Arr' => array (22,3, 41,18), // array value to be sorted

'Sort '=> 'insert', // possible values: insert, select, bubble, and quick

'Debug' => TRUE // possible values: TRUE, FALSE

)

*/

Public function _ construct ($ config = array ()){

If (count ($ config)> 0 ){

$ This-> _ init ($ config );

}

}

/**

* Obtain the sorting result

*/

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;

}

// Initialize the member variable

Foreach ($ config as $ key => $ val ){

If (isset ($ this-> $ key )){

$ This-> $ key = $ val;

}

}

// Call the corresponding member method to complete sorting

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

*

* @ 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 sorting

*

* @ 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 sorting

*

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

}

/**

* Fast sorting

*

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

}

/**

* Logging

*/

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. the sort_demo.php file is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Bytes

Require_once ('sort. php ');

$ Config = array (

'Arr' => array (23, 22, 41, 18, 20, 12,200 303,2200, 1192 ),

// Array values to be sorted

'Sort '=> 'select ',

// Possible values: insert, select, bubble, and quick

'Debug' => TRUE

// Possible values: TRUE and FALSE

);

$ Sort = new Sort ($ config );

// Var_dump ($ config ['arr']);

Var_dump ($ sort-> display ());

/* End of php */

I hope this article will help you with php programming.

Examples in this article describes the PHP sorting algorithm class. Share it with you for your reference. Specifically: PHP implementation of the four Sort algorithms: 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.