0

Problem- Given a sorted doubly link list and two numbers C and K. You need to decrease the info of node with data K by C and insert the new node formed at its correct position such that the list remains sorted.

I would think of insertion sort for such problem, because, insertion sort at any instance looks like, shown bunch of cards,

enter image description here

that are partially sorted. For insertion sort, number of swaps is equivalent to number of inversions. Number of compares is equivalent to number of exchanges + (N-1).

So, in the given problem(above), if node with data K is decreased by C, then the sorted linked list became partially sorted. Insertion sort is the best fit.

Another point is, amidst selection of sorting algorithm, if sorting logic applied for array representation of data holds best fit, then same sorting logic should holds best fit for linked list representation of same data.

For this problem, Is my thought process correct in choosing insertion sort?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Perhaps [this](https://stackoverflow.com/questions/1525117/whats-the-fastest-algorithm-for-sorting-a-linked-list) answer would help? – Richard Dec 27 '16 at 00:50
  • Is `decrease(int nodeWithK, int decreaseByC)` operation a one time operation? – Cahit Gungor Dec 27 '16 at 00:55
  • @Richard Are you saying that, if this data given in problem was in array representation, then *Insertion sort* would be best. If this data is in linked list , then *Merge sort* is best for partially sorted data? – overexchange Dec 27 '16 at 00:57
  • @CahitGungor Yes `decrease(int nodeWithK, int decreaseByC)` is O(1) operation – overexchange Dec 27 '16 at 00:58
  • If this is a card deck (52 items or 54 with jokers) or double deck (104-108), which algorithm is fastest is moot. The data set size N is fixed, and fairly small. – Kaz Dec 27 '16 at 00:58
  • @overexchange: I don't really understand the question as you've posed it. But, at least in the general context, is the "best" way to sort a linked list depends on the degree of memory fragmentation, among other factors. – Richard Dec 27 '16 at 01:00

2 Answers2

1

Maybe you mean something else, but insertion sort is not the best algorithm, because you actually don't need to sort anything. If there is only one element with value K then it doesn't make a big difference, but otherwise it does.

So I would suggest the following algorithm O(n), ignoring edge cases for simplicity:

  1. Go forward in the list until the value of the current node is > K - C.
  2. Save this node, all the reduced nodes will be inserted before this one.
  3. Continue to go forward while the value of the current node is < K
  4. While the value of the current node is K, remove node, set value to K - C and insert it before the saved node. This could be optimized further, so that you only do one remove and insert operation of the whole sublist of nodes which had value K.
maraca
  • 8,468
  • 3
  • 23
  • 45
  • So this is just `O(L)` to go forward and O(1) replace before saved node. Is this correct? Assume `L` nodes before node have value `K` – overexchange Dec 27 '16 at 01:12
  • You mean O(n) where n is the number of nodes, then it is correct. K is the node value, you don't know where in the list it is and K could be much bigger than n. If you want to look up values and get the corresponding nodes, then a hash map is probably a good choice or an array if the possible values for K are very limited. – maraca Dec 27 '16 at 01:15
  • What about using [skipList](https://en.wikipedia.org/wiki/Skip_list) for fast search? – overexchange Dec 27 '16 at 01:28
1

If these decrease operations can be batched up before the sorted list must be available, then you can simply remove all the decremented nodes from the list. Then, sort them, and perform a two-way merge into the list.

If the list must be maintained in order after each node decrement, then there is little choice but to remove the decremented node and re-insert in order.

Doing this with a linear search for a deck of cards is probably acceptable, unless you're running some monstrous Monte Carlo simulation involving cards, that runs for hours or day, so that optimization counts.

Otherwise the way we would deal with the need to maintain order would be to use an ordered sequence data structure: balanced binary tree (red-black, splay) or a skip list. Take the node out of the structure, adjust value, re-insert: O(log N).

Kaz
  • 55,781
  • 9
  • 100
  • 149