輸入10個數,用冒泡法實現排序。

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>

//實現兩個數的交換
void swap(int *a,int *b)
{
	int temp=*a;
	*a=*b;
	*b=temp;
}

#define Length 10  //定義一個常量

//輸入10個數,並用冒泡法實現排序
void main(void)
{
	int index=0;
	int Nums[Length];
	int b,c;
	for ( index = 0; index < Length; index++)
	{
		printf("請輸入一個數:\r\n");
		scanf("%d",&Nums[index]);
		getchar();
	}
	//實現,最大的在左邊
	for ( b = 0; b < Length-1; b++) //對比的總次數要減一次
	{
		for ( c = 0; c < Length-1-b; c++) //每個數都要與後邊的一堆數進行對比
		{
			if(Nums[c]<Nums[c+1])
			{
				swap(&Nums[c],&Nums[c+1]);
			}
		}
	}
	printf("排序後輸出的結果:\r\n");
	//輸出排序後的結果
	for ( index = 0; index < Length; index++)
	{
		printf("%d\r\n",Nums[index]);
	}
	system("pause");
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章