sort [] = []
sort [x] = [x]
sort l = merge (sort $ take halfLength l) (sort $ drop halfLength l)
where halfLength = length l `div` 2
merge a1 [] = a1
merge [] a2 = a2
merge (x:a1) (y:a2)
| x >= y = y:(merge (x:a1) a2 )
| x < y = x:(merge a1 (y:a2))
The Big Programming Thread - Page 592
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
Prillan
Sweden350 Posts
| ||
Ropid
Germany3557 Posts
sort [] = [] | ||
solidbebe
Netherlands4921 Posts
Another interesting tidbit. There seems to be no difference in size between a singly linked list of size 10000 and a doubly linked list of size 10000 (both about 400 KB), yet the doubly linked list obviously stores an extra pointer for each node. Why isnt this showing up in the test results? | ||
Nesserev
Belgium2760 Posts
| ||
solidbebe
Netherlands4921 Posts
On February 23 2015 04:41 Nesserev wrote: Well, something could be wrong with your implementation ... or, it could be Java. It's probably Java. I'm pretty sure it's Java. It's Java. Yeah the implementation was actually given to us by the teacher so I doubt its that ![]() | ||
Ropid
Germany3557 Posts
On February 23 2015 04:41 Nesserev wrote: Well, something could be wrong with your implementation ... or, it could be Java. It's probably Java. I'm pretty sure it's Java. It's Java. The same happens in C/C++. ![]() There's rules for the alignment of the various data types. Things that are 4 byte sized have to start at 4 bytes boundaries, 1 byte at 1 byte, 8 byte at 8 bytes, etc. If you group one char or two chars or three chars etc. in one struct, the whole struct will snap to 8 bytes size by adding empty space after the chars. In Java, the references are 4 bytes size it seems. In the single-linked list, there's then one reference and one empty 4 byte space. The doubly-linked list has two references and no empty space. | ||
spinesheath
Germany8679 Posts
On February 23 2015 04:32 solidbebe wrote: I have a question for you guys. For a course on algorythms and data structures in java we have to run an experiment on memory usage of linked lists. My output is as follows. A singy linked list of size 10 used about 7 kilobytes, and a singly linked list of size 10000 used about 400 kilobytes. Clearly this is not linear space complexity, yet the space complexity of a linked list is supposed to be O(n). Does anyone have an idea of what is going on? Another interesting tidbit. There seems to be no difference in size between a singly linked list of size 10000 and a doubly linked list of size 10000 (both about 400 KB), yet the doubly linked list obviously stores an extra pointer for each node. Why isnt this showing up in the test results? What exactly is in those lists? 7 kb for 10 items seems quite generous. That's 700 bytes per item. Subtract a couple of pointers and you're still well beyond 600 bytes. If your items are 600 bytes each, having one pointer more or less really doesn't matter a whole lot, so that would hardly be a useful analysis. Also, even a linked list can be implemented with an array holding the list elements. And such an array would usually be doubled in size once it runs out of unused elements. So if it is implemented in that way, you usually wouldn't see a difference in size unless you increase the number of items beyond the next power of 2. Furthermore, there's also the issue of how you're measuring the size. Depending on the language, there can be more or less accurate methods. Sometimes you could be measuring extra space because of memory fragmentation and what not. | ||
solidbebe
Netherlands4921 Posts
On February 23 2015 05:16 spinesheath wrote: What exactly is in those lists? 7 kb for 10 items seems quite generous. That's 700 bytes per item. Subtract a couple of pointers and you're still well beyond 600 bytes. If your items are 600 bytes each, having one pointer more or less really doesn't matter a whole lot, so that would hardly be a useful analysis. Also, even a linked list can be implemented with an array holding the list elements. And such an array would usually be doubled in size once it runs out of unused elements. So if it is implemented in that way, you usually wouldn't see a difference in size unless you increase the number of items beyond the next power of 2. Furthermore, there's also the issue of how you're measuring the size. Depending on the language, there can be more or less accurate methods. Sometimes you could be measuring extra space because of memory fragmentation and what not. Youre right actually. These lists store integers so the size is way off. The memory is calculated just subtracting the runtime free memory from the runtime totalmemory. A lot of those KBs are not the lists. Then again it keeps producing the same results, so memory thats not the list should be about the same every time as well. | ||
spinesheath
Germany8679 Posts
On February 23 2015 05:25 solidbebe wrote: Youre right actually. These lists store integers so the size is way off. The memory is calculated just subtracting the runtime free memory from the runtime totalmemory. A lot of those KBs are not the lists. Then again it keeps producing the same results, so memory thats not the list should be about the same every time as well. That method of measuring memory probably is quite inaccurate due to memory fragmentation and other factors. I don't know how the runtime free/total memory are calculated in detail, but my guess is it really is only an estimate. | ||
windzor
Denmark1013 Posts
On February 23 2015 04:32 solidbebe wrote: I have a question for you guys. For a course on algorythms and data structures in java we have to run an experiment on memory usage of linked lists. My output is as follows. A singy linked list of size 10 used about 7 kilobytes, and a singly linked list of size 10000 used about 400 kilobytes. Clearly this is not linear space complexity, yet the space complexity of a linked list is supposed to be O(n). Does anyone have an idea of what is going on? Another interesting tidbit. There seems to be no difference in size between a singly linked list of size 10000 and a doubly linked list of size 10000 (both about 400 KB), yet the doubly linked list obviously stores an extra pointer for each node. Why isnt this showing up in the test results? First of, it is impossible to measure memory consumption of data structures in java. With the way garbage collection works, you cannot do any trustworthy measurements which you can base any conclussion on, I've tried. But, if you want to make any conclussion, first of you need more than two measurements. 7 kB to 400 kB is always linear. The memory consumption of the JVM is just 7 kB then... You are remembering to enforce GC like 10 times before printing the memory usage? For the double linked vs single linked lists, 10000 pointers are is approximatly the size og 10 kB. With 400 kB and java this is within the error margin of +-90% Furtmorefor memory usage bigger is better. You need more than 10,000 objects to measure anything. You need like 1,000,000 objects if you want to have anything realistic. Remember each pointer is about 4-8 bytes so with 1,000,0000 objects the links will only be around 4 MB which is not alot. | ||
![]()
tofucake
Hyrule18982 Posts
| ||
nunez
Norway4003 Posts
| ||
solidbebe
Netherlands4921 Posts
![]() | ||
Prillan
Sweden350 Posts
On February 23 2015 04:14 Ropid wrote: There's a 'splitAt' that outputs a (,), and what's neat is Haskell understands (,) on the left side of a '=', so this works: sort [] = [] Nice, forgot about that one. | ||
Manit0u
Poland17203 Posts
PHP to the rescue ![]()
I know it's not the same... Here's a full merge-sort:
| ||
Khalum
Austria831 Posts
| ||
nunez
Norway4003 Posts
+ Show Spoiler [preamble] + #include<iostream> + Show Spoiler [merge] + namespace detail + Show Spoiler [merge sort] + namespace detail{ + Show Spoiler [compile time invocation] + using in=sequence<int[3],int[5],int[2],int[1]>; + Show Spoiler [run time print] + [jeh@gimli tl2]$ make | ||
Deleted User 3420
24492 Posts
do I specifically need to find a way to save my files as encrypted to avoid it? | ||
windzor
Denmark1013 Posts
On February 24 2015 02:37 travis wrote: if I used serialization to save data from my program to a file, will that file be readable/editable in some way that would allow users to manipulate the saved states? because I don't want that. do I specifically need to find a way to save my files as encrypted to avoid it? If the user have access rights to edit the file he can do it. There is nothing you can do about that. What you can do is create a checksum of the file and use that to detect if the user has changed the file. If so, you can yell at the user and say to stop editing that file. | ||
Blisse
Canada3710 Posts
| ||
| ||