1)
这是伪代码,可以帮助您理解:
FOR J=1 TO N-1 {
FOR I=1 TO N-J {
IF A[I]>A[I+1] THEN SWAP A[I],A[I+1]
NEXT I
}
NEXT J
}
2)
至于过程中的冒泡排序..这是一个简单的例子:
1) At first we compare the first two elements.
If the 1st el. is bigger (or equal) than the next el. - we swap them.
If the 1st el. is smaller - we do nothing.
(The smallest elements will be closer to the top & biggets to the bottom)
Then we compare 2nd and 3rd elements, them 3rd and 4th etc.
Compare all the elements until the last in array.
/* In this cycle, the biggest element will go to the bottom. */
2) Then we "forget" the last (the biggest) element and repeat the same again.
3) Repeat 1) and 2) successively until the end.
/* After all, all the elements will be sorted now: */
/* from the smallest to the largest. */
EXAMPLE:
Suppose we have 4 elements: 8, 6, 2, 1. That's how we will sort them:
1st cycle:
8, 6, 2, 1
v v
8 is bigger than 6, so we swap them
6, 8, 2, 1
v v
8 is bigger than 2, so we swap them
6, 2, 8, 1
v v
8 is bigger than 1, so we swap them
6, 2, 1, 8
v
The biggest element is at the bottom now.
2nd cycle:
6, 2, 1, 8
v v
6 is bigger than 2, so we swap them
2, 6, 1, 8
v v
6 is bigger than 1, so we swap them
2, 1, 6, 8
v v
6 will always be smaller or equal to 8, so we use ...
for (inner = 0; inner < (N-outer); inner++)
^^^^^^^ ... this expression to avoid
unnecessary actions.
3rd cycle:
2, 1, 6, 8
v v
2 is bigger than 1, so we swap them
The are 4 elements but we do (4-1)= 3 cycles:
for(outer = 0; outer < (N-1); outer++)
^^^
3)
现在,想象一下N
= 10
、outer
=J
和inner
= I
:
for(J = 0; J < (N-1) ; J++) {
didSwap = 0;
for (I = 0; I < (N-J); I++)
{
if (nums[I] < nums[I + 1])// at the beginning, here J = 0 and I = 1;
{ // then J = 0 and I = 2 etc.
temp = nums[I]; // It compares the first element with the
nums[I] = nums[I + 1]; // othe ones and swaps them so more lightweight
nums[I + 1] = temp; // (smaller, light) element will move higher.
didSwap = 1; // Just like a bubble.
}
}
if (didSwap == 0)
{
break;
}
}
更新:
4)
break
在完成之前,您不能进行排序循环!
看下面的代码。它确实适合冒泡排序伪代码(在此答案的顶部):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10 // Here you can change the number of elements that
// will be sorted.
int main()
{
int ctr, inner, outer, didSwap, temp;
int nums[N];
time_t t;
srand(time(&t));
for (ctr = 0; ctr < N; ctr++)
{
nums[ctr] = (rand() % 99) + 1; // Filling the elements with random
} // values from 1 to 99.
puts("\nHere is the list before the sort:");
for (ctr=0; ctr < N; ctr++) {
printf("%d\n", nums[ctr]);
}
didSwap = 0;
for(outer = 0; outer < (N-1); outer++) {
for (inner = 0; inner < (N-outer); inner++)
{
if (nums[inner] >= nums[inner + 1]) // notice that there is `>=`
{ // instead of `>`.
temp = nums[inner]; // This will exchange also
nums[inner] = nums[inner + 1]; // equal elements so the
nums[inner + 1] = temp; // sorting will work correctly.
}
}
didSwap = 1; // Change `didSwap` only once --> after all cycles
// and all swappings: changing it's value after each
// swapping is a waste of machine's resources.
}
/* I can't understand why do you want to use this variable, but here it is. */
printf(" >>> didSwap = %d <<<\n", didSwap);
puts("\nHere is the list after the sort:");
for(ctr = 0; ctr < N; ctr++) {
printf("%d\n", nums[ctr]);
}
return 0;
}