function [estimate] = knn(train, test, k)

data = load (train);
[rows, cols] = size(data);
test = load(test);

# Step 1: Sort the rows in ascending order by their distance to test instance (Euclidean distance)
for i = 1 : rows
distance = 0;
for j = 1 : cols - 2
distance = distance + power(data(i,j)-test(1,j),2);
end
data(i,cols+1) = distance;
end
[rows,cols] = size(data);
[junk, indices] = sort(data(:,cols));
data = data(indices,:);

# Step 2: Pick K closest, take median of those
if (k < rows)
estimate = median(data(1:k,cols-1));
else
estimate = median(data(:,cols-1));
end