1 / 14

演算法 final project 299 Train Swapping

演算法 final project 299 Train Swapping. Team 6 99703006 資科二 林裕翔 99703016 資科二 黃永大 99703025 資科二 許宇樵. About the subject : Train Swapping.

altessa
Download Presentation

演算法 final project 299 Train Swapping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 演算法final project299Train Swapping Team 6 99703006 資科二 林裕翔 99703016 資科二 黃永大 99703025 資科二 許宇樵

  2. About the subject : Train Swapping • At an old railway station, you may still encounter one of the last remaining ``train swappers''. A train swapper is an employee of the railroad, whose sole job it is to rearrange the carriages of trains.Once the carriages are arranged in the optimal order, all the train driver has to do, is drop the carriages off, one by one, at the stations for which the load is meant.The title ``train swapper'' stems from the first person who performed this task, at a station close to a railway bridge. Instead of opening up vertically, the bridge rotated around a pillar in the center of the river. After rotating the bridge 90 degrees, boats could pass left or right.The first train swapper had discovered that the bridge could be operated with at most two carriages on it. By rotating the bridge 180 degrees, the carriages switched place, allowing him to rearrange the carriages (as a side effect, the carriages then faced the opposite direction, but train carriages can move either way, so who cares).Now that almost all train swappers have died out, the railway company would like to automate their operation. Part of the program to be developed, is a routine which decides for a given train the least number of swaps of two adjacent carriages necessary to order the train. Your assignment is to create that routine.

  3. What it really want • Input SpecificationThe input contains on the first line the number of test cases (N). Each test case consists of two input lines. The first line of a test case contains an integer L, determining the length of the train (   ). The second line of a test case contains a permutation of the numbers 1 through L, indicating the current order of the carriages. The carriages should be ordered such that carriage 1 comes first, then 2, etc. with carriage L coming last. • Output SpecificationFor each test case output the sentence: 'Optimal train swapping takes S swaps.' where S is an integer.

  4. Example Input3 (three times)3 (1st,3 carriages)1 3 2 4 (2nd,4 carriages)4 3 2 12 (3rd,2 carrisges)2 1 • Example OutputOptimal train swapping takes 1 swaps. (132  123)Optimal train swapping takes 6 swaps. (4321  1234)Optimal train swapping takes 1 swaps. (21 12)

  5. According to the form of the input • int main(void){inttimes, carriages, *train;int i, changes=0;scanf("%d",&times);    while(times>0){scanf("%d",&carriages);        train=malloc(sizeof(int)*carriages);        for(i=0;i<carriages;i++)scanf("%d",&train[i]);        changes=SomeKindOfSort(train,carriages);printf("Optimal train swapping takes %d swaps\n",changes);        free(train);        changes=0;        times--;    }    return 0;}

  6. *The permutation part(this is the important part) • intSomeKindOfSort(int *train,int carriages){inti,j,times=0,tmp;    for(i=1;i<carriages;i++){        for(j=0;j<carriages-1;j++){            if(train[j]>train[j+1]){tmp=train[j];                train[j]=train[j+1];                train[j+1]=tmp;                times++;            }        }    }    return times;} • P.s. according to the text ,we use the bubble sort to do this

  7. Bubble sort • comparing each pair of adjacent items and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. • Best case :O(N) • Worst case : O(N^2) • Average case :O(N^2) • Worst case space complexity : O(1) • Although the algorithm is simple, most other algorithms are more efficient for sorting large lists.

  8. Run timeaverage time : close to 0.004 sec

  9. How to improve ??arrange through other sort !!! • Insertion sort : void insertionsort(int*train,int carriages){ inti,j; inttimes=0,temp;      for (i=1;i<carriages;i++)          for (j=i;j>0;j--){              if (train[j]<train[j-1]){ temp=train[j]; train[j]=train[j-1]; train[j-1]=temp; times++;              } } return times; } http://www.youtube.com/watch?v=10G5u4jfZ1I

  10. Selection sort : void selectionsort(int*train,int carriages){inti,j,smallest,tempdata,times;     for (i=0;i         smallest=i;         for (j=i+1;j<=carriages;j++)             if (train[j]                smallest=j;tempdata=train[i];train[i]=train[smallest];train[smallest]=tempdata; times++;} return times;} http://www.youtube.com/watch?v=boOwArDShLU

  11. void quicksort(int *train,intleft,int right){intkey,i,j,times;     if (left< font>     {i=left;      j=right;      key=train[left];      do {         doi++;         while (train[i]< font>         while (train[j]>key)         j--;         if (i< font>            swap(&(train[i]),&(train[j]));         }}while(i< font>      swap(&(train[left]),&(train[j]));      quicksort(list,left,j-1);      quicksort(list,j+1,right);     }} • Quick sort : void swap(int *a,int *b){ int temp;      temp=*a;      *a=*b;      *b=temp; } http://www.youtube.com/watch?v=vxENKlcs2Tw&feature=fvwrel

  12. Comparison different sorting Determine speed as sorting average speed, Quick, Merge, and Heap sort can makes the lowest time complexity which is (time*carriages*Nlogn).

  13. Supplement :Strand sort

  14. Thanks for listen !!

More Related