/* * File Name: BubbleSort.c * Author: MH * Since 2013/02/22 * Toolkit: Dev C++ 4.9.9.9 */ #include <stdio.h> #include <stdlib.h> void swap (int *a, int *b){ int tmp; tmp = *a; *a = *b; *b = tmp; } int main(int argc, char *argv[]){ int i, j, n=5; int list[5] = {10, 7, 19, 5, 6}; printf("Before Bubble Sort\n"); for (i=0; i<n; i++) printf("list[%d] = %d\n", i, list[i]); for (i=0; i<n-1; i++) for (j=0; j<n-i-1; j++) if (list[j]>list[j+1]) swap(&list[j], &list[j+1]); printf("\nAfter Bubble Sort\n"); for (i=0; i<n; i++) printf("list[%d] = %d\n", i, list[i]); system("PAUSE"); return 0; }
搜尋此網誌
[C/C++][Bubble Sort]
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言