//输入几个数,然后将它们按大小排序 #include <stdio.h> #include <stdlib.h> #include <conio.h>
void main() { int numsize; int *ip;
int counter; int tempcounter; int temp; cprintf("please enter the numsize:\t"); cscanf("%d",&numsize); ip = (int *)malloc(numsize*sizeof(int)); if(!ip) exit(1); for(counter=0;counter<numsize;counter++,ip++) { cprintf("\nplease enter the %dth number:\t",counter+1); cscanf("%d",ip); } ip-=numsize; cprintf("\nthe numbers in arry before order:\n"); for(counter=0;counter<numsize;counter++,ip++) cprintf("%5d",*ip); cprintf("\n"); ip-=numsize; //冒泡排序 for(counter=0;counter<numsize;counter++) { for(tempcounter=0;tempcounter<numsize-counter-1;tempcounter++) { if(*(ip+tempcounter)>*(ip+tempcounter+1)) { temp=*(ip+tempcounter); *(ip+tempcounter)=*(ip+tempcounter+1); *(ip+tempcounter+1)=temp; } } } cprintf("the numbers in arry after order:\n"); for(counter=0;counter<numsize;counter++,ip++) cprintf("%5d",*ip); ip-=numsize; free(ip); cprintf("\n"); cprintf("END\n"); }