Curriculum design data structures - sorting exchange operations

Topics requirements:
Comparative exchange sorting
[] Basic requirements
(1) to generate a set of random numbers is stored in a sequential manner, and outputs.
(2) are one-way and two-way bubble sort bubble sort method to sort, while the number of statistical algorithms and comparing the number of movements, and displays the results.
(3) using the quick sort sorting, while statistical comparison frequency and the number of movements, the results displayed.
(4) determining whether a set of data ordered.
(5) before all the even-odd adjusted.
(6) Optional: obtaining a set of data values by size exclusion at the k-bit data (ordering not required).

#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])
			{
				mark=true;
				swap(&a1[j],&a1[j-1]);
				move2++;com2++;
			}else com2++;
		}
		i++;//每趟扫描结束都可以在两头确定一个元素的位置
	}
}
void display(int b[]){
	for(int i=0;i<maxsize;i++){
		if(i!=maxsize-1)cout<<b[i]<<" ";//行末空格 
		else cout<<b[i]<<endl;
	}
} 
int get_mid(int b[],int left,int right){
	int pivot=b[left];
	while(left<right){
		while(b[right]>=pivot&&left<right) 
		{
			right--;
			com3++;
		}
		b[left]=b[right];
		com3++;move3++; 
		while(b[left]<=pivot&&left<right) 
		{
			left++;
			com3++;
		}
			b[right]=b[left];
			com3++;move3++;
		}  
	b[left]=pivot;
	move3++;
	return left;
}
void quick_sort(int b[],int left,int right){
	if(left<right){
	int mid=get_mid(b,left,right);
	quick_sort(b,left,mid-1);
	quick_sort(b,mid+1,right);                                                
	}
}
bool judgement_num(){
	int cc1[maxsize],i=0;
	 while(1){
	 	cin>>cc1[i];
	 	if(cc1[i]==-99999)break; 
	 	i++;
	 }
	 for(int j=0;j<i;j++){
	 	cout<<cc1[j]<<" ";
	 }
	 printf("\n");
	int temp=1;
	for(int j=0;j<i-1;j++){
		cout<<cc1[j+1]<<" "<<cc1[j];
		if(cc1[j+1]>cc1[j])cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
		
		if(cc1[j+1]<cc1[j]){
			temp=0;
			break;
		}
	}
	if(temp==1)
		return true;
	else return false;  
}
void change_num(){
	int cc1[maxsize],i=0;
	 while(1){
	 	cin>>cc1[i];
	 	if(cc1[i]==-99999)break; 
	 	i++;
	 }
	 for(int j=0;j<i;j++){
	 	cout<<cc1[j]<<" ";
	 }
	 printf("\n");
	 int mark1=0,mark2=i-1;
	 while(1){
	 	while(cc1[mark1]%2!=0)mark1++;
	 	while(cc1[mark2]%2==0)mark2--;
	 	if(mark1>=mark2)
	 		break;
	 	swap(cc1[mark1],cc1[mark2]);
	 }
	 cout<<"交换成功,现在所有奇数都在偶数的前面"<<endl;
	 for(int j=0;j<i;j++){
	 	cout<<cc1[j]<<" ";
	 }
	 printf("\n");
	 }
int find_max(int q[],int len){
	int max=0,i; 
	for(i=0;i<len;i++){
		if(q[i]>q[max])max=i;
	}	
	return max;
}
void find_k_num(){
	int cc1[maxsize],i=0,k,fre=0;
	 while(1){
	 	cin>>cc1[i];
	 	if(cc1[i]==-99999)break; 
	 	i++;
	 }
	 for(int j=0;j<i;j++){
	 	cout<<cc1[j]<<" ";
	 }
	 printf("\n");
	 cout<<"请输入K值"<<endl;
	 cin>>k;
	 while(fre<k-1){
	 	int ii=find_max(cc1,i);
	 	cc1[ii]=-77777;
	 	fre++;
	 }
	 int kk=find_max(cc1,i);
	 cout<<"排名第"<<k<<"位的数字为"<<cc1[kk]<<endl; 
}

int main(){
 	srand((unsigned)time(NULL));//生成随机数 
 	cout<<"原本随机生成数组为:"; 
	for(int i=0;i<maxsize;i++){
		a[i]=rand()%100;
		a1[i]=a[i];a2[i]=a[i];a3[i]=a[i];
		if(i!=19)cout<<a[i]<<" ";//行末空格 
		else cout<<a[i]<<endl;
	}
	cout<<"单向起泡排序数组为:";
	bubble_sort();
	display(a);
	
	cout<<"单向起泡比较次数为"<<com1<<endl;
	cout<<"单向起泡移动次数为"<<move1<<endl; 
	
	
	cout<<"原本随机生成数组为:"; 
	display(a1);
	cout<<"双向起泡排序数组为:";
	Double_Bubble_sort();
	display(a1);
	cout<<"双向起泡比较次数为"<<com2<<endl;
	cout<<"双向起泡移动次数为"<<move2<<endl; 
	cout<<"原本随机生成数组为:";
	display(a2);
	cout<<"快速排序数组为:";
	quick_sort(a2,0,maxsize-1);
	display(a2);
	cout<<"双向起泡比较次数为"<<com3<<endl;
	cout<<"双向起泡移动次数为"<<move3<<endl; 
	cout<<"请输入需要判断有序的数组(顺序)(-99999结束)"<<endl;
	bool aaa=judgement_num();
	if(aaa) cout<<"该数组为有序数组"<<endl;
	else cout<<"该数组为无序数组"<<endl;
	cout<<"请输入需要判调换奇数偶数的数组(顺序)(-99999结束)"<<endl;
	change_num(); 
	cout<<"请输入需要查找第k位的数组(顺序)(-99999结束)"<<endl;
	find_k_num();
	return 0;
}
Released three original articles · won praise 4 · Views 1207

Guess you like

Origin blog.csdn.net/weixin_44965308/article/details/104914857