/*
Author:Albert Tesla Wizard
TIme:2020/11/22 15:36
*/
#include<bits/stdc++.h>
#define MAXSIZE 1000
using namespace std;
struct Sqlist
{
int* elem;
int length;
};
int create(Sqlist&L,int a[],int n)
{
L.elem=new int[MAXSIZE];
L.length=0;
for(int i=0;i<n;i++){L.elem[i+1]=a[i];L.length++;}
}
void print(Sqlist L)
{
for(int i=1;i<=L.length;i++)cout<<L.elem[i]<<" ";
cout<<'\n';
}
int BubbleSort1(Sqlist&L)
{
for(int i=1;i<=L.length;i++)
for(int j=1;j<=L.length-i;j++)
{
if(L.elem[j]>L.elem[j+1])
{
int temp=L.elem[j];
L.elem[j]=L.elem[j+1];
L.elem[j+1]=temp;
}
}
}
int BubbleSort2(Sqlist&L)
{
bool flag=true;
int i=L.length-1;
while(flag&&i>0)
{
flag=false;
for(int j=1;j<=i;j++)
if(L.elem[j]>L.elem[j+1])
{
flag=true;
int temp=L.elem[j];
L.elem[j]=L.elem[j+1];
L.elem[j+1]=temp;
}
i--;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
Sqlist L;
int a[10]={2,1,5,4,3,9,6,7,8,10};
create(L,a,10);
print(L);
BubbleSort2(L);
print(L);
return 0;
}
顺序表实现冒泡排序
最新推荐文章于 2023-09-28 06:00:00 发布