[ create a new paste ] login | about

Link: http://codepad.org/dTyw0Qqx    [ raw code | output | fork ]

C, pasted on Oct 8:
#define n 20
// 宣告標頭檔
#include <stdio.h>
// 宣告函式原型
// 利用傳址的方式把Array A傳進bubble_sort函式中
void bubble_sort(int *A);
// 利用傳參考的方式把Value1,Value2兩個變數,傳入swap函式中
void swap(int *Value1, int *Value2);
void main(void)
{
	int i;
	int A[n]={1,21,0,47,60,
	15,84,65,77,88,
	100,93,8,17,36,
	5,24,63,72,20};
	// 排序前的資料
	printf(" Data before sorting \n");
	for(i=0;i<n;i++)
		printf("%d,",A[i]);
	printf("\n");
	bubble_sort(A);
	// 排序後的資料
	printf(" Data after sorting \n");
	for(i=0;i<n;i++)
		printf("%d,",A[i]);
	printf("\n");
}
void bubble_sort(int *A)
{
	int i,j,xflag=0;
	for(i=0;(i<=n-2)&&(xflag==0);i++)
	{
		xflag=1;
		for(j=0;j<=n-i-2;j++)
		{
			if(A[j]>A[j+1])
			{
				swap(&A[j],&A[j+1]);
				xflag=0;
			}
		}
	}
}
void swap(int *Value1, int *Value2)
{
	int temp;
	temp = *Value2;
	*Value2 = *Value1;
	*Value1 = temp;
}


Output:
1
2
3
4
 Data before sorting 
1,21,0,47,60,15,84,65,77,88,100,93,8,17,36,5,24,63,72,20,
 Data after sorting 
0,1,5,8,15,17,20,21,24,36,47,60,63,65,72,77,84,88,93,100,


Create a new paste based on this one


Comments: