【问题描述】对待排序序列使用直接插入排序算法或冒泡排序算法或简单选择排序算法或快速排序算法进行排序,输出每一趟排序后的结果
【输入形式】序列元素个数
序列
【输出形式】 每一趟排序后的结果
【样例输入】
6
18 6 28 93 46 55
1.1直接插入排序算法
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 20
#define OVERFLOW -1
#define OK 1
typedef int Status;
typedef int KeyType;
typedef int InfoType;
typedef int ElemType;
typedef struct{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct{
RedType *r;
int length;
}SqList;
Status InitList(SqList &L){
L.r=new RedType[MAXSIZE+1];
if(!L.r)exit(OVERFLOW);
L.length=0;
return OK;
}
void InsertSort(SqList &L,int n){
int i,j;
for(i=2;i<=L.length;i++){
if(L.r[i].key<L.r[i-1].key){
L.r[0]=L.r[i];
L.r[i]=L.r[i-1];
for(j=i-2;L.r[0].key<L.r[j].key;j--)
L.r[j+1]=L.r[j];
L.r[j+1]=L.r[0];
}
for(j=1;j<=n;j++){
cout<<L.r[j].key<<" ";
}
cout<<endl;
}
}
int main(){
SqList L;
InitList(L);
int i,n;
cin>>n;
for(i=1;i<=n;i++){
cin>>L.r[i].key;
L.length++;
}
InsertSort(L,n);
return 0;
}
1.2直接插入排序算法
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
using namespace std;
#define MAXSIZE 20
typedef int KeyType;
typedef char InfoType;
typedef struct{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct{
RedType r[MAXSIZE+1];
int length;
}SqList;
void SelectSort(SqList &L){
int i,j,k,m;
RedType t;
for(i=1;i<L.length;++i){
k=i;
for(j=i+1;j<=L.length;++j)
if(L.r[j].key<L.r[k].key) k=j;
if(k!=i)
{
t=L.r[i];L.r[i]=L.r[k];L.r[k]=t;
}
for(m=1;m<=L.length;m++){
cout<<L.r[m].key<<" ";
}
cout<<endl;
}
}
int main(void)
{
SqList L;
int n,i;
cin>>n;
L.length=n;
for(i=1;i<=L.length;i++){
cin>>L.r[i].key;
}
SelectSort(L);
return 0;
}
2.冒泡排序算法
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 20
#define OVERFLOW -1
#define OK 1
typedef int Status;
typedef int KeyType;
typedef int InfoType;
typedef int ElemType;
typedef struct{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct{
RedType *r;
int length;
}SqList;
Status InitList(SqList &L){
L.r=new RedType[MAXSIZE+1];
if(!L.r)exit(OVERFLOW);
L.length=0;
return OK;
}
void BubbleSort(SqList &L,int n){
int i,j,m,flag;
RedType t;
m=L.length-1;
flag=1;
while((m>0)&&(flag==1)){
flag=0;
for(j=1;j<=m;j++)
if(L.r[j].key>L.r[j+1].key){
flag=1;
t=L.r[j];
L.r[j]=L.r[j+1];
L.r[j+1]=t;
}
m--;
for(j=1;j<=n;j++){
cout<<L.r[j].key<<" ";
}
cout<<endl;
}
}
int main(){
SqList L;
InitList(L);
int i,n;
cin>>n;
for(i=1;i<=n;i++){
cin>>L.r[i].key;
L.length++;
}
BubbleSort(L,n);
return 0;
}
3.简单选择排序算法
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 20
#define OVERFLOW -1
#define OK 1
typedef int Status;
typedef int KeyType;
typedef int InfoType;
typedef int ElemType;
typedef struct{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct{
RedType *r;
int length;
}SqList;
Status InitList(SqList &L){
L.r=new RedType[MAXSIZE+1];
if(!L.r)exit(OVERFLOW);
L.length=0;
return OK;
}
void SelectSort(SqList &L,int n){
int i,j,k;
RedType t;
for(i=1;i<L.length;i++){
k=i;
for(j=i+1;j<=L.length;j++)
if(L.r[j].key<L.r[k].key)k=j;
if(k!=i){
t=L.r[i];
L.r[i]=L.r[k];
L.r[k]=t;
}
for(j=1;j<=n;j++){
cout<<L.r[j].key<<" ";
}
cout<<endl;
}
}
int main(){
SqList L;
InitList(L);
int i,n;
cin>>n;
for(i=1;i<=n;i++){
cin>>L.r[i].key;
L.length++;
}
SelectSort(L,n);
return 0;
}
4.快速排序算法
#include<iostream>
#include<stdlib.h>
using namespace std;
#define MAXSIZE 20
#define OVERFLOW -1
#define OK 1
typedef int Status;
typedef int KeyType;
typedef int InfoType;
typedef int ElemType;
typedef struct{
KeyType key;
InfoType otherinfo;
}RedType;
typedef struct{
RedType *r;
int length;
}SqList;
Status InitList(SqList &L){
L.r=new RedType[MAXSIZE+1];
if(!L.r)exit(OVERFLOW);
L.length=0;
return OK;
}
int Partition(SqList &L,int low,int high,int n){
KeyType privotkey;
L.r[0]= L.r[low];
privotkey=L.r[low].key;
while(low<high){
while(low<high&&L.r[high].key>= privotkey)high--;
L.r[low]=L.r[high];
while(low<high&&L.r[low].key<= privotkey)low++;
L.r[high]=L.r[low];
}
L.r[low]=L.r[0];
return low;
}
void QSort(SqList &L,int low,int high,int n){
int pivotloc,j;
if(low<high){
pivotloc=Partition(L,low,high,n);
for(j=1;j<=n;j++){
cout<<L.r[j].key<<" ";
}
cout<<endl;
QSort(L,low,pivotloc-1,n);
QSort(L,pivotloc+1,high,n);
}
}
void QuickSort(SqList &L,int n){
QSort(L,1,L.length,n);
}
int main(){
SqList L;
InitList(L);
int i,n;
cin>>n;
for(i=1;i<=n;i++){
cin>>L.r[i].key;
L.length++;
}
QuickSort(L,n);
return 0;
}