|
|
- #include <stdio.h>
- //#define DIM 7
- #define DIM 14
- int main()
- {
- //int array[DIM] = {44,55,12,42,94,13,57};
- int array[DIM] = {29, 27, 38, 23, 0, 24, 26, 28, 25, 33, 52, 40, 43, 36};
- int index;
- int i;
- int temp;
- int flag = 1;/*1 means sorting is needed, 0 means otherwise.*/
- printf("The original array is:\n");
- for (i=0; i<=DIM-1; i++)
- printf("%d ", array[i]);
- putchar('\n');
- for (index=DIM-1; index>=1 && flag; index--) {
- /*'index' means to which array element should the search for the largest be carried out*/
- /*the last search deals with only two elements, thus 'index' is 1 at that time*/
- flag=0;
- for (i=0; i<=index; i++) {
- if (array[i] > array[i+1]) {
- temp = array[i+1];
- array[i+1] = array[i];
- array[i] = temp;
- flag = 1;
- }
- }
- }
- for (i=0; i<=DIM-1; i++)
- printf("%d ", array[i]);
- putchar('\n');
- }
复制代码
作冒泡算法实验的。如果用7 个元素,输出正常;用14个元素,输出就有问题。
:confused: |
|