• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
 
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 intbinarySearchNear(int[]arr,intend,intvalue){ 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
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...