Você está na página 1de 14

public class onesMatrix {

public static int[][] biuldOnesMatrix(int num){


int[][] matrix=new int[num][num];
for (int i=0;i<num;i++){
for (int j=0;j<num;j++){
matrix[i][j]=(int)(Math.random()*2);
}

}
return matrix;
}
public static int[][] OnesMatrix(int num){
int[][] matrix=new int[num][num];
for (int i=0;i<num;i++){
for (int j=0;j<num;j++){
matrix[i][j]=1;
}

}
return matrix;
}
public static int[][] CreateEffectiveMatrix(int[][] matrix){
int[][] newmatrix=new int[matrix.length][matrix.length];
for (int i=0;i<matrix.length;i++){
if (matrix[i][0]==1){newmatrix[i][0]=1;}
}
for (int i=0;i<matrix.length;i++){
if (matrix[0][i]==1){newmatrix[0][i]=1;}
}
for (int i=1;i<matrix.length;i++){

for (int j=1;j<matrix.length;j++){


if (matrix[i][j]==0){newmatrix[i][j]=0;}
else {int a=Math.min(newmatrix[i-1][j], newmatrix[i][j-1]);
newmatrix[i][j]=Math.min(a, newmatrix[i-1][j-1])+1;
}
}
}
return newmatrix;
}
public static int[][] printMatrix(int[][] matrix){

for (int i=0;i<matrix.length;i++){


for (int j=0;j<matrix.length;j++){
System.out.print(matrix[i][j]+" |");

}
System.out.println();
}
System.out.println();
return matrix;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] matrix =biuldOnesMatrix(20);
printMatrix(matrix);
int[][] NewMatrix=CreateEffectiveMatrix(matrix);
printMatrix(NewMatrix);
}

}
_________________________________________________
public class wallAndBall {

public static int[] createRndArr(int size){


int[] arr =new int[size];
arr[0]=0;
for(int i= 1;i<arr.length;i++){
arr[i]=arr[i-1]+(int)(Math.random()*10);
}
return arr;
}
public static int runOnTower(int[] arr,int start,int stop,int breakPoint ){
int theBreak=start;
for(int i= start;i<stop;i++){
if (breakPoint<=arr[i]){theBreak=i;i=stop;}
}

return theBreak;
}
public static int twoBalls(int[] arr,int start,int stop,int breakPoint){
int checkPlace=0;
int theBreake=0;
int divide =(int)Math.sqrt(arr.length);
//int divide =(int)(-1+Math.sqrt(1+8*arr.length)/2);
while (breakPoint >=arr[checkPlace]){
if (checkPlace+divide>=arr.length){
for(int i=checkPlace;i<arr.length;i++){
if (breakPoint<=arr[i]){theBreake=i;return theBreake;}
}return arr[arr.length-1];
}
else{checkPlace=checkPlace+divide;}
}
System.out.println(checkPlace);
return runOnTower(arr,checkPlace-divide,checkPlace,breakPoint);
}
public static int SecondtwoBalls(int[] arr,int start,int stop,int breakPoint){
int checkPlace=0;
int theBreake=0;

int divide =(int)((-1+Math.sqrt(1+8*(arr.length-1)))/2);

while (breakPoint >=arr[checkPlace]){


if (checkPlace+divide>=arr.length){
for(int i=checkPlace;i<arr.length;i++){
if (breakPoint<=arr[i]){theBreake=i;return theBreake;}
}return arr[arr.length-1];
}
else{checkPlace=checkPlace+(divide-1);System.out.println(checkPlace);}
}
System.out.println(checkPlace);
return runOnTower(arr,checkPlace-divide,checkPlace,breakPoint);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] arr=createRndArr(100);
for (int i=0;i<arr.length;i++){
System.out.println(i+" : "+arr[i]);
}
System.out.println(SecondtwoBalls(arr,0,arr.length-1,620));
}

