C# IF statement -


        (int k = 0; k < proc.count; k++){             (int = k + 1; < proc.count; i++){                 if (proc[k].arrivaltime >= proc[i].arrivaltime && proc[k].priority >= proc[i].priority && proc[k].brust > proc[i].brust){                      temp = proc[i];                     proc[i] = proc[k];                     proc[k] = temp;                 }             }         } 

input

process arrival brust priority p0 0 10 5 p1 1 3 1 p2 1 2 1 p3 5 1 5 p4 5 8 2

i want sort these processes following these rules 1) if process arrived alone it'll work no matter what. 2) if 2 processes arrived in same time, gonna check priority if first 1 has higher priority(lower number) it'll work first, , if have same priority gonna let 1 has lower brust work first.

there's problem last 2 processes where's problem?

p3    5    1     5 p4    5    8     2 

process 4 should work because has higher priority

you provided clear explanation of rules.

now check line

if (proc[k].arrivaltime >= proc[i].arrivaltime && proc[k].priority >= proc[i].priority && proc[k].brust > proc[i].brust) 

is doing rules say? no. direct translation of rules this

if (proc[k].arrivaltime > proc[i].arrivaltime ||    (proc[k].arrivaltime == proc[i].arrivaltime &&    (proc[k].priority > proc[i].priority ||    (proc[k].priority == proc[i].priority && proc[k].brust > proc[i].brust)))) 

better , more readable be

int compare = proc[k].arrivaltime.compareto(proc[i].arrivaltime); if (compare == 0)     compare = proc[k].priority.compareto(proc[i].priority); if (compare == 0)     compare = proc[k].brust.compareto(proc[i].brust); if (compare > 0) {     // ... } 

which standard way of doing multi key comparison.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -