📄 bubblesort(冒泡排序).cpp
字号:
// bubblesort(冒泡排序).cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<assert.h>
using namespace std;
struct node
{
int key;
int oth;
};
void bubblesort(node r[],int n);
int _tmain(int argc, _TCHAR* argv[])
{
node s[11];
ifstream infile("D:\\1.txt");
assert(infile);
for(int i=1;i<11;i++)
infile>>s[i].key;
ofstream outfile("D:\\1.txt",ios_base::app);
assert(outfile);
bubblesort(s,10);
outfile<<endl;
for(int i=1;i<11;i++)
outfile<<s[i].key<<' ';
outfile<<endl;
return 0;
}
void bubblesort(node r[],int n)///冒泡排序是交换排序的一种,效率还是比较的高,因为有个tag,当发现没有交换的项时,排序就停止了,已经是排好序的了,是稳定的排序
{
int tag=0;
int i=1,j;
do{
tag=0;
for(j=n;j>i;j--)
if(r[j].key<r[j-1].key)
{
node x=r[j];
r[j]=r[j-1];
r[j-1]=x;
tag=1;
}
i++;
}while(tag==1&&i<n);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -