[問題] quicksort (c++)?

看板C_and_CPP (C/C++)作者 (拜託讓我上~)時間16年前 (2009/07/18 15:05), 編輯推噓6(604)
留言10則, 6人參與, 最新討論串1/1
int quicksort(int *,int start,int end); int quicksort(int *ptr,int start,int end){ int swap(int *,int *); int *pk,k,mid,left,right; mid=(left+right)/2; *pk=ptr[start]; left=start++; right=end--; while(left<right){ while(ptr[left]<*pk) left++; while(ptr[right]>*pk) right--; if(left>=right) break; swap(&ptr[left],&ptr[right]); } swap(pk,&ptr[right]); quicksort(ptr,start,right-1); quicksort(ptr,right+1,end); for(k=0;k<end;k++) cout<<ptr[k]<<endl; } int swap(int *ptr1,int *ptr2){ int temp; temp=*ptr1; *ptr1=*ptr2; *ptr2=temp; } main(){ int const sum=4; int A[sum],i=0,j=sum; cout<<"please input four integer="<<endl; for(i=0;i<sum;i++) cin>>A[i]; quicksort(A,i,j); system("pause"); } 小弟ㄧ直run不出? 請幫我看一下 compiler會過 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 60.250.147.18

07/18 17:16, , 1F
編譯不過嗎
07/18 17:16, 1F

07/18 17:17, , 2F
我想你至少也要#include <iostream.h>
07/18 17:17, 2F

07/18 18:51, , 3F
別再iostream.h了....
07/18 18:51, 3F

07/18 18:55, , 4F
沒記錯的話 c有內建qsort的函式 直接用吧
07/18 18:55, 4F

07/18 19:32, , 5F
<iostream.h> , <iostream> 不是這篇討論的重點..
07/18 19:32, 5F

07/18 19:34, , 6F
你自己要提的.. 原po都說compile會過了= =
07/18 19:34, 6F

07/18 19:36, , 7F
漏看原po最後一句, 我想講的是要"引入標頭檔"
07/18 19:36, 7F

07/18 20:09, , 8F
謝謝你們 我已經試出了 多謝!
07/18 20:09, 8F

07/18 21:42, , 9F
mid變數是為了? 感覺好像Binary Search要用的變數 @@?
07/18 21:42, 9F

07/18 23:48, , 10F
就算不是重點, 也不能給人錯的觀念吧
07/18 23:48, 10F
文章代碼(AID): #1AONHMPl (C_and_CPP)
文章代碼(AID): #1AONHMPl (C_and_CPP)