Search Results

Search found 1337 results on 54 pages for 'sorted'.

Page 5/54 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Find existence of number in a sorted list in constant time? (Interview question)

    - by Rich
    I'm studying for upcoming interviews and have encountered this question several times (written verbatim) Find or determine non existence of a number in a sorted list of N numbers where the numbers range over M, M N and N large enough to span multiple disks. Algorithm to beat O(log n); bonus points for constant time algorithm. First of all, I'm not sure if this is a question with a real solution. My colleagues and I have mused over this problem for weeks and it seems ill formed (of course, just because we can't think of a solution doesn't mean there isn't one). A few questions I would have asked the interviewer are: Are there repeats in the sorted list? What's the relationship to the number of disks and N? One approach I considered was to binary search the min/max of each disk to determine the disk that should hold that number, if it exists, then binary search on the disk itself. Of course this is only an order of magnitude speedup if the number of disks is large and you also have a sorted list of disks. I think this would yield some sort of O(log log n) time. As for the M N hint, perhaps if you know how many numbers are on a disk and what the range is, you could use the pigeonhole principle to rule out some cases some of the time, but I can't figure out an order of magnitude improvement. Also, "bonus points for constant time algorithm" makes me a bit suspicious. Any thoughts, solutions, or relevant history of this problem?

    Read the article

  • Opening images sorted by modification date/size/type/etc.

    - by menino bolinho
    Suppose I have a folder with pictures in them. If I sort them by name once I open one with Image Viewer and navigate to the others the order is respected. But if I sort my files by modification date, for example, I can't do that. Basically, the default Image Viewer only lets you navigate images by name. According to this post on the ubuntuforums this has been an issue since 2007! Is there a good/easy way to fix it? Seems like such a trivial thing to me.

    Read the article

  • Finding the median of the merged array of two sorted arrays in O(logN)?

    - by user176517
    Refering to the solution present at MIT handout I have tried to figure out the solution myself but have got stuck and I believe I need help to understand the following points. In the function header used in the solution MEDIAN -SEARCH (A[1 . . l], B[1 . . m], max(1,n/2 - m), min(l, n/2)) I do not understand the last two arguments why not simply 1, l why the max and min respectively. Thanking You.

    Read the article

  • NSArrayController that is sorted and unique (no duplicates) for use in a pop-up in a core-data app

    - by Douglas Weaver
    I have core data app with an entity OBSERVATION that has as one of its attributes DEALNAME. I want to reference through Interface Builder or by making custom modifications to an NSArrayController a list of unique sorted dealnames so that I can use them in a pop-up. I have attempted to use @distinctUnionOfSets (and @distinctUnionOfArrays) but am unable to locate the proper key sequence. I can sort the ArrayController by providing a sort descriptor, but do not know how to eliminate duplicates. Are the @distinct... keys the right methodology? It would seem to provide the easiest way to optimize the use of IB. Is there a predicate form for removing duplicates? Or do I need to use my custom controller to extract an NSSet of the specific dealnames, put them back in an array and sort it and reference the custom array from IB? Any help would be appreciated. I am astounded that other have not tried to create a sorted-unique pop-up in tableviews.

    Read the article

  • Data access strategy for a site like SO - sorted SQL queries and simultaneous updates that affect th

    - by Kaleb Brasee
    I'm working on a Grails web app that would be similar in access patterns to StackOverflow or MyLifeIsAverage - users can vote on entries, and their votes are used to sort a list of entries based on the number of votes. Votes can be placed while the sorted select queries are being performed. Since the selects would lock a large portion of the table, it seems that normal transaction locking would cause updates to take forever (given enough traffic). Has anyone worked on an app with a data access pattern such as this, and if so, did you find a way to allow these updates and selects to happen more or less concurrently? Does anyone know how sites like SO approach this? My thought was to make the sorted selects dirty reads, since it is acceptable if they're not completely up to date all of the time. This is my only idea for possibly improving performance of these selects and updates, but I thought someone might know a better way.

    Read the article

  • Is there anything like memcached, but for sorted lists?

    - by depesz
    I have a situation where I could really benefit from having system like memcached, but with the ability to store (per each key) sorted list of elements, and modifying the list by addition of values. For example: something.add_to_sorted_list( 'topics_list_sorted_by_title', 1234, 'some_title') something.add_to_sorted_list( 'topics_list_sorted_by_title', 5436, 'zzz') something.add_to_sorted_list( 'topics_list_sorted_by_title', 5623, 'aaa') Which I then could use like this: something.get_list_size( 'topics_list_sorted_by_title' ) // returns 3 something.get_list_elements( 'topics_list_sorted_by_title', 1, 10 ) // returns: 5623, 1234, 5436 Required system would allow me to easily get items count in every array, and fetch any number of values from the array, with the assumption that the values are sorted using attached value. I hope that the description is clear. And the question is relatively simple: is there any such system?

    Read the article

  • Efficient algorithm to find a the set of numbers in a range

    - by user133020
    If I have an array of sorted numbers and every object is one of the numbers or multiplication. For example if the sorted array is [1, 2, 7] then the set is {1, 2, 7, 1*2, 1*7, 2*7, 1*2*7}. As you can see if there's n numbers in the sorted array, the size of the set is 2n-1. My question is how can I find for a given sorted array of n numbers all the objects in the set so that the objects is in a given interval. For example if the sorted array is [1, 2, 3 ... 19, 20] what is the most efficient algorithm to find the objects that are larger than 1000 and less than 2500 (without calculating all the 2n-1 objects)?

    Read the article

  • Sorted exsl:node-set. Return node by it position.

    - by kalininew
    Good afternoon, gentlemen. Help me solve a very simple task. I have a set of nodes <menuList> <mode name="aasdf"/> <mode name="vfssdd"/> <mode name="aswer"/> <mode name="ddffe"/> <mode name="ffrthjhj"/> <mode name="dfdf"/> <mode name="vbdg"/> <mode name="wewer"/> <mode name="mkiiu"/> <mode name="yhtyh"/> and so on... </menuList> I have it sorted now this way <xsl:variable name="rtf"> <xsl:for-each select="//menuList/mode"> <xsl:sort data-type="text" order="ascending" select="@name"/> <xsl:value-of select="@name"/> </xsl:for-each> </xsl:variable> Now I need to get an arbitrary element in the sorted array to the number of its position. I write code <xsl:value-of select="exsl:node-set($rtf)[position() = 3]"/> and get a response error. How to do it right?

    Read the article

  • Given a 2d array sorted in increasing order from left to right and top to bottom, what is the best w

    - by Phukab
    I was recently given this interview question and I'm curious what a good solution to it would be. Say I'm given a 2d array where all the numbers in the array are in increasing order from left to right and top to bottom. What is the best way to search and determine if a target number is in the array? Now, my first inclination is to utilize a binary search since my data is sorted. I can determine if a number is in a single row in O(log N) time. However, it is the 2 directions that throw me off. Another solution I could use, if I could be sure the matrix is n x n, is to start at the middle. If the middle value is less than my target, then I can be sure it is in the left square portion of the matrix from the middle. I then move diagnally and check again, reducing the size of the square that the target could potentially be in until I have honed in on the target number. Does anyone have any good ideas on solving this problem? Example array: Sorted left to right, top to bottom. 1 2 4 5 6 2 3 5 7 8 4 6 8 9 10 5 8 9 10 11

    Read the article

  • Sorted list of file names in a folder in VBA?

    - by Karsten W.
    Is there a way to get a sorted list of file names of a folder in VBA? Up to now, I arrived at Dim fso As Object Dim objFolder As Object Dim objFileList As Object Dim vFile As Variant Dim sFolder As String sFolder = "C:\Docs" Set fso = CreateObject("Scripting.FileSystemObject") Set objFolder = fso.GetFolder(sFolder) Set objFileList = objFolder.Files For Each vFile In objFileList ' do something ' Next vFile but it is crucial to be sure the processing order of the for loop is determined by the file names... Any help appreciated!

    Read the article

  • How to determine if a List is sorted in Java?

    - by FarmBoy
    I would like a method that takes a List<T> where T implements Comparable and returns true or false depending on whether the list is sorted or not. What is the best way to implement this in Java? It's obvious that generics and wildcards are meant to be able to handle such things easily, but I'm getting all tangled up. It would also be nice to have an analogous method to check if the list is in reverse order.

    Read the article

  • O(log n) algorithm for computing rank of union of two sorted lists?

    - by Eternal Learner
    Given two sorted lists, each containing n real numbers, is there a O(log?n) time algorithm to compute the element of rank i (where i coresponds to index in increasing order) in the union of the two lists, assuming the elements of the two lists are distinct? I can think of using a Merge procedure to merge the 2 lists and then find the A[i] element in constant time. But the Merge would take O(n) time. How do we solve it in O(log n) time?

    Read the article

  • O(log n) algorithm to find the element having rank i in union of pre-sorted lists

    - by Eternal Learner
    Given two sorted lists, each containing n real numbers, is there a O(log?n) time algorithm to compute the element of rank i (where i coresponds to index in increasing order) in the union of the two lists, assuming the elements of the two lists are distinct? I can think of using a Merge procedure to merge the 2 lists and then find the A[i] element in constant time. But the Merge would take O(n) time. How do we solve it in O(log n) time?

    Read the article

  • Why does sorted list have to have a key value pair?

    - by clawson
    If I just want a sorted list of just dates, integers, or doubles is it really necessary to have to define a SortedList(of Integer, Integer)? Seems intriguing to me, but may just be trival. I'd prefer just to use a SortedList(of Integer). (This question is in relation to the .Net generic collections)

    Read the article

  • What is the best way to declare sorted association in grails domain classes ?

    - by fabien7474
    It seems that there are two different ways of declaring sorted associations in Grails : Method 1 (see here) using default sort order class Book { String title } class Author { static hasMany = [books : Book] static mapping = { books sort: "title"} } Method 2 (see here) using SortedSet class Book implements Comparable { String title int compareTo(obj) { title <=> obj.title } } class Author { SortedSet books static hasMany = [books : Book] } I am not sure which one to use and what is the difference (if any), pros and cons between using one against the other. I would appreciate any clarification. Thank you

    Read the article

  • python: a way to get an exhaustive, sorted list of keys in a nested dictionary?

    - by saidimu
    exhaustive: - all keys in the dictionary, even if the keys are in a nested dictionary that is a value to a previous-level dictionary key. sorted: - this is to ensure the keys are always returned in the same order The nesting is arbitrarily deep. A non-recursive algorithm is preferred. level1 = { 'a' : 'aaaa', 'level2_1' : {'b': 'bbbbb', 'level3': {'c': 'cccc', 'd': 'dddddd'} }, 'level2_2' : { 'z': 'zzzzzzz' } }

    Read the article

  • How can I find the boundaries of a subset of a sorted list?

    - by Alex
    I have the following dilemma: I have a list of strings, and I want to find the set of string which start with a certain prefix. The list is sorted, so the naive solution is this: Perform binary search on the prefixes of the set, and when you find an element that starts with the prefix, traverse up linearly until you hit the top of the subset. This runs in linear time, however, and I was wondering if anyone can suggest a more efficient way to do it.

    Read the article

  • Why is processing a sorted array faster than an unsorted array?

    - by GManNickG
    Here is a piece of code that shows some very peculiar performance. For some strange reason, sorting the data miraculously speeds up the code by almost 6x: #include <algorithm> #include <ctime> #include <iostream> int main() { // generate data const unsigned arraySize = 32768; int data[arraySize]; for (unsigned c = 0; c < arraySize; ++c) data[c] = std::rand() % 256; // !!! with this, the next loop runs faster std::sort(data, data + arraySize); // test clock_t start = clock(); long long sum = 0; for (unsigned i = 0; i < 100000; ++i) { // primary loop for (unsigned c = 0; c < arraySize; ++c) { if (data[c] >= 128) sum += data[c]; } } double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC; std::cout << elapsedTime << std::endl; std::cout << "sum = " << sum << std::endl; } Without std::sort(data, data + arraySize);, the code runs in 11.54 seconds. With the sorted data, the code runs in 1.93 seconds. Initially I thought this might be just a language or compiler anomaly. So I tried it Java... import java.util.Arrays; import java.util.Random; public class Main { public static void main(String[] args) { // generate data int arraySize = 32768; int data[] = new int[arraySize]; Random rnd = new Random(0); for (int c = 0; c < arraySize; ++c) data[c] = rnd.nextInt() % 256; // !!! with this, the next loop runs faster Arrays.sort(data); // test long start = System.nanoTime(); long sum = 0; for (int i = 0; i < 100000; ++i) { // primary loop for (int c = 0; c < arraySize; ++c) { if (data[c] >= 128) sum += data[c]; } } System.out.println((System.nanoTime() - start) / 1000000000.0); System.out.println("sum = " + sum); } } with a similar but less extreme result. My first thought was that sorting brings the data into cache, but my next thought was how silly that is because the array was just generated. What is going on? Why is a sorted array faster than an unsorted array? The code is summing up some independent terms, the order should not matter.

    Read the article

  • How to use YQL to merge 2 RSS feeds sorted by pubDate?

    - by jnman
    Seeing that YQL is being promoted as a good way to do things, I was curious as to how to use YQL to fetch and merge 2 different feeds into one (sorted by pubDate). It's pretty trivial to fetch 2 feeds but it turns out that the feeds are just concatenated together and not merged. Here's the sample code. select channel.title,channel.link,channel.item.title,channel.item.link from xml where url in( 'http://code.flickr.com/blog/feed/rss/', 'http://feeds.delicious.com/v2/rss/codepo8?count=15', 'http://www.stevesouders.com/blog/feed/rss', 'http://www.yqlblog.net/blog/feed/', 'http://www.quirksmode.org/blog/index.xml' )

    Read the article

  • How do I get the sequence of numbers in a sorted-set that are between two integers in clojure?

    - by Greg Rogers
    Say I have a sorted-set of integers, xs, and I want to retrieve all the integers in xs that are [x, y), ie. between x and y. I can do: (select #(and (>= % x) (< % y)) xs) But this is inefficient - O(n) when it could be O(k log n) where k is the number of integers returned. I am just learning clojure so here is how I would do it in C++: set<int>::iterator first = xs.lower_bound(x); set<int>::iterator last = xs.upper_bound(y); for (; first != last; ++first) // do something with *first Can I do this in clojure?

    Read the article

  • Java List Sorting: Is there a way to keep a list permantly sorted automatically like TreeMap?

    - by david
    In Java you can build up an ArrayList with items and then call: Collections.sort(list, comparator); Is there anyway to pass in the Comparator at the time of List creation like you can do with TreeMap? The goal is to be able add an element to the list and instead of having it automatically appended to the end of the list, the list would keep itself sorted based on the Comparator and insert the new element at the index determined by the Comparator. So basically the list might have to re-sort upon every new element added. Is there anyway to achieve this in this way with the Comparator or by some other similar means? Thanks.

    Read the article

  • How do I get the sequence of numbers in a sorted-set that are between two integers in clojure?

    - by Greg Rogers
    Say I have a sorted-set of integers, xs, and I want to retrieve all the integers in xs that are [x, y), ie. between x and y. I can do: (select #(and (>= % x) (< % y)) xs) But this is inefficient - O(n) when it could be O(log n), I expect the number of elements returned to be small. Using take-while and drop-while would let me exit once I've reached y, but I still can't jump to x efficiently. I am just learning clojure so here is how I would do it in C++: set<int>::iterator first = xs.lower_bound(x); set<int>::iterator last = xs.lower_bound(y); for (; first != last; ++first) // do something with *first Can I do this in clojure?

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >