I am trying to plant a garden. Certain plants are good for some plants and bad for others, and I am trying to find the best order of plants: most adjacent friends and no adjacent foes, as defined in this table:
Num Vegetable Friends Foes
1 Watermelon 7,4,3 8,6
2 Tomatoes 9,8,6,5,1 7
3 Sunflowers 7,6,11
4 Zucchini 9,7,3
5 Eggplant 9,6,2 7,10
6 Cucumbers 9,7,3 8,1
7 Corn 8,6,4,3,1 5,2
8 Cantelloupe 7,4,3 6,1
9 Bell peppers6,5,11,10,2
10 Swiss chard 2 5
11 Rhubarb 9,3
Assuming I have one of each plant and they are being planted in a row, how do I sort them (most efficiently) so that I will get the most adjacent friends and no adjacent enemies? There are tools online, but I am trying to understand the thought process and the implementation. Java is a language I know, so that would be the most helpful out of any language, but the concepts are the main point for me.
I am studying sorting in an algorithm course. I need a hint because I really don't know where to start with these next two questions. Any help is appreciated.
Suppose that all the integers are stored in a single linked list.
Write a Sort function whose input is a pointer to the first node of a list of unknown length.
List sorting (node * header)
Your method must return its elements in a sorted list. Again, your method must not create new nodes and must have a time complexity O (nlogn).
Suppose that two (unsorted) arrays A (0 … n – 1) and B (0 … n – 1) together contain 2n distinct numbers. Describe a method for constructing an array C (O..n – 1) such that for each index i E (O..n -1), A (i) is greater than exactly C (i) elements in the array B (0 … n – 1). Your method can use O (n) extra space and must have O (nlogn) running.