I have a confusion that how the weights (a and b) for equation (y= a+bx) are calculated in linear regression Machine learning Algorithm 

by solving the linear equation a = mean (y)  b * mean(x) and b = correlation *(std dev of y /std dev of x)
or 
The weights are first arbitrarily taken and then cost function J(theta) is used to minimize the weights depending on the adjustment of the best fit line on the dataset.