题目要求:
交换排序的比较
【基本要求】
(1)生成一组随机数以顺序方式存储,并输出。
(2)分别利用单向起泡排序法和双向起泡排序法进行排序,同时统计各算法比较次数和移动次数,并显示结果。
(3)利用快速排序法排序,同时统计比较次数和移动次数,显示结果。
(4)判断一组数据是否有序。
(5)将所有奇数调到偶数之前。
(6)选做:求出一组数据中按值大小排在第k位的数据(不需要排序)。
#include<iostream>
#include<stdio.h>
#include<time.h>//用于生成随机数
#include<stdlib.h>
using namespace std;
#define maxsize 20
#define elemtype int
int a[maxsize],a1[maxsize],a2[maxsize],a3[maxsize];
int move1=0,com1=0,move2=0,com2=0,com3=0,move3=0;
void swap(int *a,int *b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void bubble_sort(){
int i,j,temp;
bool mark=true;
for(i=0;i<maxsize-1;i++){
temp=false;
for(j=0;j<maxsize-1-i;j++){
if(a[j+1]<a[j]){
swap(&a[j+1],&a[j]);
move1++;
com1++;
mark=true;
}
else{
com1++;
}
if(mark==false)
break;
}
}
}
void Double_Bubble_sort(){
int i=0,j;
bool mark=true;
while(mark)
{
mark=false;
for(j=i;j<maxsize-i-1;j++)//向右扫描
{
if(a1[j]>a1[j+1])
{
mark=true;
swap(&a1[j],&a1[j+1]);
move2++;com2++;
}else com2++;
}
for(j=maxsize-i-1;j>i;j--)//向左扫描
{
if(a1[j]<a1[j-1])
{