//題目: 撰寫雙向起泡排序演算法。
#include <iostream>
using namespace std;
#include<stdlib.h>
//函式結果狀態代碼
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status; //一般作為函式回傳值的型別,其值是代表函式結果狀態的代碼
#define MAXSIZE 64 //順序表的最大長度(陣列的長度)
typedef int KeyType; //關鍵字的型別
typedef char* InfoType; //其它資料項的型別
typedef struct
{
KeyType key; //排序關鍵字
InfoType otherinfo; //其它資料項(衛星資料)
}RedType; //資料元素(或記錄)的型別
//待排記錄的順序存盤結構(即順序表的型別)
typedef struct
{RedType r[MAXSIZE+1]; //存盤待排記錄的陣列(r[0]閑置或當做哨卡)
int length; //存盤待排記錄的長度
}SqList; //順序表的型別
//輸入待排記錄
void Create_Sq(SqList &L)
{
int i,n;
cout<<"請輸入資料個數,不超過"<<MAXSIZE<<"個。"<<endl;
cin>>n; //輸入個數
while(n>MAXSIZE)
{
cout<<"個數超過上限,不能超過"<<MAXSIZE<<",請重新輸入"<<endl;
cin>>n;
}
cout<<"請輸入待排序的資料(只需輸入各記錄的關鍵字):\n";
for(i=1;i<=n;i++)
cin>>L.r[i].key;
L.length=n;
}
//輸出
void show(SqList L)
{
int i;
for(i=1;i<=L.length;i++)
cout<<L.r[i].key<<" ";
cout<<endl;
}
void abc( SqList &L )
// 對L.r[1..n]進行雙向冒泡排序。即相鄰兩趟向兩個相反方向起泡
{int i=1, j; // 設標記
RedType temp;
j=L.length;
while(i<j)
{
for(j=L.length;L.r[j].key<0;j--)
for(i=1;L.r[i].key>0;i++)
if(i<j)
{temp=L.r[i];
L.r[i]=L.r[j];
L.r[j]=temp;}
}//演算法結束
void main()
{
SqList L;
Create_Sq(L);
cout<<endl;
cout<<"排序前的狀態為:"<<endl;
show(L);
cout<<endl;
abc(L);
cout<<"排序后的結果為:"<<endl;
show(L);
system("pause>nul");
}
uj5u.com熱心網友回復:
看程式。。。字不重要uj5u.com熱心網友回復:
偶遇到類似問題都是用“每次用/*...*/注釋掉不同部分再重新編譯,直到定位到具體語法出錯的位置。”
的方法解決的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/116818.html
標籤:基礎類
