sorting

What is the big-O complexity of this algorithm?

落花浮王杯 提交于 2020-01-14 15:45:34
问题 I have a function that I wrote below. This function is essentially a merge-sort. public static long nlgn(double[] nums) { if(nums.length > 1) { int elementsInA1 = nums.length/2; int elementsInA2 = nums.length - elementsInA1; double[] arr1 = new double[elementsInA1]; double[] arr2 = new double[elementsInA2]; for(int i = 0; i < elementsInA1; i++) arr1[i] = nums[i]; for(int i = elementsInA1; i < elementsInA1 + elementsInA2; i++) arr2[i - elementsInA1] = nums[i]; nlgn(arr1); nlgn(arr2); int i = 0

print numbers in ascending order from an n x m array whose rows are sorted

ε祈祈猫儿з 提交于 2020-01-14 13:56:49
问题 We have an n x m matrix whose rows are sorted, we need to print the numbers in the matrix in ascending order. The columns are not necessarly sorted. The solution that came to my mind was simple merging the rows in the matrix considering them as separate lists(basically the merging step in merge sort) which in merge sort takes O(n). I wanted to know what is the complexity of merging n separate arrays as in this case. I thought it would be O(n x m) but I am not sure. Also, what would be a

print numbers in ascending order from an n x m array whose rows are sorted

空扰寡人 提交于 2020-01-14 13:54:06
问题 We have an n x m matrix whose rows are sorted, we need to print the numbers in the matrix in ascending order. The columns are not necessarly sorted. The solution that came to my mind was simple merging the rows in the matrix considering them as separate lists(basically the merging step in merge sort) which in merge sort takes O(n). I wanted to know what is the complexity of merging n separate arrays as in this case. I thought it would be O(n x m) but I am not sure. Also, what would be a

Javascript - Sort Letter Number Combination

感情迁移 提交于 2020-01-14 12:58:09
问题 I have a combination of letters and numbers. For example: 2E12, 1Z10, 3D13, 3D03, FB14, X002, etc. I've tried a handful of methods to sort these strings, but nothing seems to work. parseInt works in clumps but the whole array is never sorted (it is a json array) and different results appear if the sort is run a second time. I've also tried using regex to replace all of the letters with numbers, but this creates a logic error. Each time a large letter in the middle of the string is replaced it

Naturally sort a list moving alphanumeric values to the end

送分小仙女□ 提交于 2020-01-14 11:36:41
问题 I have a list of strings I want to natural sort: c = ['0', '1', '10', '11', '2', '2Y', '3', '3Y', '4', '4Y', '5', '5Y', '6', '7', '8', '9', '9Y'] In addition to natural sort, I want to move all entries that are not pure number strings to the end. My expected output is this: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '2Y', '3Y', '4Y', '5Y', '9Y'] Do note that everything has to be natsorted - even the alphanumeric strings. I know I can use the natsort package to get what I

Naturally sort a list moving alphanumeric values to the end

无人久伴 提交于 2020-01-14 11:36:26
问题 I have a list of strings I want to natural sort: c = ['0', '1', '10', '11', '2', '2Y', '3', '3Y', '4', '4Y', '5', '5Y', '6', '7', '8', '9', '9Y'] In addition to natural sort, I want to move all entries that are not pure number strings to the end. My expected output is this: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '2Y', '3Y', '4Y', '5Y', '9Y'] Do note that everything has to be natsorted - even the alphanumeric strings. I know I can use the natsort package to get what I

How to get localeCompare to behave similarly to .sort(), so that all capital letters come first?

流过昼夜 提交于 2020-01-14 10:48:14
问题 I have an array of characters I want to sort: const arr = ['z', 'a', 'Z', 'A']; I want the sorted order to be: upper-case characters alphabetically, followed by lower-case characters alphabetically: ['A', 'Z', 'a', 'z'] This is trivial to accomplish using .sort() without any parameters: const arr = ['z', 'a', 'Z', 'A']; arr.sort(); console.log(arr); But how could localeCompare be used to determine the same relative position of each character? The caseFirst option looked promising: Whether

How to get localeCompare to behave similarly to .sort(), so that all capital letters come first?

雨燕双飞 提交于 2020-01-14 10:48:06
问题 I have an array of characters I want to sort: const arr = ['z', 'a', 'Z', 'A']; I want the sorted order to be: upper-case characters alphabetically, followed by lower-case characters alphabetically: ['A', 'Z', 'a', 'z'] This is trivial to accomplish using .sort() without any parameters: const arr = ['z', 'a', 'Z', 'A']; arr.sort(); console.log(arr); But how could localeCompare be used to determine the same relative position of each character? The caseFirst option looked promising: Whether

python sort OrderedDict keys chronologically

懵懂的女人 提交于 2020-01-14 10:47:06
问题 I have the following OrderedDict : from collections import OrderedDict a = OrderedDict() a['2016:April'] = 1 a['2016:January'] = 2 a['2017:February'] = 3 a['2015:November'] = 4 I would like to sort the dictionary by the keys in chronological order so that the result is: OrderedDict([('2015:November', 4), ('2016:January', 2), ('2016:April', 1), ('2017:February', 3)]) 回答1: You need to recreate the OrderedDict by passing sorted items; (OrderedDict remembers key insertion order) import calendar

How do I sort a key:list dictionary by values in list?

廉价感情. 提交于 2020-01-14 10:32:42
问题 I have a dictionary mydict = {'name':['peter', 'janice', 'andy'], 'age':[10, 30, 15]} How do I sort this dictionary based on key=="name" list? End result should be: mydict = {'name':['andy', 'janice', 'peter'], 'age':[15, 30, 10]} Or is dictionary the wrong approach for such data? 回答1: If you manipulate data, often it helps that each column be an observed variable (name, age), and each row be an observation (e.g. a sampled person). More on tidy data in this PDF link Bad programmers worry