sorting - Not getting Proper Sorted Array In Windows Javascript App -


i not able proper sorted output in case of bubble sort algorithm when implemented in windows store app (javascript) . below source code javascript :-

function bubblesort() {     var numlist = numlist.value;     var swap,flag=1,i=0;     var ar = numlist.split(",");     val.value = ar[i];     while(flag!=0)     {         (var j = 0; j < ar.length; j++)         {            flag = 0;             if(ar[j]>ar[j+1])             {                 swap = ar[j + 1];                 ar[j + 1] = ar[j];                 ar[j] = swap;                 flag++;             }         }     }     is.value = ar.tostring(); } 

input :- "10,4,3,2,1,5,7,6,9" output :- "10,3,2,1,4,5,6,7,9"

i using visual studio 2013 update 3 making app. please tell me wrong?

try (adjust needs):

function bubblesort(v, desc) {     var ar = v.split(','), = v.length;     while((i-=1,i))     {         (var j = 0; j < ar.length; j++)         {             if(desc ? +ar[j]<+ar[j+1] : +ar[j]>+ar[j+1]) //<= numeric comparison             {                 var swap = ar[j];                 ar[j] = ar[j+1];                 ar[j+1] = swap;             }         }     }     return ar.join(); } // usage asc en desc: bubblesort('10,4,3,2,1,5,7,6,9');       //=> [1,2,3,4,5,6,7,9,10] bubblesort('10,4,3,2,1,5,7,6,9', true); //=> [10,9,7,6,5,4,3,2,1] 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -