daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Jan 16th, 2018 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="viewport" content="width=device-width">
  6.   <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. function getPivot(numbers,low,high){
  12.   if(low===high){
  13.     return numbers[low];
  14.   }
  15.   var step = 5, n=-1,i=low,l=high-step,medianIndex;
  16.   while(i<=l){
  17.     medianIndex = getMedian(numbers,i,i+step-1);
  18.     exchange(numbers,++n,medianIndex);
  19.     i+=5;
  20.   }
  21.   if(i<=high){
  22.     medianIndex = getMedian(numbers,i,high);
  23.     exchange(numbers,++n,medianIndex);
  24.   }
  25.   return getPivot(numbers,low,n);
  26. }
  27.  
  28. function getMedian(numbers,low,high){
  29.   var medianIndex = Math.trunc(0.5*(high-low))+low;
  30.   for(var i =low;i<=medianIndex;i++){
  31.     var flag =i;
  32.     for(var j = i+1;j<=high;j++){
  33.       if(numbers[j]<numbers[flag]){
  34.         flag = j;
  35.       }
  36.     }
  37.     exchange(numbers,i,flag);
  38.   }
  39.   return medianIndex;
  40. }
  41. function exchange(numbers,index1,index2){
  42.   var temp = numbers[index1];
  43.   numbers[index1]=numbers[index2];
  44.   numbers[index2]=temp;
  45. }
  46.  
  47. var numbers=[1,12];
  48. var pivot = getPivot(numbers,0,numbers.length-1);
  49. console.log(pivot);
  50. </script>
  51.  
  52.  
  53.  
  54. <script id="jsbin-source-javascript" type="text/javascript">function getPivot(numbers,low,high){
  55.   if(low===high){
  56.     return numbers[low];
  57.   }
  58.   var step = 5, n=-1,i=low,l=high-step,medianIndex;
  59.   while(i<=l){
  60.     medianIndex = getMedian(numbers,i,i+step-1);
  61.     exchange(numbers,++n,medianIndex);
  62.     i+=5;
  63.   }
  64.   if(i<=high){
  65.     medianIndex = getMedian(numbers,i,high);
  66.     exchange(numbers,++n,medianIndex);
  67.   }
  68.   return getPivot(numbers,low,n);
  69. }
  70.  
  71. function getMedian(numbers,low,high){
  72.   var medianIndex = Math.trunc(0.5*(high-low))+low;
  73.   for(var i =low;i<=medianIndex;i++){
  74.     var flag =i;
  75.     for(var j = i+1;j<=high;j++){
  76.       if(numbers[j]<numbers[flag]){
  77.         flag = j;
  78.       }
  79.     }
  80.     exchange(numbers,i,flag);
  81.   }
  82.   return medianIndex;
  83. }
  84. function exchange(numbers,index1,index2){
  85.   var temp = numbers[index1];
  86.   numbers[index1]=numbers[index2];
  87.   numbers[index2]=temp;
  88. }
  89.  
  90. var numbers=[1,12];
  91. var pivot = getPivot(numbers,0,numbers.length-1);
  92. console.log(pivot);</script></body>
  93. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top