C language to achieve parity sorting algorithm
- 2020-04-02 02:05:29
- OfStack
= = = = =
Problem 2: odd-even sorting (I)
= = = = =
Total time limit:
1000 ms
Memory limit:
65536 KB
describe
Enter ten integers, and output them in ascending order, with odd Numbers first and even Numbers last.
The input
Enter ten integers
The output
Ten integers sorted by parity
#include<stdio.h>
#define COUNT 10
#define bool int
#define true 1
#define false 0
int* sortFunction(int data[]){
int i,j;
for(j=0;j<COUNT-1;j++){
for(i=0;i<COUNT-1-j;i++){
if (data[i]>data[i+1]){
int tmp=data[i]; data[i]=data[i+1];data[i+1]=tmp;
}
}
}
return data;
}
bool isOddNumber(int data){
if(data % 2==0){
return false;
}else{
return true;
}
}
main()
{
int arr[]={10, 9 ,8, 7 ,6, 5, 4, 3, 2, 1};
int oddArr[COUNT]={0},evenArr[COUNT]={0};
int *tmp=NULL;
int i;
int m=0;
int n=0;
for(i=0;i<COUNT;i++){
if(isOddNumber(arr[i])){
evenArr[m++]=arr[i];
}else{
oddArr[n++]=arr[i];
}
}
//Sort an odd array
tmp=sortFunction(oddArr);
puts("n========odd number==========n");
for(i=0;i<COUNT;i++){
int num=*tmp;
if(num!=0)
{
printf("%d ",num);
}
*tmp++;
}
//Sort even arrays
tmp=sortFunction(evenArr);
puts("n========even number==========n");
for(i=0;i<COUNT;i++){
int num=*tmp;
if(num!=0)
{
printf("%d ",num);
}
*tmp++;
}
printf("over");
}
The above code is written and completed in vs2012.