Traditional Culture Encyclopedia - Weather inquiry - The bubbling method in C language has never been understood. Please make it simple and detailed according to the chart. That's right. Ten points.

The bubbling method in C language has never been understood. Please make it simple and detailed according to the chart. That's right. Ten points.

There are two ways of foaming, one is that the maximum element sinks in each cycle; One is to promote the smallest element in each outer loop. The algorithm you give here is the latter.

The outer loop corresponds to the loop of I in the algorithm you gave, from 1 to n- 1, and the loop is n- 1 times. For each value of I, there will be a smallest element floating to the position of i- 1, and the elements before i- 1 have been sorted from small to large.

For example, when i= 1, pj points to the last element 1, and then judge the size relationship between the element before pj and the element pointed by pj. If pj- 1 points to an element greater than that pointed by pj, swap two elements, and then pj points to the position of the previous element until pj & gtP+i- 1, where p points to the first address of the array. When i= 1, p+i- 1 is p+0, pointing to the first address of the array. At this point i= 1, so the smallest element in the array will float to the first I-65438+ at the end of this period. Generally speaking, the value of each outer loop I is the number of sequences arranged in front of the array from small to large+1. For example, when i=3, it means that while the outer loop is going on, two elements have been arranged in order, and at the end of the loop, three elements have been arranged in order, from small to large, at the first place in the array.