数据结构与算法-排序

Posted by Zexin Zhang on November 8, 2017

以3的不同余数进行分类、排序


测试截图

img

//实验8

//张泽鑫

//2017-11-08

#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <algorithm>

#include <queue>

using namespace std;
/*
num=q.front()
q.pop()
q.push(num);
size=q.size();
 */
 
queue<int> q0;
queue<int> q1;
queue<int> q2;
int qu0=0,qu1=0,qu2=0;
int sort3(int a[],int start,int end){
	for(int i=start;i<=end;i++){
		if(a[i]%3==0){
			q0.push(a[i]);
			qu0++;
		}
		else if(a[i]%3==1){
			q1.push(a[i]);
			qu1++;
		}
		else if(a[i]%3==2){
			q2.push(a[i]);
			qu2++;
		}
	}
}
int printar(int a[],int start,int end){
	for(int i=start;i<=end;i++){
		printf("%d ",a[i]);
	}
	printf("\n");
}
int main(){
	int num;
	int a[15]={0,23,45,7,23,6723,7,34,5768,3456,423,78,0};
	int len=11;
	printf("the array before sorting:\n");
	//sort(a,a+12);
  
	printar(a,1,11);
	sort(a,a+12);
	//printar(a,1,11);
  
	sort3(a,1,11);
	printf("the array after sorting:\n");
	while(qu0>0){
		num=q0.front();
		q0.pop();
		qu0--;
		printf("%d ",num);
	}
	while(qu1>0){
		num=q1.front();
		q1.pop();
		qu1--;
		printf("%d ",num);
	}
	while(qu2>0){
		num=q2.front();
		q2.pop();
		qu2--;
		printf("%d ",num);
	}
	return 0;
}