תוכנית אשר בודקת שני מערכים - תכנות - HWzone פורומים
עבור לתוכן
  • צור חשבון

תוכנית אשר בודקת שני מערכים


neogod

Recommended Posts

כתבתי תוכנית אשר אמורה להשוות שני מערכים חד מימדיים ,אך משום מה החיפוש הבינארי לא מחזיר לי את ההתוצאות הדרושות

public class IntVector
{
//instance variable
private int [ ] _arr;

/**
* Constructor for objects of class IntVector
*/
public IntVector (int size)
{
_arr = new int [size];
}

//methods
/**
*
*/
public void addInt (int num , int index)
{
if(index<_arr.length)
_arr[index] = num;
else
System.out.println("the index is bigger than the array");
}

/**
*
*/
private void swap(int [] data , int first , int second)
{
int temp;

temp = data[first];
data[first] = data[second];
data[second] = temp;
}

/**
* 3 arguments partition
*/
private int partition (int [] a , int lo , int hi)
{
swap (a , lo , medianLocation(a , lo+1 , hi , (lo+hi)/2 ) );
int m = partition (a, lo+1 , hi ,a[lo]);
swap (a, lo , m);
return m ;
}

/**
* 4 argument partition
*/
private int partition( int [] a , int lo , int hi , double pivot)
{
if (hi == lo )
if (a[lo]< pivot)
return lo ;
else
return lo-1;
else if (a[lo]<= pivot)
return partition(a, lo+1 , hi , pivot);
else
{
swap (a ,lo , hi);
return partition (a , lo , hi-1, pivot );
}
}

/**
* median location method
*/
private int medianLocation(int [] a , int i , int j , int k )
{
if( a[i]<= a[j] )
{
if ( a[j]<= a[k])
return j;
else if(a[i] <= a[k])
return k ;
return i;
}
else // a[j] < a[i]
if ( a[i] <= a[k])
return i;
else if (a[j]<= a[k])
return k;
else return j;
}

/**
* Quick Sort
*/
private void quicksort(int [] a , int lo , int hi)
{
int m;
if (hi>lo+1) // there are at least 3 elements , sort recursively
{
m=partition(a,lo,hi);
quicksort(a , lo , m-1);
quicksort(a , m+1 , hi);
}
else //1,2 or 3 elements , sort directly
if( hi ==lo+1 &&a[lo] >a[hi] )
swap(a , lo , hi);
}


/**
* binary search for num in an ordered array
*/
private int binary(int [] data , int num) //binary search for num in an ordered array
{
int middle , lower=0 , upper=(data.length-1) ;
do
{
middle=((lower + upper)/2);
if (num < data[middle])
upper = middle-1;
else lower = middle+1;
}
while ( ( data[middle] != num) && (lower<= upper)); // postcondition : if data[middle] isn't num
if(data[middle] == num)
return middle;
else return -1;

}

/**
* whhhhhhhhhhhhhhhhatttttttttttt
*/
public boolean what(int [ ] other )
{
if(_arr.length != other.length)
{
return false;
}
quicksort(_arr , 0 , _arr.length-1);
quicksort(other , 0 , other.length-1);
boolean f=false;
for(int i=0 ; i<_arr.length ; i++)
{
if((binary(_arr , other[i])== (-1)) && (binary(other, _arr[i])==(-1)))
return f;
else f= true;

}
return f;
}
}



public class Tester1
{
public static void main (String[] args)
{
IntVector aaa = new IntVector(10);
aaa.addInt(5,0);
aaa.addInt(55,1);
aaa.addInt(89,2);
aaa.addInt(15,3);
aaa.addInt(3,4);
aaa.addInt(8,5);
aaa.addInt(785,6);
aaa.addInt(78,7);
aaa.addInt(99,8);
aaa.addInt(512,9);

IntVector intArr1 = new IntVector(3);
intArr1.addInt(5,0);
for(int i=0 ; i<3; i++)
intArr1.addInt(i+1 ,i);


int[] bbb= {15,89,55,5,3,8,785,78,96565,512};//new int [10];
System.out.println(aaa.what(bbb));
// int[] ccc = {1,3,2};
// System.out.println(intArr1.what(ccc));


}
}

קישור לתוכן
שתף באתרים אחרים

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

×
  • צור חדש...