Elevator Scheduling algorithm---Scan algorithm __ data structure and algorithm

Source: Internet
Author: User

Please cherish small compilation of labor results, the article for the small series of original, reproduced please indicate the source.


Scan (SCAN) scheduling algorithm: Always start from the current position of the magnetic arm, along the direction of the movement of the arm to select the closest to the current boom of the cylinder visitors. If there is no request to access along the direction of the magnetic arm, the direction of movement of the magnetic arm is changed. Using this scheduling algorithm, we need to set two queues for visitors, according to the direction of the head, visitors can access from near queue, departure from the direction of the head of the visitors also from near row for another team. First, press the head move Direction queue scheduling visitors to access the disk, when there is no visitors in that direction, then change direction, select another visitor queue access disk.


Java Code Implementation:

Import Java.util.Scanner;
	public class SCAN {int visitadd[];
	int visitsub[];   int index=0;
		The Subscript public void scan (int data[],int begin,int direction) {int temp=0, which is found in a sorted sequence with a minimum value larger than its size, is stored; Small to large sort for (int i=0;i<data.length;i++) {for (int j=i;j<data.length;j++) {if (Data[i]>data[j]) {temp=
					Data[i];
					DATA[I]=DATA[J];
				Data[j]=temp;
				for (int i=0;i<data.length;i++) {if (Data[i]>begin) {//////To find subscript index=i for the smallest value larger than it in the sorted sequence;
			Break   }}///visitsub=new Int[index] smaller than the starting position;
		Smaller than him there are index for (int i=0;i<index;i++) {visitsub[i]=data[i];   }//Visitadd=new Int[data.length-index] Larger than the starting position;
		Larger than him altogether data.length-index for (int i=0;i<visitadd.length;i++) {Visitadd[i]=data[i+index];
		///Calculate average time int lengthadd=0;
		int lengthsub=0;
		int length=0;
		for (int i=0;i<visitadd.length-1;i++) {lengthadd+=math.abs (visitadd[i]-visitadd[i+1]); for (int i=visitsub.length-1;i>0;i--) {Lengthsub+=math.abs(Visitsub[i]-visitsub[i-1]);
		
		} length=lengthadd+lengthsub+math.abs (Begin-visitadd[0]) +math.abs (begin-visitsub[visitsub.length-1]);    if (direction==0) {//Find small for (int i=visitsub.length-1;i>=0;i--) {System.out.print (visitsub[i]+)
			");
			}//slewing for (int i=0;i<visitadd.length;i++) {System.out.print (visitadd[i]+ "");
		} length+=math.abs (Visitsub[0]-begin);
			}else{//Find large for (int i=0;i<visitadd.length;i++) {System.out.print (visitadd[i]+ "");
			}//slewing for (int i=visitsub.length-1;i>=0;i--) {System.out.print (visitsub[i]+ "");
		} length+=math.abs (Visitadd[visitadd.length-1]-begin);
	} System.out.println ("Average seek length is" +length/data.length);
		public static void Main (string[] args) {SCAN scan=new SCAN ();
		Scanner scanner=new Scanner (system.in);
        System.out.println ("Please enter the number of data");
        int Num=scanner.nextint ();
   SYSTEM.OUT.PRINTLN ("Please input data once, space separated");     int data[]=new Int[num];
        for (int i=0;i<num;i++) {data[i]=scanner.nextint ();
        SYSTEM.OUT.PRINTLN ("Please input read and write Head Start position");
        int Begin=scanner.nextint ();
        SYSTEM.OUT.PRINTLN ("Please input head move Direction 0: Track reduce direction 1: Track increase direction");
        int Direction=scanner.nextint ();
        
	Scan.scan (data, begin, direction);
 }//98 183 37 122 14 124 65 67}

Test:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.