sorting

How do I sort the possibilities vector in 0/1 knapsack problem using the template min_max function I have written?

大城市里の小女人 提交于 2020-04-16 05:47:23
问题 SO already has multiple questions related to this topic. My question ain't 'bout the algorithm. It's 'bout sorting the final possibilities vector as per profit I wrote the following piece of code to solve the 0/1 knapsack problem: #include "algorithms.h" struct Item { int index = 1; int profit = 1; int weight = 1; Item() = delete; explicit Item(int i, int _profit, int w) { index = i; profit = _profit; weight = w; } bool operator<(const Item& item) { return this->profit < item.profit; } bool

Android Room Dao: Order By CASE not working

江枫思渺然 提交于 2020-04-16 02:58:06
问题 I have a Room database using Dao to process queries etc. I am using static (non live data) function to retrieve results via the query, all works well when I manual hard code the Order By values and column as below, however when passing params to the Dao to do the sorting, the Order By reverts back to default (order by id column) and does not retrieve results based on the passed sort param Hard coded Dao example works, results sorted by ASC or DESC @Query("SELECT * FROM cameras WHERE suburb

Sorting file contents numerically by field

北城以北 提交于 2020-04-15 19:28:27
问题 I am trying to write a BASH script to sort the contents of a file numerically according to a specific field in the file. The file is under /etc/group . All of the fields are colon-separated : . I have to sort the contents of /etc/group numerically based on the 3rd field. Example field: daemon:*:1:root What I'm trying so far: #!/bin/bash sort /etc/group -n | cut -f 3-3 -d ":" /etc/group This is getting me really close, but it only prints out a sorted list of 3rd field values (since cut

Relative sorting with minimum possible swaps

时光总嘲笑我的痴心妄想 提交于 2020-04-15 03:45:11
问题 Problem Statement: There are two arrays given A and B. We need to print the number of minimum swaps b/w i'th elements such that the both array will become strictly increasing. if arrays are already strictly increasing then print 0. if arrays are not possible to become strictly increasing then print -1. only ith element A can be swapped with ith element of B. There is a possibility that the elements may occur more than once. eg: Input: t=7 A=1 2 3 8 9 7 8 B=1 2 3 4 5 10 11 output 2 By swaping

Sorting with OrderBy, ThenBy

天涯浪子 提交于 2020-04-14 06:24:29
问题 I'm trying to sort several columns of a table depending of the previous sorted column. It works fine for the first two columns. But as soon as I sort the third column the second one loses its sort. As far as I know for now there must be a problem with my foreach loop. Here is my code for sorting: public List<object> inhaltSortieren(List<object> zuSortierendeListe, Dictionary<string, SortierRichtung> sortierung) { IOrderedEnumerable<object> sortierteListe = null; if (sortierung.First().Value =

How to manually sort a list of numbers in Python?

折月煮酒 提交于 2020-04-13 17:04:42
问题 Specs: Ubuntu 13.04, Python 3.3.1 Background: total beginner to Python, came across this "manual sorting" problem. What I was asked to do: "Have the user enter 3 numeric values and store them in 3 different variables. Without using lists or sorting algorithms, manually sort these 3 numbers from smallest to largest." What I was able to come up with: number = input("Please enter 3 numbers: ") number = list(number) a = int(number[0]) b = int(number[1]) c = int(number[2]) new_l = [] if a > b and

How to manually sort a list of numbers in Python?

青春壹個敷衍的年華 提交于 2020-04-13 17:04:40
问题 Specs: Ubuntu 13.04, Python 3.3.1 Background: total beginner to Python, came across this "manual sorting" problem. What I was asked to do: "Have the user enter 3 numeric values and store them in 3 different variables. Without using lists or sorting algorithms, manually sort these 3 numbers from smallest to largest." What I was able to come up with: number = input("Please enter 3 numbers: ") number = list(number) a = int(number[0]) b = int(number[1]) c = int(number[2]) new_l = [] if a > b and

Stable sort - do we really need it?

房东的猫 提交于 2020-04-11 04:46:07
问题 I do not understand the underlying problem that tries to solve the stable sorting algorithm. Arrays.sort(Object[]) Javadoc states: This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. But if elements are equal, they are not distringuishable from each other! If you swap two equal elements, this should not affect anything. This is the definition of equality. So, why do we need stability at all? UPDATE: My question is about Collections.sort(List<T>)

TreeSet Comparator failed to remove duplicates in some cases?

百般思念 提交于 2020-04-11 04:45:18
问题 I have the following comparator for my TreeSet: public class Obj { public int id; public String value; public Obj(int id, String value) { this.id = id; this.value = value; } public String toString() { return "(" + id + value + ")"; } } Obj obja = new Obj(1, "a"); Obj objb = new Obj(1, "b"); Obj objc = new Obj(2, "c"); Obj objd = new Obj(2, "a"); Set<Obj> set = new TreeSet<>((a, b) -> { System.out.println("Comparing " + a + " and " + b); int result = a.value.compareTo(b.value); if (a.id == b

TreeSet Comparator failed to remove duplicates in some cases?

余生长醉 提交于 2020-04-11 04:44:47
问题 I have the following comparator for my TreeSet: public class Obj { public int id; public String value; public Obj(int id, String value) { this.id = id; this.value = value; } public String toString() { return "(" + id + value + ")"; } } Obj obja = new Obj(1, "a"); Obj objb = new Obj(1, "b"); Obj objc = new Obj(2, "c"); Obj objd = new Obj(2, "a"); Set<Obj> set = new TreeSet<>((a, b) -> { System.out.println("Comparing " + a + " and " + b); int result = a.value.compareTo(b.value); if (a.id == b