Bubble sort (Bubble sort)
is a relatively simple sorting algorithm in the field of computer science.
It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted.
The name of the algorithm is because the larger the element will slowly "float" to the top of the sequence, hence the name.
algorithm principleThe bubbling Sort algorithm works as follows: (from back to forward)
- Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
- Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.
- Repeat the above steps for all elements, except for the last one.
- Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
algorithm Stability
The bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal, I think you will not be bored to exchange them again, if the two equal elements are not adjacent, then even through the preceding 22 exchange two adjacent together, this time will not be exchanged, so the same elements of the order has not changed, so bubble sort is a stable sorting algorithm.
Two-Part search method
Binary search also known as binary lookup, the advantages are less than the number of comparisons, Find Fast, the average performance is good, the disadvantage is that the unknown origin table is ordered table, and insert delete difficult. Therefore, the binary lookup method is suitable for an ordered list that does not change frequently and finds frequent. First, suppose that the elements in the table are arranged in ascending order, comparing the keywords in the middle position of the table with the lookup keywords, and if they are equal, the lookup succeeds; otherwise, the table is divided into the front and the last two sub-tables with the intermediate positional records, and if the middle position record keyword is greater than the Find keyword, the previous child Otherwise, find the latter child table further. Repeat the process until you find a record that satisfies the criteria, make the lookup successful, or until the child table does not exist, the lookup is unsuccessful at this time.
Algorithm requirements
- Sequential storage structure must be used
2. Must be sorted by keyword size. Algorithm
The basic idea of binary search is to divide n elements into roughly equal two parts, take A[N/2] and X to compare, if X=A[N/2], then find X, the algorithm aborts, if X<A[N/2], as long as the left half of the array a continues to search X, if X>A[N/2], Search for x in the right half of the array A.
It takes full advantage of the order relationship between elements and uses the divide-and-conquer strategy to complete the search task in the worst case with O (log n). Its basic idea is that n elements are divided into roughly the same number of two halves, take A[N/2] and to find the X to compare, if X=A[N/2] Find x, the algorithm terminates. If X<A[N/2], we simply continue to search for x in the left half of array a (assuming that the array elements are in ascending order). If X>A[N/2], then we just continue to search for x in the right half of array A.
Bubble Sort method and binary search method