public class game {


public static int i,j;
public static int[] createArr(int num){
int[] ans=new int[num];
for(int i=0;i<ans.length;i++){
ans[i]=(int)(Math.random()*10);
}
return ans;
}
public static int[][] CreateMatrix(int[] arr){
int[][] matrix=new int[arr.length][arr.length];
for(int i=0;i<arr.length;i++){
matrix[i][i]=arr[i];
}
return matrix;
}

public static int[][] FillTheMatrix(int[][] matrix,int[] arr){


for(int i=matrix.length;i>=0;i--){
for(int j=i+1;j<matrix.length;j++){
matrix[i][j]=Math.max((arr[i]-matrix[i+1][j]),(arr[j]- matrix[i][j-1]));
}

}
return matrix;
}
public static int OneTurn(int[] arr,int[][] matrix){
int turn=0;
if (matrix[i][j]==(arr[i]-matrix[i+1][j])){turn=turn+arr[i];i++;}
else {turn=turn+arr[j];j--;}
return turn;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//int[] arr=createArr(5);
int[] arr={1,3,6,1,3,6};
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]+" |");
}

int[][] matrix =CreateMatrix(arr);


matrix=FillTheMatrix(matrix,arr);
PrintMatrix(matrix);
int comp1 = 0,comp2=0;
j=matrix.length-1;i=0;
for (int k=0;k<6;k++){
if (k%2==0){comp1=comp1+OneTurn(arr,matrix);}
else {comp2=comp2+OneTurn(arr,matrix);}
}
System.out.println(comp1);
System.out.println(comp2);

}
}

public class flight {

public static Point[][] createMatrix(int sizeA,int sizeB){


Point[][] arr= new Point[sizeA][sizeB];
for(int i=0;i<sizeA;i++){
for(int j=0;j<sizeB;j++){
int r,u;
r=(int)(Math.random()*10);
u=(int)(Math.random()*10);
arr[i][j]=new Point(r,u);
}
}
return arr;
}
public static int[][] lenthToZero(Point[][] Parr,int[][] arr){
arr[0][0]=0;
for (int i=1;i<arr[0].length;i++){
arr[0][i]=
arr[0][i-1]+
Parr[0][i-1].get_r();
}
return arr;
}
public static int[][] hightToZero(Point[][] Parr,int[][] arr){
arr[0][0]=0;
for (int i=1;i<arr.length;i++){
arr[i][0]=arr[i-1][0]+Parr[i-1][0].get_r();
}
return arr;
}

public static int[][] flightRoute(Point[][] Parr,int[][] arr){


for(int i=1;i<arr.length;i++){
for(int j=1;j<arr[0].length;j++){
arr[i][j]=Math.min(arr[i-1][j]+Parr[i-1][j].get_r(),arr[i][j-
1]+Parr[i][j-1].get_u());
}
}
return arr;
}
public static String oneOptimalRoute(Point[][] Parr,int[][] arr){
String route="";
//for(int i=arr.length;i<arr.length;i++){
// for(int j=1;j<arr[0].length;j++){
int i=arr.length-1;
int j=arr[0].length-1;
while ((i>0)&&(j>0)){
if (arr[i][j]==arr[i][j-1]+Parr[i-1][j-1].get_u()){
j--;
route="u"+route;
}
else {i--;
route="r"+route;
}
}
if (i==0){ for( i=j;j>0;j--){
route="u"+route;
}}
else{ for( i=i;i>0;i--){
route="r"+route;
}}
return route;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Point[][] arr=createMatrix(5,5);
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[0].length;j++){
System.out.print(arr[i][j]);
}
System.out.println();
}
System.out.println("___________________");
System.out.println();
int[][] test= new int[5][5];
test=lenthToZero(arr,test);
test=hightToZero(arr,test);
test=flightRoute(arr,test);

System.out.println(oneOptimalRoute(arr,test));
}
}

