In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to realize the java bitmap sorting algorithm". In the daily operation, I believe many people have doubts about how to realize the java bitmap sorting algorithm. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the java bitmap sorting algorithm". Next, please follow the editor to study!
Algorithm requirement
Input: a file containing up to n positive integers, where each number is less than n (n = 10 ^ 7). There are no duplicate files.
Output: output in ascending order
Constraints: only about 1m of memory space, enough disk space. The maximum run is a few minutes, and if it is 10 seconds, there is no need for optimization.
When I first graduated from school, I was asked by three interviewers about this question. At that time, my first answer was a simple bubble sort. The interviewer directly reminded me that memory was limited, and then I had an idea. I could use a third-party database to store it in the database first, and then query it in order by order. Obviously, this is not a solution. Of course, I didn't answer the question at that time. But now, in retrospect, there is still a solution.
Analysis of problems
If we say that it is obviously unrealistic to introduce this data into memory for sorting, because some of the sizes of n may be more than 1m, we can consider using bitmap sorting.
Implementation summary
Suppose we have a set of data less than 10, such as {3, 1, 1, 8, 5, 4, 9}. We can represent this collection with the following string
0 1 0 1 1 1 0 0 11 where the data in the representative set is represented as 1 and the other is zero
If we use pseudo code, we can do this as follows:
1 initialize an array of size n
For n = [1Jing n]
Bit [I] = 0
2 read every number in each file sequentially
For each i
Bit [I] = 1
3 output
For n = [0jinn)
If (bit [I] = = 1)
Print i
Java code implementation
Int [] arrs = {122.34 arrs = 13Jing 1Jing 143J 321J 1411}; / / Files on disk
Int [] sorts = new int [2000]
For (int arr: arrs) {
Sorts [arr] = 1
}
For (int iTuno Bandi)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.