*
The moose likes Beginning Java and the fly likes A time complexity question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "A time complexity question" Watch "A time complexity question" New topic
Author

A time complexity question

Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
What's the time complexity to remove an element from a sorted and unsorted linked list? I have heard different answers. I mean for remove, do we need to concern with the "find" part? Because in an unsorted linked list, you need to find it first, which takes O(n), though the remove action itself just takes 1. So do we say N or 1? Sorted and unsorted have the same time complexity I think. Insert also has to do with "find".

Thanks!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

In a linked list, the elements are only accessible by walking through the list (i.e. no random-access by index as in an array). It doesn't matter if the list is sorted or not, you'll have to walk the list until you find the element to remove, which takes O(N).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
In an ArrayList, however, it would be O(logn) if it is already sorted and O(n) if not sorted. If you already know the index, it would be constant time. To obtain the logarithmic time you would need a binary search to find the element to remove.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Campbell Ritchie wrote:In an ArrayList, however, it would be O(logn) if it is already sorted and O(n) if not sorted. If you already know the index, it would be constant time. To obtain the logarithmic time you would need a binary search to find the element to remove.


Actually, no. Removing an element from an ordered array or ArrayList takes linear time, because after you remove the element, you then have to move an average of N/2 elements down to fill the "hole". If order doesn't matter, then you can just move the last element, and that does take constant time; of course, then you might as well have been using a Set rather than a List in the first place.

Removing from a linked list does, indeed, always take constant time.


[Jess in Action][AskingGoodQuestions]
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Ernest Friedman-Hill wrote:
Removing from a linked list does, indeed, always take constant time.


Hi Ernest, why removing from a linked list always takes constant time? Because I kinda agree with Jesper that it will have to find the element to remove first, then actually do the remove action.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Cheryl Scodario wrote:
Ernest Friedman-Hill wrote:
Removing from a linked list does, indeed, always take constant time.


Hi Ernest, why removing from a linked list always takes constant time? Because I kinda agree with Jesper that it will have to find the element to remove first, then actually do the remove action.


Finding an object takes linear time; subsequently removing it takes constant time. For an array, finding takes linear time for unsorted, and logarithmic time for a sorted list; and removing takes linear time.

Removing does not always imply finding. For example, consider removing the first element of a list. For a linked list, it's done in constant time, and for an array or ArrayList, it takes linear time.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38044
    
  22
Yes, you are correct about linear time, and I was mistaken. Sorry.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Ernest Friedman-Hill wrote:
Cheryl Scodario wrote:
Ernest Friedman-Hill wrote:
Removing from a linked list does, indeed, always take constant time.


Hi Ernest, why removing from a linked list always takes constant time? Because I kinda agree with Jesper that it will have to find the element to remove first, then actually do the remove action.


Finding an object takes linear time; subsequently removing it takes constant time. For an array, finding takes linear time for unsorted, and logarithmic time for a sorted list; and removing takes linear time.

Removing does not always imply finding. For example, consider removing the first element of a list. For a linked list, it's done in constant time, and for an array or ArrayList, it takes linear time.


I see...How about insert in a sorted list? I always think of remove and insert very similar. But is insert implying find for sure? Because you need to find the right position to insert it. Then it would be N, linear time?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11163
    
  16

Not if you are inserting in the first position.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
fred rosenberger wrote:Not if you are inserting in the first position.


I am not talking about any special(best) case. So let's just say sorted list: insert and find (average case).
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

For sorted lists, for find/insert, both linked lists and array(list)s will give linear performance. For the linked list, the time is dominated by the find; for the array(list)s, it's dominated by the insert.
 
Consider Paul's rocket mass heater.
 
subject: A time complexity question
 
Similar Threads
How to sort a map descendingly
Printing out a linked list
Vector operation implementation why JDK preferred arrays over linkedlist
How would you design this java class
Way to Google