Running starter1_closestp.js

➕︎ Aa ➖︎ |Dark 1Dark 2Light|HackFira Code|Back to  PT Mono
// CPCS 223 Analysis & Design of Algorithms
// Brute force closest pair - coordinate arrays
// 2020, Dr. Muhammad Al-Hashimi


// -----------------------------------------------------------------------
// straight implementation of textbook algorithm,
// a generic code that is almost language independent
// typical of 1970-80s

// points specified via common index in x and y-coordinate arrays
// P_i = (x[i],y[i]), i between 1,n (ignore index 0)

var x = [,1,3,2,4,5,6];   // use debugger to check x
var y = [,-1,1,0,2,3,4];  // P3 = (x[3],y[3]) = (2,0) 

document.write( "<p>", closestPair(), "</p>" );

// --------------------
function closestPair()
{

   // x[], y[] are global for this function
   // you can just use them
   
   var n = x.length-1;       // since we ignore x[0], y[0]
                             // you might want to check that x[], y[] are the same length
   
   var cp = [-1,-1];         // array to track pair with minimum distance so far
                             // for example: cp[0] = i, cp[1] = j
                             // initialized with invalid indices for testing

   // your code next: just translate the algorithm directly


      
   return cp;              // return array of pair indices
}