Based on your algorithm variables there is not much you can do other than:
shortening the variables names (while still maintaining readability):
Perhaps use ImageNum and GroupNum instead of NumberOfImages and NumberOfGroups.
reducing the horizontal indentation for nested structures:
Use \SetInd to reduce the space around the vertical lines.
The line-breaking is as expected, since there is no hyphenation pattern specified for the "words" used in your algorithm. Moreover, setting everything inside math mode allows break-points only to occur at specific locations, like binary operators (+, say) or relations (=, say).
The below example attempts to highlight both the above measures, as well as suggest a more appropriate formatting for the components of your algorithm. To that end, it defines
\var{<variable>} to set a <variable>;
\proc{<procedure>}{<arguments>} to set a <procedure> with possible <arguments>; and
\prop{<property>} to set a <property> associated with a variable:
Defining macros to take care of the formatting allows you to easily change this in the future.

\documentclass{article}
\usepackage[ruled,vlined]{algorithm2e}
\newcommand{\var}{\texttt}
\newcommand{\proc}[2]{\textsl{#1}(#2)}
\newcommand{\prop}{\textit}
\begin{document}
\begin{algorithm}[H]
\caption{Building MedGraph}
\footnotesize
\DontPrintSemicolon
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\Input{\var{Path}, \var{CV\_LOAD\_IMAGE\_COLOR} \tcc*[f] {Path to the directory and subdirectories that contain images}}
\Output{Array of roots \tcc*[f] {The indexing structure}}
\Begin{
\nl \proc{InvestigateDirSubDir}{\var{Path}}\;
\tcc*[f] { Mat object is used as a handler for a file} \\[-\baselineskip]
\nl\ForEach{\var{file}}{
\nl\If{\var{Matobject.data}}{
\nl Increment \var{ImageNum}\;
\nl \proc{cvtColor}{\var{-},\var{-},\var{CV\_BGR2GRAY)}}\;
\nl \proc{getHeightWidth}{}\;
\nl $\var{HeadDLL} = \proc{buildAgrid}{\var{height}, \var{width}}$\;
\nl $\var{Previous} = \var{arrayOfRoots}[\var{ImageNum\%GroupNum}].\prop{east}$\;
\nl $\var{arrayOfRoots}[\var{ImageNum\%GroupNum}].\prop{east} = \var{HeadDLL}$\;
\nl $\var{TailDLL} = \proc{getLastgrid}{\var{HeadDLL}}$\;
\nl $\var{TailDLL}.\prop{south} = \var{Previous}$\;
}
}
}
\end{algorithm}
%\SetInd{<before rule space>}{<after rule space>}
%\SetInd{0.5em}{1em}% Default
\SetInd{0.5em}{0.5em}
\begin{algorithm}[H]
\caption{Building MedGraph}
\footnotesize
\DontPrintSemicolon
\SetKwInOut{Input}{input}\SetKwInOut{Output}{output}
\Input{\var{Path}, \var{CV\_LOAD\_IMAGE\_COLOR} \tcc*[f] {Path to the directory and subdirectories that contain images}}
\Output{Array of roots \tcc*[f] {The indexing structure}}
\Begin{
\nl \proc{InvestigateDirSubDir}{\var{Path}}\;
\tcc*[f] { Mat object is used as a handler for a file} \\[-\baselineskip]
\nl\ForEach{\var{file}}{
\nl\If{\var{Matobject.data}}{
\nl Increment \var{ImageNum}\;
\nl \proc{cvtColor}{\var{-},\var{-},\var{CV\_BGR2GRAY)}}\;
\nl \proc{getHeightWidth}{}\;
\nl $\var{HeadDLL} = \proc{buildAgrid}{\var{height}, \var{width}}$\;
\nl $\var{Previous} = \var{arrayOfRoots}[\var{ImageNum\%GroupNum}].\prop{east}$\;
\nl $\var{arrayOfRoots}[\var{ImageNum\%GroupNum}].\prop{east} = \var{HeadDLL}$\;
\nl $\var{TailDLL} = \proc{getLastgrid}{\var{HeadDLL}}$\;
\nl $\var{TailDLL}.\prop{south} = \var{Previous}$\;
}
}
}
\end{algorithm}
\end{document}
\SetInd{<before rule space>}{<after rule space>} was used to adjust the spacing around the vertical rules in the second algorithm.
Another option would be to reduce the font size even more, but that's a bit ridiculous. Finally, you can set the entire algorithm in a box and shrink it to fit the \linewidth. However, that's equivalently non-beautiful.
$ ... $for italicised text,$ ... $is for mathematics and so letters are treated as variables and spaced as such. SoOfwithin$ ... $is not treated as the word "Of" but some variable O multiplied by some variable f, which is why you have that horrible spacing between O and f in arrayOfRoots (to give one example). This spacing is horrible within an italicised word, but quite right if these are mathematical variables being multiplied – Au101 Mar 06 '16 at 17:38