Site icon C1CTech

Bubble Sort(Java Implementation)

<p><span style&equals;"color&colon; &num;008000&semi;"><strong>Bubble sort<&sol;strong><&sol;span> algorithm is comparison-based algorithm in which each pair of adjacent elements is compared and the elements are swapped if they are not in order&period; This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο&lpar;n<sup>2<&sol;sup>&rpar; where <b>n<&sol;b> is the number of items&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre style&equals;"padding-left&colon; 60px&semi;"><strong><code>public class Sort &lbrace;&NewLine;&NewLine;public static void bubbleSort&lpar;int arr&lbrack;&rsqb;&rpar; &lbrace;&NewLine;int temp&semi;&NewLine;for&lpar;int i&equals;0&semi;i&lt&semi;arr&period;length-1&semi;i&plus;&plus;&rpar; &NewLine; &lbrace;&NewLine; for &lpar;int j &equals; 0&semi; j &lt&semi; &lpar;arr&period;length - 1&rpar; - i&semi; j&plus;&plus;&rpar; &NewLine; &lbrace; &NewLine; if &lpar;arr&lbrack;j&rsqb; &gt&semi; arr&lbrack;j &plus; 1&rsqb;&rpar; &lbrace;&NewLine; temp &equals; arr&lbrack;j&rsqb;&semi;&NewLine; arr&lbrack;j&rsqb; &equals; arr&lbrack;j &plus; 1&rsqb;&semi;&NewLine; arr&lbrack;j &plus; 1&rsqb; &equals; temp&semi; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine;public static void main&lpar;String&lbrack;&rsqb; strr&rpar; &lbrace;&NewLine;int arr&lbrack;&rsqb; &equals; &lbrace;23&comma;67&comma;90&comma;12&comma;3&comma;87&comma;34&comma;23&comma;78&comma;56&rcub;&semi;&NewLine;System&period;out&period;println&lpar;" Before sorting"&rpar;&semi;&NewLine;for&lpar;int j&equals;0&semi;j&lt&semi;&equals;arr&period;length-1&semi;j&plus;&plus;&rpar;&NewLine;&lbrace;&NewLine;System&period;out&period;print&lpar;" "&plus;arr&lbrack;j&rsqb;&rpar;&semi;&NewLine;&rcub;&NewLine;bubbleSort&lpar;arr&rpar;&semi;&NewLine;System&period;out&period;println&lpar;"&bsol;nAfter Sorting"&rpar;&semi;&NewLine;for&lpar;int j&equals;0&semi;j&lt&semi;&equals;arr&period;length-1&semi;j&plus;&plus;&rpar;&NewLine;&lbrace;&NewLine;System&period;out&period;print&lpar;" "&plus;arr&lbrack;j&rsqb;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&NewLine;&rcub;<&sol;code><&sol;strong><&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;">output&colon;<&sol;span><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Before sorting<&sol;span><&sol;strong><br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">23   67   90   12   3   87   34   23   78   56<&sol;span><&sol;strong><br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">After Sorting<&sol;span><&sol;strong><br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">3    12   23   23   34   56   67   78   87  90<&sol;span><&sol;strong><&sol;p>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Time Complexity&colon;<&sol;strong> <&sol;span><&sol;h4>&NewLine;<p>The complexity of bubble sort is <span style&equals;"color&colon; &num;008000&semi;"><strong>Ο&lpar;n<sup>2<&sol;sup>&rpar;<&sol;strong><&sol;span> in both worst and average cases&comma; because the entire array needs to be iterated for every element&period;&NewLine;

Exit mobile version