sorting

Turbo sort - Time Limit Exceeded

て烟熏妆下的殇ゞ 提交于 2020-01-24 12:53:45
问题 Am trying to solve a Codechef problem (Turbo Sort). The problem is Given the list of numbers, you are to sort them in non decreasing order. Input t – the number of numbers in list, then t lines follow [t <= 10^6]. Each line contains one integer: N [0 <= N <= 10^6] Output Output given numbers in non decreasing order. Example Input: 5 5 3 6 7 1 Output: 1 3 5 6 7 My Solution is : l = [] t = input() MAX = 10**6 while t <= MAX and t != 0: n = input() l.append(n) t = t - 1 st = sorted(l) for x in

Finding the Index of sorted elements in Python Array

*爱你&永不变心* 提交于 2020-01-24 12:43:08
问题 I have seen answers to the question: Is it possible to arrange a numpy array (or python list) by using the indexes of the elements in decreasing order? (eg. Finding the Index of N biggest elements in Python Array / List Efficiently) A very concise answer seems to be (from above link): L = array([4, 1, 0, 8, 5, 2]) sorted(range(len(L)), key=lambda i:L[i]) This gives the position (in the original array) of the sorted elements. 8 --> 3 5 --> 4 4 --> 0 2 --> 5 1 --> 1 0 --> 2 So the answer is: [3

Sorting an array of pointers

不打扰是莪最后的温柔 提交于 2020-01-24 12:42:28
问题 Am I right in thinking it is fine to treat a pointer as an int for the purposes of sorting an array of pointers, e.g. qsort(ptrs, n, sizeof(void*), int_cmp); I want to sort the ptrs to ascertain whether there are any duplicates, irrespective of the type of thing the pointer is pointing to, so the qsort is a precursor to doing that. my int_cmp() is pretty standard, e.g. int int_cmp(const void *a, const void *b) { const int *ia = (const int *)a; // casting pointer types const int *ib = (const

Inserting an element into a sorted list

我的梦境 提交于 2020-01-24 11:34:11
问题 Ok I'm using getSharedPreferences to store my high score but before I fill it up I wanted to sort the scores into ascending order via and array, but if it finds a Score less than it in the first pos then it wont check the rest for the smallest? //function to add score to array and sort it public void addscoretoarray(int mScore){ for(int pos = 0; pos< score.length; pos++){ if(score[pos] > mScore){ //do nothing }else { //Add the score into that position score[pos] = mScore; break; } } sortArray

Sort a Numpy Python matrix progressively according to rows

馋奶兔 提交于 2020-01-24 11:34:06
问题 I have searched around and tried to find a solution to what seems to be a simple problem, but have come up with nothing. The problem is to sort a matrix based on its columns, progressively. So, if I have a numpy matrix like: import numpy as np X=np.matrix([[0,0,1,2],[0,0,1,1],[0,0,0,4],[0,0,0,3],[0,1,2,5]]) print(X) [[0 0 1 2] [0 0 1 1] [0 0 0 4] [0 0 0 3] [0 1 2 5]] I would like to sort it based on the first column, then the second, the third, and so on, to get a result like: Xsorted=np

Sorting table with just jquery, no other plugins

血红的双手。 提交于 2020-01-24 11:13:50
问题 So far i have got this bit of code: $('.link-sort-table').click(function(e) { var $sort = this; var $table = $('#sort-table'); var $rows = $('tbody > tr', $table); $rows.sort(function(a, b) { var keyA = $('td:eq(0)', a).text(); var keyB = $('td:eq(0)', b).text(); if ($($sort).hasClass('asc')) { return (keyA > keyB) ? 1 : -1; } else { return (keyA < keyB) ? 1 : -1; } }); $.each($rows, function(index, row) { $table.append(row); }); e.preventDefault(); }); thead { color: green; } tbody { color:

Sorting and finding values in other data frames

孤者浪人 提交于 2020-01-24 09:44:12
问题 I have a dataframe named commodities_3 . It contains 28 columns with different commodities and 403 rows representing end-of-month data. What I need is to find the position for each row separately: max value, min value, all other positives all other negatives Those index should then be used to locate the corresponding data in another dataframe with the same column and row characteristics called commodities_3_returns . These data should then be copied into 4 new dataframes (one dataframe for

Sorting a list of objects based on different data members in java

一个人想着一个人 提交于 2020-01-24 09:32:26
问题 I have this class: public class Friend { private String name; private String location; private String temp; private String humidity; public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getHumidity() { return humidity; } public void setHumidity(String humidity) { this.humidity = humidity; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; }

Sorting a list of objects based on different data members in java

微笑、不失礼 提交于 2020-01-24 09:32:26
问题 I have this class: public class Friend { private String name; private String location; private String temp; private String humidity; public String getTemp() { return temp; } public void setTemp(String temp) { this.temp = temp; } public String getHumidity() { return humidity; } public void setHumidity(String humidity) { this.humidity = humidity; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; }

How to find common keys in a list of dicts and sort them by value?

大憨熊 提交于 2020-01-24 07:28:22
问题 I want to create a finalDic which contains common keys and sum of their values myDic = [{2:1, 3:1, 5:2}, {3:4, 6:4, 2:3}, {2:5, 3:6}, ...] First find common keys commonkey = [{2:1, 3:1}, {2:3, 3:4}, {2:5, 3:6}] Then Sum and sort by their values finalDic= {3:11, 2,9} I've tried this and not even close what i want import collections myDic = [{2:1, 3:1, 5:2}, {3:4, 6:4, 2:3}, {2:5, 3:6}] def commonKey(x): i=0 allKeys = [] while i<len(x): for key in x[0].keys(): allKeys.append(key) i=i+1