[問題] 利用指標完成quicksort

看板C_and_CPP作者 (漂流木)時間14年前 (2010/03/22 05:51), 編輯推噓0(003)
留言3則, 1人參與, 最新討論串1/3 (看更多)
遇到的問題: (題意請描述清楚) 在我標紅色等號的地方 如果把等號拿掉便可以得到正確排序的結果 一但加上了就會產生記憶體不能為written的錯誤訊息 是哪個環結不小心漏失了嗎? 還是十二誡的哪一條? 怎麼多加一個等號就死掉了…… 找得有點久,拜託有空的高手指點了 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) Dev-C++ 有問題的code: (請善用置底文標色功能) #include<stdio.h> #include<stdlib.h> #define LEN 6 int input[LEN]={3,6,1,5,4,2}; void swap(int &a , int &b){ int temp; temp = a; a = b; b = temp; } void quickSort(int* front , int* last){ int meddle; int *initFront = front; int *initLast = last; meddle = *front; while(last > front){ while(*last >= meddle) last--; swap(*front,*last); while(*front <= meddle) front++; swap(*last,*front); } front--; last++;printf("\n"); if(initFront<front)quickSort(initFront,front); if(initLast>last)quickSort(last,initLast); } main(){ int *p,*q; p = input; q = p+LEN-1; quickSort(p,q); for(int i=0;i<LEN;i++)printf("%d ",input[i]); system("pause"); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.27.146.174

03/22 06:25, , 1F
你確定拿掉等號就可以正確執行嗎?
03/22 06:25, 1F

03/22 06:26, , 2F
我自己試過無法執行
03/22 06:26, 2F

03/22 06:27, , 3F
你遞回的收斂條件是???
03/22 06:27, 3F
t大對不起= = 因為等號無法加上去的關係 所以當測資有相同時會成換來換去的無限迴圈 我把測資改成不重覆就可以跑了 只是 等號為什麼還是不能加上去 這點希望能指導一下 謝謝你一大早就看到 ※ 編輯: secondsee 來自: 114.27.146.174 (03/22 10:54)
文章代碼(AID): #1BffJ9ol (C_and_CPP)
文章代碼(AID): #1BffJ9ol (C_and_CPP)