Sorting a large number of unknown items with multiple stacks

Here is a problem I’m trying to solve for a personal project. I’m not exactly sure how best to even approach it.

Problem:

I have a single stack already populated with several thousands of unknown items. (Not completely unique items, maybe 300 different values. So it’s a stack with plenty of duplicates shuffled together in a random order.) I want to sort these items by moving them between a limited number of other stacks. (Maybe between 10-30 stacks at most.) An algorithm with less moves is ideal.

Limitations:

  • The initial stack is full of unknown values (except for the top item).
  • I can read the top item in a stack.
  • I can move the top item of a stack directly to the top of another stack.
  • I can check if a stack is empty.
  • I can remember and store all the items I have read and moved.

Based on the above limitations, all the items need to be moved once to another stack just so they can be read/scanned. Then I would know where and what everything is.

Once this is done I can do the real sorting. (This is the part I mainly need help with.) After I’ve read and moved all the items once, I can then calculate and queue up all the stack moves needed to actually sort the items.

Presorting and optimizing:

It might be too early to think about this, but I can potentially do some pre-sorting onto different stacks as the items are first read and moved.

I do have a overall idea about the distribution of all the items. For instance, if the items were sorted alphabetically, I might know that items values starting with “S” are five times more common then items that start with “V” and that “S” is also twice as common as items starting with “A”. Is knowing this even useful here?

I’m not sure if this would be helpful for pre-sorting but in some instances I might also know all the possible values of the items. So I could calculate if the value of an item is “odd” or “even”. Even other similar calculations can be done (like the remainder of a larger modulos operation). Maybe separating the items into “odd” or “even” stacks would make it easier to weave into the final sorted stacks. I’m just speculating here, since I haven’t run any tests yet.

Desired Result:

Items don’t need to be sorted back into a single stack, it would probably actually be more ideal if they were moved into several smaller stacks of somewhat equal size. For example, I might end with alphabetically sorted stacks as such: A-D, D-K, K-R, R-S, S-Z.

What kind of algorithms would be best given these limitations? Pseudo code?

5

Do a “binning” of the initial black box stack into N groups of equal proportions (your A-D, D-K…). At that point you can use any O(n log n) algorithm to sort each group in turn.

Actually, you could pop items from the black-box stack directly into a group of N red-black trees.

But if you have lots of identical elements… could you not just store a counter into a single tree? If you only have 300 values, you might then store your several thousands values into a single tree of comparatively small height. When extracting values you’d just decrement the counter and delete the element when the counter eventually reached 0. And you could use the tree to fill the output stacks if you found that you still needed them.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật