站在巨人的肩膀是骗人的
2013年(28)
分类: C/C++
2013-02-20 11:47:38
描述一下冒泡排序:
代码:
#includevoid bubble_sort(int* s, size_t n) { size_t i, j; bool exchanged; for (i = 0; i < n; ++i) { exchanged = false; for (j = n - 1; j > i; --j) if (s[j] < s[j - 1]) { exchanged = true; int tmp = s[j]; s[j] = s[j - 1]; s[j - 1] = tmp; } if (!exchanged) break; } } int main() { int s[] = {44, 2, 5, 7, 10, 1}; bubble_sort(s, sizeof(s) / sizeof(int)); return 0; }