Need help with optimizing an algorithm
As far as number 2 goes, there's 2 problems :
A track is not defragmented when the first N clusters are busy. It's defragmented when all busy clusters are next to each other (which could be anywhere).
The other thing is that the disk is circular, so if there's 2 clusters, one at the begining of the track, and the other at the end, they are actually next to each other.
right, i did not think about that. how about grandpa gets an ssd and we don't have to deal with this 2010 crap anymore
you observed the right thing, only the ends of the sequence change. another good news is that you don't give two craps about the order in which clusters are in the current subsequence, you only need to know how many free clusters there are, so you can proceed like this:
for each track: count the number N of busy clusters in the track count the number M of free clusters present in the first N clusters starting from sector 0 (the same way as said before) call this value of M "bestM" and keep track of which sector this subsequence starts at now we gotta slide 'pop' the first cluster: if the first cluster is free, decrement M by one. do nothing otherwise 'push' in the next cluster: if the next cluster is free, increment M by one. do nothing otherwise if this value of M is better than bestM (less moves), then update bestM and keep track of this subsequence starting sector keep going until you run the full circle
the modulo method is nice for working on circular arrays, only notice that if the track is N sectors long and it has M busy clusters you don't need to iterate up to 2*N but you can stop at N+M

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now