Skip to content

Disk Scheduling Algorithm Implementation in Java

Disk Scheduling Algorithm Implementation in Java  
/**
 * 
 * @param current  : current position
 * @param sequence : sequence of positions
 * @return
 */
private int[] arrangeBySSTF(int current, int sequence[])
{
    int n = sequence.length;
    int sstf[] = new int[n];
    for(int i = 0;i < n;i ++)
    {
        sstf[i] = sequence[i];
    }
    
    int ii = -1;
    for(int i = 0;i < n;i ++)
    {
        int minimum = Integer.MAX_VALUE;
        ii = i;
        for(int j = i;j < n;j ++)
        {
            int distance = Math.abs(current - sstf[j]);
            if( distance < minimum )
            {
                ii = j;
                minimum = distance;
            }
        }
        int tmp = sstf[i];
        sstf[i] = sstf[ii];
        sstf[ii] = tmp;
        current = sstf[i];
    }
    return sstf;
}

/**
 * 
 * @param previous position
 * @param current position
 * @param sequence of positions
 * @return
 */
private int[] arrangeBySCAN(int previous, int current, int sequence[])
{
    boolean isLeft = false;
    int scanSequence[] = new int[sequence.length+1]; //+1 for boundary
    int scanSeqIndex = 0;
    
    int sortedArray[] = sequence.clone();
    
    Arrays.sort(sortedArray);
    
    int result = Arrays.binarySearch(sortedArray, current);
    int index = result;
    if( result < 0 )
        index = -(result + 1);
    
    System.out.println("Index : " + index);
    //index -= 1;
    
    if( previous > current )
        isLeft = true; 
    
    if( !isLeft )
    {
        for(int i=index;i < sequence.length;i ++)
            scanSequence[scanSeqIndex++] = sortedArray[i];
        
        scanSequence[scanSeqIndex++] = dp.getCylinders(); 
        
        for(int i=index-1;i >= 0;i --)
            scanSequence[scanSeqIndex++] = sortedArray[i];
    }
    else
    {
        //untested?
        for(int i=index-1;i >= 0;i --)
            scanSequence[scanSeqIndex++] = sortedArray[i];
        
        scanSequence[scanSeqIndex++] = 0; //0 is the assumed left boundary
        
        for(int i=index;i < sequence.length;i ++)
            scanSequence[scanSeqIndex++] = sortedArray[i];
        
    }
    return scanSequence;
}

/**
 * Return the sequence based on LOOK algorithm
 * @param previous 	: previous position 
 * @param current 	: current position
 * @param sequence	: sequence of positions
 * @return
 */
private int[] arrangeByLOOK(int previous, int current, int sequence[])
{
    boolean isLeft = false;
    int scanSequence[] = new int[sequence.length]; //+1 for boundary
    int scanSeqIndex = 0;
    
    int sortedArray[] = sequence.clone();
    
    Arrays.sort(sortedArray);
    
    //find the index if the element would to be inserted
    //if not found, returns  (-(insertion point) - 1)
    int result = Arrays.binarySearch(sortedArray, current); 
    int index = result;
    if( result < 0 )
        index = -(result + 1);
    
    System.out.println("Index : " + index);
    
    if( previous > current )
        isLeft = true; 
    
    if( !isLeft )
    {
        for(int i=index;i < sequence.length;i ++)
            scanSequence[scanSeqIndex++] = sortedArray[i];
        
        for(int i=index-1;i >= 0;i --)
            scanSequence[scanSeqIndex++] = sortedArray[i];
    }
    else
    {
        //untested?
        for(int i=index-1;i >= 0;i --)
            scanSequence[scanSeqIndex++] = sortedArray[i];
                
        for(int i=index;i < sequence.length;i ++)
            scanSequence[scanSeqIndex++] = sortedArray[i];
        
    }
    return scanSequence;
}
Will post the full solution soon
Enjoyed the content ? Share it with your friends !
Published inProgramming

Be First to Comment

Leave a Reply

Your email address will not be published.