Data structure (bubbling method)

xiaoxiao2021-03-06  43

Bubble Sort: From the backward steps, the size sequence is gradually compared, one of the comparisons, and finally list the arrangement of the order.

If there are 4 digits: 43, 21, 12, 54 to use Bubble Sort

The first first second third trip

43 12 12 12

21 43 21 21

12 21 43 43

54 54 54 54

The sorting of N-1 is performed on any N data. If there is no exchange of recording position in a certain sort, the sequence has been in accordance with keywords.

Sorted. (If the example is indeed, the second and third 趟 are repeated), in order to optimize the code, use FLAG to process!

Bubblesort (R) // Bubbling Sort

RecdType r [N];

{INT I, J,

Flag;

Recdtype temp;

For (i = 0; i

{Flag = true;

For (j = n-1; j> = i; j -) // from the previous scan

IF (r [j 1] .key

{TEMP = R [J 1];

R [J 1] = R [J];

R [J] = TEMP;

Flag = false; // set the switch position

}

IF (flag) breaf; // does not exchange

}

}

转载请注明原文地址:https://www.9cbs.com/read-53003.html

New Post(0)