⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bubblesort.cpp

📁 冒泡排序
💻 CPP
字号:
#include <stdio.h>
#include <iostream.h>

#define MAX 255
int R[MAX];

void Bubble_Sort(int n)
{  
     int i,j;
     int exchange; /* 交换标志 */
     for(i=1;i<n;i++){  
       exchange=0; /* 本趟排序开始前,交换标志应为假 */
       for(j=n-1;j>=i;j--)  
        if(R[j+1]<R[j]){/* 交换记录 */
          R[0]=R[j+1];  
          R[j+1]=R[j];
          R[j]=R[0];
          exchange=1; /* 发生了交换,故将交换标志置为真 */
         }
       if(!exchange) /* 本趟排序未发生交换,提前终止算法 */
	     return;
     }
}

void main()
{
	int i,n;
 
	cout << "Please input total element number of the sequence:" ;
	cin >> n;
	if(n<=0||n>MAX)
	{
		cout << "n must more than 0 and less than" << MAX;
	}
	cout << "Please input the elements one by one:" << endl;
	for(i=1;i<=n;i++)
		cin >> R[i];
	cout << "The sequence you input is:";
	for(i=1;i<=n;i++)
		cout << "   " << R[i];
	Bubble_Sort(n);
	cout  << endl << endl;
	cout << "The sequence after bubble_sort is:";
	for(i=1;i<=n;i++)
		cout << "   " <<  R[i];
	cout << endl << endl;
	return;
 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -