# Sorting algorithm of PHP

Time：2020-6-24

*For algorithm, it’s a headache for every little partner, but why study algorithm?

Algorithm is the foundation. Algorithm can improve intelligence. I think these two points are worth our time to learn. Don’t give up. I won’t. I will memorize it first and then understand it slowly. Here is my own summary sorting algorithm:

1. Bubble sorting(buttle Sort)

function buttleSort(\$arr){

for(\$i=0; \$i

for(\$j=0; \$j

if(\$arr[\$j] > \$arr[\$j+1]){

\$temp = \$arr[\$j];

\$arr[\$j] = \$arr[\$j+1];

\$arr[\$j+1] = \$temp;

}

}

}

return \$arr;

}

2. select sort

function selectionSort(){

for(\$i=0; \$i

\$minIndex = \$i;

for(\$j=\$i+1; \$j

if(\$arr[minIndex] > \$arr[\$j]){

\$minIndex = \$j;

}

}

\$temp = \$arr[\$i];

\$arr[\$i] = \$arr[\$minIndex];

\$arr[\$minIndex] = \$temp;

}

return \$arr;

}

3. Insert sort

function insertionSort(\$arr){

for(\$i=1; \$i

\$preIndex = \$i-1;

\$current = \$arr[\$i];

while(\$preIndex >= 0 && \$arr[\$preIndex] > \$current){

\$arr[\$preIndex+1] = \$arr[\$preIndex];

\$preIndex–;

}

\$arr[\$preIndex+1] = \$current;

}

return \$arr;

}

To be continued