![]() You are seeking a way to sort the list in such a way so that order_list is relative to the original order. The current code assumes that at each step, order_list describes how to sort original_list relative to its current order. If all(=original_list for i in range(N)]): Order_list = sample(range(0,N), N) # ORDERįor j in range(i+1, len(order_list_map)): ![]() I think that an intermediary step is needed, but I can’t find it. My problem is that at the beginning the original_list is sorted (as in the example above), but then after the first step, my original list looks like that: and after the next switch it should look like. Which means that for example, at the step 2 and the location 0, I want to have the value original_list, and at the step 3 at the same location I wil have original_list, etc. This location varies with time, described by the order_list depending on the step. Let’s say that I have a table of size 5 corresponding to each object’s spatial location. Now, I would like to do it several times. Thank you very much, that works way better! # Now that the list has been sorted, remove the tagged indices if you want # but you can implement your own sorting algorithm that does all the swaps manually. # Sort original_list based on the tagged index. Original_list = (original_list, order_list_map) # tag each element of original_list with the index where it needs to end up # precompute a dictionary that can be used to quickly find the index of This solution takes O(n) memory (because of the creation of order_list_map and that each element of original_list needs to be tagged with its target index) and has runtime O(n log n) (because of the sorting algorithm that’s used). The following code does that and should still work even if original_list is a list of objects. If you must do the sorting by making swaps in the original list, then the idea would be to sort the original list based on the index of the corresponding element in the order list. Order_list = įinal_list = for i in order_list] This solution uses O(n) memory (because of the extra list that’s created) and has runtime O(n) (because you just do a single pass through order_list). To simplify the code, I’ll start counting from 0 since Python indices start at 0. ![]() The simplest solution I can think of doesn’t involve swaps at all it just constructs a new list. This changes the algorithm that needs to be done. Instead, you want the 8th element of the original list to become the 1st element of the final list. My original solution assumed that if the first number in the order list is 8, then you want the first element of the initial list to become the 8th element of the final list. I see what the problem is I misinterpreted what the order list is supposed to do in my original solution. I took the sorting algorithm on geekforgeeks.ĭo you know why in my example it does not work?ĮDIT: My next attempt was the following, taken from another website, and it is still not working, I have no clue why. Unfortunately, I don’t get the results I would like to have (order0=l at the end for this example). Order0 = np.array(sample(range(1,N+1), N)) # ORDER I tried something like this: import numpy as np Using quicksort, you can sort order_list one swap at a time and just apply the same swap at each step to initial_list. As for figuring out which sequence of swaps to do, you can implement quicksort, which has runtime O(n log n) for a list of size n. This technique will work even if initial_list is a list of objects. Checking the values, we find that initial_list is now and order_list is. Now that order_list has been sorted fully from smallest to largest, initial_list will have been sorted based on order_list. ![]() order_list, orderlist = order_list, orderlist Initial_list, initial_list = initial_list, initial_list To sort the initial list based on the order list, you should sort the order list from smallest to largest using swaps and use the same swaps for the initial list. If you really need to implement the swapping yourself instead of relying on any libraries or a built-in method, then here’s a trick. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |