※ [本文轉錄自 ESOE-91 看板]
作者: shmm (我要電小堡) 看板: ESOE-91
標題: 小堡 我計程跟你講的
時間: Thu May 8 02:24:04 2003
#include <stdio.h>
#include <stdlib.h>
int* my_sort(int* num,int n);
int main()
{
int *num,n,i;
printf("總共數目: ");
scanf("%d",&n);
num=(int*)calloc(n,sizeof(int));
for(i=0;i<n;i++)
{
printf("第%2d個數: ",i+1);
scanf("%d",(num+i));
}
num=my_sort(num,n);
for(i=0;i<n;i++) printf("%d ",*(num+i));
printf("\n");
return 0;
free(num);
}
int* my_sort(int* num,int n)
{
int *sort,i,j,count;
char *check;
sort=(int*)calloc(n,sizeof(int));
check=(char*)calloc(n,sizeof(char));
for(i=0;i<n;i++)
{
for(count=0,j=0;j<n;j++)
if(*(num+i)>*(num+j)) count++;
for(;*(check+count)=='d';count++);
*(sort+count)=*(num+i);
*(check+count)='d';
}
return sort;
free(sort);
free(check);
}
這理論上比泡泡快很多
實際上我沒試
隻前有po的因為有數字重複會變0的情形
所以我自D了
改成多用一個 check 來檢查
不小心看到的請更正
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 140.112.240.76
※ 編輯: shmm 來自: 140.112.240.76 (05/08 02:24)
※ 編輯: shmm 來自: 140.112.240.76 (05/08 02:25)
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 140.112.26.85