PHP Interview finishing (b) data structure basis - the usual sort

2019-09-0119:04:39

PHP graduating students, on the Internet read a lot of interviews and information related stuff, here recorded, some places may be related to the original others, but at that time did not have time to add notes, if offended, please tell me, I will add the source is acknowledged. (Learning on the way there are a lot of mistakes, and ignoring the place, I hope you will correct me, thank you !!!)

 

Data structure here is really super important, regret sophomore did not take time to learn, did not take note of it, many of which are know these, I do not know why, so write this, intended as a study notes (the first to write ordering and tree) welcome to correct and supplement.

 

Common sorting algorithm

 

Source: Figure

 

First, the bubble sort (taken from Wikipedia)

It is a simple sorting algorithm, which repeatedly visited the number of columns to be sorted, a comparison of two elements, if they put them in the wrong order switching over. The number of visits to the column work is carried out repeatedly, there is no need to exchange know, that the number of columns already sorted completed. The origin of the name of the algorithm is because the smaller elements via exchange, slowly "float" to the top of the columns.

My understanding is relatively adjacent, then swap.

 

 

 (Sophomore textbook)

 

 

 

 

 

 

 

 

 (Screenshot from Wikipedia)

In the best case, the time complexity is O (n)

In the worst case, the time complexity is O (n- 2 )

On the average, the same is O (n- 2 )

It is a stable sorting algorithm

 

Second, the quick sort

Quick sort using a divide and conquer strategy to put a sequence into smaller and larger of the two sequences, then recursively sort of two sub-sequences

I understand: from the beginning to the middle to find the two, if the exchange position occurs, change direction until i, j equal.

 

 

 

 

 

 In the best case, the time complexity is O (nlog 2 n-)

 In the worst case, the time complexity is O (n- 2 )

 In the average case, the time complexity is O (log 2 n-)

It is an unstable sorting method.

 

Third, insertion sort

By constructing an ordered sequence, for unsorted data, scanned data in sorted from back to front, find the corresponding position of the insert.

我的理解:分成两部分,有序和无序,在无序里按顺序取出,在有序里插入。直到无序里没有

 

 

 

 在最好的情况下,时间复杂度为O(n)

 在最坏的情况下,时间复杂度为O(n2)

 在平均的情况下,时间复杂度为O(n2)

是一种稳定的排序方法。

 

四、选择排序

首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

我的理解:挑最小或者最大的放有序里,在从无序里找,在放有序里。(第一次时候是交换位置的)

 

 

 

 在最好的情况下,时间复杂度为O(n2)

 在最坏的情况下,时间复杂度为O(n2)

 在平均的情况下,时间复杂度为O(n2)

是一种不稳定的排序算法。

 

五、归并排序

归并操作(merge),也叫归并算法,指的是将两个已经排序的序列合并成一个序列的操作。归并排序算法依赖归并操作。

我的理解:拆完排,在合并排

书上这一节有两小节,二路归并排序的非递归实现,二路归并排序的递归实现。

 

二路归并排序的非递归实现

 

 

 

二路归并排序的递归实现

 

 

 

 

 

在最好的情况下,时间复杂度为O(nlog2n)

在最坏的情况下,时间复杂度为O(nlog2n)

咋平均的情况下,时间复杂度为O(nlog2n)

是一种稳定的排序算法。

 

 

 

未完…………………………

 

Guess you like

Origin www.cnblogs.com/zhaoguofeng/p/11443201.html