2

I'm looking for an algorithm to sort an array, but not by moving the values. Rather, I'd like to delete as few values as possible and end up with a sorted list. Basically I want to find the longest ascending sub-array.

To illustrate:

1 4 5 6 7 2 3 8

Should become (2 removes)

1 4 5 6 7 8

And not (5 removes)

1 2 3

I can see how I can do this in a naive way, i.e. by recursively checking both the 'remove' and 'dont remove' tree for each element. I was just wondering if there was a faster / more efficient way to do this. Is there a common go-to algorithm for this kind of problem?

2
  • 1
    Why not check if each succeeding element is larger than the previous element (and delete it if it's not)? Commented Apr 8, 2013 at 10:29
  • 2
    @ASGM: Because that "greedy" algorithm does not give the longest ascending sub-array. Consider: 9 1 2 3 4 5 6 7 8. Commented Apr 8, 2013 at 10:31

2 Answers 2

9

You're looking for the longest increasing subsequence problem. There is an algorithm that solves it in O(n log n) time.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thanks! What a difference it makes to know the correct terms to search for ;)
3

There is one O(NlogN) algorithm from the site which is faster than the recursive algorithm .

http://www.algorithmist.com/index.php/Longest_Increasing_Subsequence

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.