r/computerscience • u/ShadowGuyinRealLife • Jan 29 '26
Discussion What Process would get the First Few Items in a List With Few Writes?
Say you had a list of N items and you wanted to get the first X items in sorted order where N >>> X. So like if you wanted to sort the first 3000 items of a list more than 10,000,000 items long, the input would be the list of items, X (in this case 3000) and the output should be a permutation of the original list with the 1st item being the smallest, the 2nd item being the next smallest, the 3rd item being the 3rd smallest of the list.... and the 3000th item being the 3000th smallest with the rest of the list just containing the rest of the items in any order. What is a way to accomplish this in as few writes as possible? If I am misunderstanding something or misusing a term, the reason I cannot mention my confusion is when I try to explain, the "post" button gets greyed out.
You could just sort the list. For example quicksort or insertion sort could be run on the list and not only would the first 3000 items be sorted, but the whole list would be. But if you are trying to minimize writes, I feel that sorting the entire list is a massive waste.
I asked on a YouTube comment (sorry I can't find it, YouTube only lets you see a few comments you post on a video, but if you post a dozen there doesn't seem to be a way to find it) and I got a weird answer and he never replied when I asked for clarification.
So the list you want is an array of items and you want the first 3000 to be sorted right? What do you mean by fewest writes? If you mean fewest writes to the array itself, I can give you a C or Common LISP code. Do you mean fewest writes to the memory? If what you are really trying to minimize is not writes to the array of the data but system calls, then there isn't a best answer besides "it's complicated"
10
u/Firzen_ Jan 29 '26
Fewest writes would probably be doing a slightly modified selection sort into a separate copy of the list.
Search through the list and select the smallest element that's larger than the last element you selected.
You basically only do one write for each element you want, so linear in the number of writes.
The disadvantage is that you need to traverse the entire list every time, but if we only care about the number of writes this is likely optimal.
3
u/DogObvious5799 Jan 29 '26
You can find the 3000th largest element with quick select, then partition the array around that element, then sort the relevant partition
3
u/kohugaly Jan 29 '26
Make a max heap (for example, a binary heap) with capacity X and fill it with first X items. You can reuse the start of the list as the storage. Loop through the rest of array, and compare each element with the max element in the heap (these are only read operations). If it is smaller, pop the maximum from the heap ( log(X) writes, for binary heap), push the new element onto the heap (one write), and put the old maximum in its place (one write).
After you went through the array, the max heap will contain the X smallest elements. You can then sort them.
In the worst case, the list is in descending order and therefore, each item will get pushed into the queue and pass through it (which takes Log(X) steps). So that's O( N*log(X) ).
If you care about the fewest "writes to the list", then you can do the above, but use separately allocated heap, that stores indices instead of the actual elements. That way you are not writing to the list while searching for the smallest X elements. You only perform X swaps, to put the X smallest elements to the start of the list.
This is the general way to think about these kinds of "find X elements with given property in looooong list". Treat the searching as a stream of elements that come one by one. Use some sort of queue/heap with capacity X to keep track of the elements you wish to keep.
8
u/flumsi Jan 29 '26
I mean you don't need to sort the entire list. You can use indices to sort only the relevant part.
1
u/qwaai Jan 29 '26
Some good answers so far. My approach would probably be:
Iteratively partition the left-most partition of the array until you have a partition the size of X. If you're slightly under or over do a heap sort to find the remaining elements. This will take roughly 2*N time. Consider the case where your partition cuts the size of the remaining array in half each time, on average. Your first iteration removes half of the original elements from contention and takes N time. Your next iteration takes N/2 time and removes another half of the elements. And so on. The series N + N/2 + N/4 ... converges to 2N operations. The logic here is the same that puts the average runtime of
Select(k)at N (finding the k largest element in an array of size n).Obviously a poor ordering at the start will shove this to N2, but assuming it's fairly random I suspect this will come out ahead of most other solutions.
If the cost of a single write is what's important, I suspect that a standard selection sort is the best. You can do a lot of reads very quickly and minimize the number of swaps or writes by ensuring that you only do X writes.
1
u/cbarrick Jan 29 '26 edited Jan 30 '26
If
sortis selection sort, it does at mostnswaps.Use that to sort the first
nelements. Then check if any of the remaining elements are smaller than those firstn, and swap them into sorted place.Best case is an already sorted list (0 swaps). Worst case is a reverse sorted list. (
n * len(input)swaps).Each new element that you want to put into the sorted group in the front requires one swap to move it in, plus one swap for each element in the group that needs to get shifted back. So worst case is
nswaps to move something to the front.You can do better if the input is a linked list, because you can put elements into place directly without shifting.
The other post about using a binary heap may be better.
Edit: Just swap the largest of your
nselected elements out. You don't need to sort until the end. Eliminates the swaps around shifting. Worst case becomesO(len(input)).1
u/tilrman Jan 30 '26
If you have an unlimited amount of memory (i.e., scratch space), you can do it in at most 6000 writes. In the worst case, you must overwrite the first 3000 indices and then put the values that had been at those indices back into the list.
If you can store one element in memory, you can still do it in 6000 writes, but slower.