16-10-2011, 09:31
|
|
|
חבר מתאריך: 04.12.09
הודעות: 12
|
|
[עזרה]בעיה בקוד שכתבתי quicksort
היי..
ניסיתי לכתוב קוד משלי לפונקציה quick sort..
אני בטוח שכל החלקים שבו נכונים אבל הקומפיילר לא מוכן להריץ את זה..
כל פעם שהוא מסמן לי איזה שורה שלא עובדת ואני "מתקן" אותה כדי שיעבוד הוא ממשיך לשורה אחרת ולא מוכן להריץ את זה..
מה הבעיה?
יש לי את אותה בעיה שמתוארת בויקיספר תחת הכותרת הראשונה טיפול בחריגות:
http://alturl.com/eudzz
גם שם, אני לא מבין למה זה עושה הודעת שגיאה-נראה לי שהכל תקין.
הנה הקוד מי שרוצה לראותיש גם קובץ מצורף)
תודה לעוזרים!!!
קוד:
object quicksort {
def main (args : Array[String]) {
//get the array
var n=5
val arr =Array(5,4,3,2,1)
var temp=0
//quick sort
def sort(x:Array[Int],s:Int,e:Int):Array[Int]={
var pivot=x((e/2))
if(x.length==1){//stop sign
x
}
else{
//check whether the functions are splited to big and small numbers relative to pivot
if((checklow(s,(e/2)-1))&&checkhigh((e/2)+1,e)){
sort(x,(e/2)+1,e)//create 2 new arrays
sort(x,s,(e/2)-1)
}
else{
//sorting the arrays to 2 sides of big and small
temp=x(sidurhigh(e))
x(sidurhigh(e))=x(sidurlow(s))
x(sidurlow(s))=temp
sort(x,s,e)
}
}
//functions
def checkhigh(s:Int,e:Int)={//check if the all the numbers are bigger than pivot
var i=s
var check=true
while ((i<=e)&check){
if(x(i)<pivot)
check=false
i+=1
}
check
}
def checklow(s:Int,e:Int)={//check if the all the numbers are lower than pivot
var i=s
var check=true
while ((i<=e)&check){
if(x(i)>pivot)
check=false
i+=1
}
check
}
def sidurhigh(e:Int):Int={//finding the place of the numbers that should be switched
if (x(e)<=pivot){
e}
else
sidurhigh(x(e-1))
}
def sidurlow(s:Int):Int={
if (x(s)>pivot){
s}
else
sidurlow(x(s+1))
}
x
}
sort(arr,0,5)
}
}
נערך לאחרונה ע"י ranl666 בתאריך 16-10-2011 בשעה 09:36.
|