public class tlat_krav {

public static int howManyAlive(lochem[] arr ){


int count=0;
for (int i=0;i<arr.length;i++){
if (arr[i]._isAlive==true)count++;
}
return count;
}
public static lochem secondOneAlive(lochem[] arr ,lochem present){
lochem temp=arr[2];

for (int i=0;i<arr.length;i++){


if
(arr[i]._isAlive==true&&!arr[i]._name.equals(present._name)){temp=arr[i];
}
}
return temp;
}
public static lochem[] whoIsAlive(lochem[] arr ){
lochem[] temp=new lochem[3];
int count=0;
for (int i=0;i<arr.length;i++){
if (arr[i]._isAlive==true){temp[count]=arr[i];count++;
}
}
return temp;
}
public static void duKrav(lochem first,lochem second,lochem[] arr){
if (first._stragey==null&&howManyAlive(arr)==3){}
else{
double random = Math.random();
//System.out.println(random);
if (first._persucced>=random) second._isAlive=false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
lochem one =new lochem("ariel",1,null);
lochem two =new lochem("benyamin",0.8,one);
lochem three =new lochem("candy",0.5,null);
two._isAlive=true;
one._stragey=two;
lochem[] arr = new lochem[3];
arr[0]=one;
arr[1]=two;
arr[2]=three;

for (int i =0;i<500;i++){


if (arr[i%3]._isAlive==true){
if
(howManyAlive(arr)==1){System.out.println(whoIsAlive(arr)[0]._name);i=501;}
if (howManyAlive(arr)==2)
{duKrav(arr[i%3],secondOneAlive(arr, arr[i%3]),arr);}
if (howManyAlive(arr)==3)
{duKrav(arr[i%3],arr[i%3]._stragey,arr);}
}
}
}

________________________

//‫אלגוריתם "חוצפני" למציאת איבר הגדול מלפחות חצי מהמספרים במערך‬


//‫האלגוריתם הוא בסיבוכיות‬
//O(1)
import java.util.Arrays;

public class BiggerthenHalf {


public static void main(String[] args) {
int[]arr=new int[1000000];
for (int i=0;i<arr.length;i++){
arr[i]=(int)(Math.random()*10000000);
}
int maxthenhalf=bth(arr);
System.out.println(maxthenhalf);
Arrays.sort(arr);
System.out.println(max(arr,0,500000));
}
public static int bth(int[]arr){
int maxthenhalf=max(arr,0,63);
return maxthenhalf;
}
public static int max(int[]arr,int start,int end){
int max=0;
for (int i=start;i<=end;i++){
if (arr[i]>max)max=arr[i];
}
return max;
}

_____________

//‫אלגוריתם המקבל סטרינג ומחזיר את כל האופציות לסידור של האיברים בו‬


public class Combinatorika {
public static void main(String[] args) {
String s1 = "";
String s2 = "123";
com(s1,s2);

}
public static void com(String s1, String s2) {
if (s2.equals("")){
System.out.println(s1);
return;
}
for (int i=0;i<s2.length();i++){
String tmp1 = s1+ s2.charAt(i);
String tmp2 = s2.substring(0,i)+s2.substring(i+1, s2.length());
com (tmp1,tmp2);
}
}

//‫אלגוריתם שמחזיר את מספר האיברים המשותפים בין שני סטרינגים‬


//O(n*m)
//‫ואלגוריתם המחזיר את הסטרינג המשותף בין שני סטרינגים‬
import java.util.Stack;
import java.util.Vector;

public class Comparing2Strings_2 {


public static void main(String[] args) {
String s1="yoaviminim";
String s2="noamigdy";
int ans2=comp2(s1,s2);
System.out.println(ans2);
}
//‫זה יוצר את המטריצה‬
public static int[][] comp(String s1,String s2){
int[][]arr=new int[s1.length()+1][s2.length()+1];
for (int i=1;i<s1.length()+1;i++){
for (int j=1;j<s2.length()+1;j++){
if (s1.charAt(i-1)==s2.charAt(j-1)){
arr[i][j]=arr[i-1][j-1]+1;
}
else arr[i][j]=Math.max(arr[i-1][j],arr[i][j-1]);
}
}
return arr;
}
//‫מחזיר את המספר שנמצא בפינה שהוא בעצם התשובה‬
public static int commonlength(int[][]arr){
return arr[arr.length-1][arr[0].length-1];
}
//‫אלגוריתם שמחזיר את אחד הסטרינגים המשותפים‬
public static String commonString1(int[][]arr,String s1,String s2){
String ans="";
int i=arr.length-1;
int j=arr[0].length-1;
while(j>0&&i>0){
if (s1.charAt(i-1)==s2.charAt(j-1)){
ans=s1.charAt(i-1)+ans;
i--;
j--;
}
else{
if (arr[i][j-1]>arr[i-1][j]){
j--;
}
else{
i--;
}
}
}
return ans;
}
//‫אלגוריתם חדש שמשתמש באלגוריתם של "כמה ממויין המערך" שהוא בסיבוכיות של‬
//O(nlogn)
//‫ננסה להשתמש בו ע"מ להגיע לסיבוכיות יותר טובה ממה שהגענו עד עכשיו‬
// ‫ נגיע ל‬O(n*mlog(min(n,m)))
public static int comp2(String s1,String s2){
int ans=0;
Vector<Integer> toLIS=new Vector<Integer>();
String temp=null,temp2=null;
Stack<Integer>[] stackarray=new Stack[26];
for( int i=0;i<stackarray.length;i++){
stackarray[i]=new Stack<Integer>();
}
if (s1.length()<s2.length()){temp=s1;temp2=s2;}
else {temp=s2;temp2=s1;}
for (int i=0;i<temp.length();i++){
int r=temp.charAt(i)-97;
stackarray[r].push((i));
}
for (int i=0;i<temp2.length();i++){
int r=temp2.charAt(i)-97;
if (!stackarray[r].empty()){
Stack<Integer> l=(Stack<Integer>)stackarray[r].clone();
while (!l.empty()){
toLIS.addElement(l.pop());
}
}
}
int[]ToLIS=new int[toLIS.size()];
for (int i=0;i<toLIS.size();i++){
ToLIS[i]=toLIS.elementAt(i);
}
ans=HowMuchSorted.howmuchsorted_Dinamic_d(ToLIS);
return ans;
}
}
__________________

public static long stupidFactorial(int num){//‫רקורסיה‬


if (num==1)return 1;
return num*stupidFactorial(num-1);

}
public static long goodFactorial(int num){//‫אינדוקציה‬
if (num==1)return 1;
int ans=1;
for (int i=1;i<=num;i++){
ans=i*ans;
}
return ans;
}
_______________________

public static int stupidFibonatshi(int num){//‫רקורסיה‬


if (num==1||num==2){
return 1;
}
return (stupidFibonatshi(num-1)+stupidFibonatshi(num-2));
}
public static long goodFibonatshi(int num){//‫אינדוקציה‬
if (num==1||num==2){
return 1;
}
long ans=0;
long a=1,b=1;
for (int i=2;i<num;i++){
ans=a+b;
a=b;
b=ans;
}
return ans;
}
__________________________

public class Groups {


public static void main(String[] args) {
String s="abc";
Vector<String> l=go1(s);
for (int i=0;i<l.size();i++){
System.out.println(l.elementAt(i));
}
}
public static Vector<String> go1(String s){
Vector<String> v=new Vector<String>();
if (s.length()==0){
v.add("");
return v;
}
else{
Vector<String> g=go1(s.substring(0, s.length()-1));
String l=s.substring(s.length()-1, s.length());
int size=g.size();
for (int i=0;i<size;i++){
String a=g.elementAt(i)+l;
g.add(a);
}
v=g;
return v;
}

}
________________________________

public class HowMuchSorted {

public static void main(String[] args) {


int[]arr={1,100,101,2,3,7,5,6,7,8};
System.out.println(howmuchsorted_Dinamic_d(arr));
}
/**
* @return the size of the biggest sorted serius in arr
* this is O(n*2^n) -----very bad
*/
public static int howmuchsorted_FullSearch(int[]arr){
int ans=0;
Vector<int[]>v=go1(arr,0,arr.length-1);
for (int i=v.size()-1;i>=0;i--){
if (isSorted(v.elementAt(i))){
return v.elementAt(i).length;
}
}
return ans;
}
/**
* @return the size of the biggest sorted serius in arr
* this is O(n) -----very good but nor always the correct answer
*/
public static int howmuchsorted_Chamdani(int[]arr){
int ans=0;
int count=1;
for (int i=0;i<arr.length-1;i++){
if (arr[i]<arr[i+1])count++;
else {
if (ans<count)ans=count;
count=1;
}
}
if (ans<count)ans=count;
return ans;
}
/**
* @return the size of the biggest sorted serius in arr
* this is O(n^2) -----good
* the second array is cloning and sorting the original array
*/
public static int howmuchsorted_Dinamic_a(int[]arr){
int[]B=arr.clone();
Arrays.sort(B);
int[][]a=new int[arr.length+1][B.length+1];
for (int i=1;i<arr.length+1;i++){
for (int j=1;j<B.length+1;j++){
if (arr[i-1]==B[j-1]){
a[i][j]=a[i-1][j-1]+1;
}
else a[i][j]=Math.max(a[i-1][j],a[i][j-1]);
}
}
return a[a.length-1][a[0].length-1];
}
/**
* @return the size of the biggest sorted serius in arr
* this is O(n^2) -----good
* the second array is all the nubmer from the min to the max in the original array
*/
public static int howmuchsorted_Dinamic_b(int[]arr){
int minmax[]=MinMax.minmax1(arr);
int size=minmax[1]-minmax[0];
int[]B=new int[size+1];
int toenterfrom=minmax[0];
for (int i=0;i<B.length;i++){
B[i]=toenterfrom++;
}
int[][]a=new int[arr.length+1][B.length+1];
for (int i=1;i<arr.length+1;i++){
for (int j=1;j<B.length+1;j++){
if (arr[i-1]==B[j-1]){
a[i][j]=a[i-1][j-1]+1;
}
else a[i][j]=Math.max(a[i-1][j],a[i][j-1]);
}
}
return a[a.length-1][a[0].length-1];
}
/**
* @return the biggest sorted serius in arr
* this is O(n^2) -----good
*/
public static int[] howmuchsorted_Dinamic_c(int[]arr){
int[][]M=new int[arr.length][arr.length];
int place=0;
for (int i=0;i<arr.length;i++){
int j=search(M,place,arr[i]);
if (j>=place)place++;
M[j][j]=arr[i];
copy(M,j);
}
return M[place-1];
}
/**
* @return the size of the biggest sorted serius in arr
* this is O(n*logn) -----the best algoritem of this class
*/
public static int howmuchsorted_Dinamic_d(int[]arr){
int []alachson=new int[arr.length];
int place=0;
for (int i=0;i<arr.length;i++){
int j=search_2(alachson,place,arr[i],0,place);
if (j>=place)place++;
alachson[j]=arr[i];
}
return place;
}
//‫מחזיר איפה יש איבר שאפשר להחליף באלכסון של המטריצה בגלל שלאיבר הזה יש פחות פוטנציאל‬
‫להתפתח‬
//complexity is O(n)
public static int search(int[][]M,int place,int num){
for (int i=0;i<place;i++){
if (num<M[i][i])return i;
}
return place;
}
//‫מעתיק את השורה הקודמת לתוך השורה הבאה עד למקום‬
//place
//complexity is O(n)
public static void copy(int[][]M,int place){
for (int i=0;i<place;i++){
M[place][i]=M[place-1][i];
}
}
//‫מחזיר איפה יש איבר שאפשר להחליף באלכסון של המטריצה בגלל שלאיבר הזה יש פחות פוטנציאל‬
‫להתפתח‬
//complexity is O(logn)
public static int search_2(int[] alachson,int place,int num,int from,int to){
/*for (int i=0;i<place;i++){
if (num<alchson[i])return i;
}
return place;*/
if (from==to){
return from;
}
if (num<=alachson[(to-from)/2+from])return search_2(alachson,place,num,from,(to-
from)/2+from);
return search_2(alachson,place,num,(to-from)/2+1+from,to);
}
/**
* @return vector with all the groups of arr
*/
public static Vector<int[]> go1(int[] arr,int start,int end){
Vector<int[]> v=new Vector<int[]>();
if (start==end){
v.add(new int[0]);
int[]y={arr[end]};
v.add(y);
return v;
}
else{
Vector<int[]> g=go1(arr,start,end-1);
int size=g.size();
for (int i=0;i<size;i++){
int[]s=g.elementAt(i);
int[]t=new int[s.length+1];
int j=0;
while(j<t.length-1){
t[j]=s[j];
j++;
}
t[t.length-1]=arr[end];
g.add(t);
}
v=g;
return v;
}

}
/**
* @return true if the array is sorted
*/
public static boolean isSorted(int[]arr){
for (int i=0;i<arr.length-1;i++){
if (arr[i]>arr[i+1])return false;
}
return true;
}
}
______________________________________
//‫מציאת מקס אחד ומקס שתיים באלגוריתם שסיבוכיותו היא‬
//nlog(n)
import java.util.Stack;

public class Max1Max2Induction {


public static void main(String[] args) {
LinkedList list=new LinkedList();
for (int i=0;i<10;i++){
list.Insert((int)(Math.random()*100));
}
LinkedListElement temp=list.head();
for (int i=0;i<10;i++){
System.out.print("["+i+"]="+temp._max);
temp=temp._next;
if (i==9)System.out.println();
}
LinkedListElement max=induction(list);
System.out.println("Max1="+max._max);
//System.out.println(max._stack);
System.out.println("max2="+findMax(max._stack));
}
public static LinkedListElement induction(LinkedList list){
LinkedListElement temp1=list.head();
LinkedListElement temp2=list.head()._next;
while (list.Size()>1){
if (temp1._max>temp2._max){
temp1._stack.push(temp2._max);
list.Delete(temp2);
if (temp1.hasNext())temp1=temp1._next;
}
else{
temp2._stack.push(temp1._max);
list.Delete(temp1);
if (temp2.hasNext())temp1=temp2._next;
else temp1._next=null;
}
if(temp1.hasNext())temp2=temp1._next;
else {
temp1=list.head();
temp2=temp1._next;
}

}
return list.head();
}
private static int findMax(Stack<Integer> stack) {
int max = stack.pop(); int temp = 0;
while (!stack.isEmpty()){
temp = stack.pop();
if (max<temp)
max=temp;
}
return max;
}
}
__________________________

public class Max1Max2RecuraiveCell {


int _max;
Stack<Integer> _stack = new Stack<Integer>();

Max1Max2RecuraiveCell(int num, Stack<Integer> stack){


_max = num;
while (!stack.isEmpty())
_stack.push(stack.pop());
}

Max1Max2RecuraiveCell(int num){
_max = num;
_stack = new Stack<Integer>();
}

Max1Max2RecuraiveCell(Max1Max2RecuraiveCell c){
_max = c.max();
while (!c._stack.isEmpty())
_stack.push(c._stack.pop());
}
int max(){return _max;}
void setMax(int num){_max=num;}

public class Max1Max2Recursive {

/**This function is a RECUSIVE one!


* It calculates the MAX1's index and returns it.
* It works on a CELL array
* This function changes the array value!!!
* In the max1 cell - it Stack will hold the indexs of the
* candidates to be max2.
* @param i - the index from where to start
* @param j - the index where to end
* @param arr - the CELL array
* @return the index of the max1
*/
public static int f(int i, int j, Max1Max2RecuraiveCell [] arr){
int ind=i;
if (i<j){
int ind1=f(i, (i+j)/2, arr);
int ind2=f(((i+j)/2)+1, j, arr);
if (arr[ind1].max()>arr[ind2].max()){
ind = ind1;
arr[ind]._stack.push(ind2);
}
else{
ind = ind2;
arr[ind]._stack.push(ind1);
}
}
return ind;
}

/**This function calculates the Max value in a given arr


* It gets a stack of "candidates" - where the indexs are given
* and checks according to the indexs which organ is the biggest
* @param stack - holds the indexs of the candidates
* @param arr - the array where the numbers are
* @return - the index of the biggest organ.
*/
private static int findMax(Stack<Integer> stack, Max1Max2RecuraiveCell [] arr) {
int max = stack.pop(); int temp = 0;
while (!stack.isEmpty()){
temp = stack.pop();
if (arr[max].max()<arr[temp].max())
max=temp;
}
return max;
}

public static void main(String[] args) {


int size = 12;
Max1Max2RecuraiveCell [] arr = new Max1Max2RecuraiveCell [size];
for (int i=0; i<size; i++){
arr[i] = new Max1Max2RecuraiveCell((int)(Math.random()*100));
System.out.print(arr[i].max()+" ");
}
System.out.println();
int ans=f(0,arr.length-1,arr);
System.out.println("Max1 - "+arr[ans].max());
System.out.println("Max2 - "+arr[(findMax(arr[ans]._stack,arr))].max());
}
}
____________________-

public static void mergesort(int[ ] data, int first, int n){


int n1; // Size of the first half of the array
int n2; // Size of the second half of the array
if (n > 1){
// Compute sizes of the two halves
n1 = n / 2;
n2 = n - n1;
mergesort(data, first, n1); // Sort data[first] through
data[first+n1-1]
mergesort(data, first + n1, n2); // Sort data[first+n1] to the end

// Merge the two sorted halves.


merge(data, first, n1, n2);
}
}
private static void merge(int[ ] data, int first, int n1, int n2)
// Precondition: data has at least n1 + n2 components starting at data[first]. The first
// n1 elements (from data[first] to data[first + n1 – 1] are sorted from smallest
// to largest, and the last n2 (from data[first + n1] to data[first + n1 + n2 - 1]) are
also
// sorted from smallest to largest.
// Postcondition: Starting at data[first], n1 + n2 elements of data
// have been rearranged to be sorted from smallest to largest.
// Note: An OutOfMemoryError can be thrown if there is insufficient
// memory for an array of n1+n2 ints.
{
int[ ] temp = new int[n1+n2]; // Allocate the temporary array
int copied = 0; // Number of elements copied from data to temp
int copied1 = 0; // Number copied from the first half of data
int copied2 = 0; // Number copied from the second half of data
int i; // Array index to copy from temp back into data

// Merge elements, copying from two halves of data to the temporary array.
while ((copied1 < n1) && (copied2 < n2)){
if (data[first + copied1] < data[first + n1 + copied2])
temp[copied++] = data[first + (copied1++)];
else
temp[copied++] = data[first + n1 + (copied2++)];
}

// Copy any remaining entries in the left and right subarrays.


while (copied1 < n1)
temp[copied++] = data[first + (copied1++)];
while (copied2 < n2)
temp[copied++] = data[first + n1 + (copied2++)];

// Copy from temp back to the data array.


for (i = 0; i < n1+n2; i++)
data[first + i] = temp[i];
}

_________________________

public class MinMax {


public static void main(String[] args) {
int[]arr=new int[1000];
for (int i=0;i<arr.length;i++){
arr[i]=(int)(Math.random()*1000000);
}
int[] ans=minmax1(arr);
System.out.println("Min="+ans[0]+" Max="+ans[1]+" Count="+ans[2]);
int[] ans2=minmax2(arr);
System.out.println("Min="+ans2[0]+" Max="+ans2[1]+" Count="+ans2[2]);
}
//‫מעבר בזוגות של איברים‬
public static int[]minmax1(int[]arr){
int min=0,max=0,count=0;
count++;
if (arr[0]>arr[1]){
max=arr[0];
min=arr[1];
}
else{
max=arr[1];
min=arr[0];
}
for (int i=2;i<arr.length-1;i=i+2){
count++;
if (arr[i]>arr[i+1]){
count++;
if (arr[i]>max){
max=arr[i];
}
count++;
if (arr[i+1]<min){
min=arr[i+1];
}
}
else{
count++;
if (arr[i]<min){
min=arr[i];
}
count++;
if(arr[i+1]>max){
max=arr[i+1];
}
}
}
int[]ans={min,max,count};
return ans;
}
//‫מעבר איבר איבר‬
public static int[]minmax2(int[]arr){
int min=0,max=0,count=0;
count++;
if (arr[0]>arr[1]){
max=arr[0];
min=arr[1];
}
else{
max=arr[1];
min=arr[0];
}
for (int i=2;i<arr.length;i++){
count++;
if (arr[i]>max){
max=arr[i];
}
else {
count++;
if (arr[i]<min){
count++;
min=arr[i];

}
}
}
int[]ans={min,max,count};
return ans;
}
}
______________

public class Parking {

public static int countArray(int []_arr){


int sign=_arr[0]+1; int count=0; boolean found=false;
int start=_arr[0];

for (int i=1; !found; i=((i+1)%_arr.length)){


count++;
if (_arr[i]==start){
_arr[i]=sign; //mark this place
if (_arr[(i-count)%_arr.length]==_arr[i])
found=true;
else _arr[i]=sign;
}
}
return count;
}

public static void main(String[] args) {


int size=45;
int[] _arr = new int[size];
for (int i=0; i<_arr.length; i++)
_arr[i]=(int)(Math.random()*size);

System.out.println(countArray(_arr));
}
}
______________________

//‫אלגוריתם למציאת מספר בחזקת מספר בסיבוכיות של‬


//log(n)
public class Power {
public static void main(String[] args) {
int ans=Power(2,4);
System.out.println(ans);
}
public static int Power(int num,int power){
int ans=1;
String bin=Integer.toBinaryString(power);
for(int i=bin.length()-1;i>=0;i--){
if (bin.charAt(i)=='1'){
ans=ans*num;
num=num*num;
}
else{
num=num*num;
}
}
return ans;
}
}
____________________________
‫נועם משחק עם חזרה‬
public class game {

public static void main(String[] args) {


int [] arr = {1,3,6,1,3,6,10};
int [][]m = new int [arr.length][arr.length];

fillM(m,arr);

int from = 0; int to = arr.length-1;


printArr(arr, from, to);
int gamerSum = 0; int compSum = 0;
int who = 0; //0 - comp; 1-gamer
while (from<=to){
if (who==0){
String rec = recomand(m, from, to);
if (rec.equals("right")){
compSum+=arr[from];
from++;
}

else{
compSum+=arr[to];
to--;
}

printArr(arr, from, to);


who++;
}
else{
int which = MyConsole.readInt("Enter");
if (which==from){
from++;
}
else {
to--;
}
gamerSum+=arr[which];
who--;
}
}

System.out.println("Comp result = "+compSum);


System.out.println("Gamer result = "+gamerSum);

public static void fillM(int[][] m, int[] arr) {


for (int i=0; i<m.length; i++)
m[i][i] = arr[i];
for (int i=m.length-2; i>=0; i--)
for (int j = i+1; j<m[0].length; j++)
m[i][j] = Math.max(m[i][i] - m[i+1][j], m[j][j] - m[i][j-1]);
}

public static String recomand(int[][] m, int from, int to) {


if (m[from][to]==m[from][from] - m[from+1][to])
return "right";
else
return "left";
}

Você também pode gostar