Sie sind auf Seite 1von 2

The Longest Increasing Sequence

import java.util.Arrays;

public class LongestIncreasingSequence {


// ‫חיפוש בינארי של מיקום המספר בתוך מערך ממוין‬
// if value does not appear into the array, but
// arr[0] < value <arr[size] the function returns index,
// arr[index-1] < value < arr[index]
// if value<arr[0] the function returns zero
// if value>arr[end] the function returns end+1

public static int binarySearchNear(int []arr, int end, int value){


int low = 0, high = end;
if (value<arr[0]) return 0;
if(value>arr[end]) return end+1;
while (low <=high){
int middle = (low + high)/2;
if (low == high) {
if (arr[low] == value) return low;
else return low;
}
else {
if (arr[middle] == value){//value was found
return middle;
}
// value suppose to be left
if (value < arr[middle]){
high = middle;
}
// value suppose to be right
else{
low = middle+1;
}
}
}
return -1;
}
‫המערך הממוין הארוך ביותר‬-‫ חיפוש אורכו של תת‬//
// array contains different integer numbers,assumption: arr.length>2
// calculation of the length of largest increment subsequence
public static int LISLength(int [] arr){
int size = arr.length;
int d[] = new int[size];
d[0] = arr[0];
int end = 0;
for (int i=1; i<size; i++){
int index = binarySearchNear(d, end, arr[i]);
if (index<=end) d[index] = arr[i];
else{
end++;
d[end] = arr[i];
}
}
return end+1;
}

1
‫המערך הממוין הארוך ביותר‬-‫ חיפוש של תת‬//
// array contains integer different numbers,assumption: arr.length>2
// calculation of the length of largest increment subsequence
public static int[] LIS(int [] arr){
int size = arr.length;
int mat [][] = new int[size][size];
int d[] = new int[size];
mat [0][0] = arr[0];
d[0] = arr[0];
int end = 0;
for (int i=1; i<size; i++){
int index = binarySearchNear(d, end, arr[i]);
mat[index][index] = arr[i];
for(int j=0; j<index; j++) mat[index][j]=mat[index-1][j];
for(int j=0; j<index; j++) d[j]=mat[index-1][j];
d[index] = arr[i];
if (index>end) end++;
}
int ans[] = new int[end+1];
for(int j=0; j<=end; j++) ans[j]=mat[end][j];
return ans;
}

‫תוכנית בדיקה‬//
public static void main(String[] args) {
int size = 10;
int [] arr = MyLibrary.randomIntArrayOfDiffNumbers(size);
//Arrays.sort(arr);
MyLibrary.printIntegerArray(arr);
//int index = binarySearchNear(arr, arr.length-1,3);
//System.out.println("index = "+index);
int m = LISLength(arr);
System.out.println("m = "+m);
int [] d = LIS(arr);
MyLibrary.printIntegerArray(d);
}
}

Das könnte Ihnen auch gefallen