2009-11-01 13:41:18來源不明

內建快排 同時移動附件

> 10筆以上的快排只能用內建的來做  (用遞迴有限制)
> 但是當我想快排的時候,數字的附件(數字)也要一起移動的話,是否有辦法呢?

以下內容   從teching老師 那邊*來的

#include   <stdio.h>  
#include   <stdlib.h>  
   
struct node  
{  
  int x;  
  int y;  
}node[4]={{4,3},{6,6},{1,7},{5,8}};  
   
int compare(const void *ele1, const void *ele2)  
{  
  struct node  *px,*py;  
   
  px=(struct node *)ele1;  
  py=(struct node *)ele2;  
/* 由小排到大 */   
  if(px->x > py->x) return 1;  
  else return 0;
 
/* 由大排到小   
  if(px->x < py->x) return 1;  
  else return 0;
*/     
}  
   
int main()  
{  
  int i;  
  qsort(node,4,sizeof(struct node), compare);  
  for(i=0;i<4;i++)  
    printf("{%d,   %d},     ",node[i].x,node[i].y);
  printf("\n");
  system("pause");
  return 0;  
}  

chchwy 2009-11-15 21:55:49

真的喔= =
理想的狀況下(數列非常隨機) 快排的遞迴深度是lgN
一百萬個int ,深度也才20
應該不會那麼容易爆stack吧....

版主回應
可能是我的錯覺吧 =] 科科 2009-11-16 08:07:04
chchwy 2009-11-13 22:25:31

我用過自己寫的遞迴版快排,排超過100萬個整數,也是眨眼就跑完了,說遞迴的限制實在沒什麼道理。

如果你不執著於C的話,C++的std::sort()要比qsort快上不少

版主回應
哦 用遞迴不會爆炸? 真的假的 @@ 你確定 ? (爆炸過好幾次) 2009-11-13 23:50:39