{"id":16,"date":"2018-11-15T18:01:00","date_gmt":"2018-11-15T18:01:00","guid":{"rendered":""},"modified":"2019-02-12T23:35:47","modified_gmt":"2019-02-12T22:35:47","slug":"how-bubble-sort-algorithm-works-implementation-in-java","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-bubble-sort-algorithm-works-implementation-in-java\/","title":{"rendered":"How Bubble Sort Algorithm Works (Implementation in Java)"},"content":{"rendered":"<p>This would be a very simple explanation with the program in Java on how the Bubble Sort Algorithms works.<\/p>\n<p>Bubble Sort works by iterating through the elements of the array and doing pairwise swap of adjacent elements that are out of order.<\/p>\n<div style=\"clear: both; text-align: center;\"><\/div>\n<p>We would examine the program in Java and then explain it line by line. The we would actually run the program using Netbeans IDE to see how it works.<\/p>\n<p>The java program is explained as follows: There would be three functions in addition to the main methods<\/p>\n<p><b>BubbleSort():<\/b> This is the function that performs the sorting on an array. This function takes an array as parameter.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\">    <span style=\"color: #888888;\">\/\/THE BUBBLESORT FUNCTION IN JAVA<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">BubbleSort<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span><span style=\"color: #333333;\">[]<\/span> a<span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">{<\/span>\r\n        <span style=\"color: #333399; font-weight: bold;\">int<\/span> i<span style=\"color: #333333;\">,<\/span> j<span style=\"color: #333333;\">;<\/span>\r\n        <span style=\"color: #333399; font-weight: bold;\">int<\/span> N <span style=\"color: #333333;\">=<\/span> a<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span><span style=\"color: #333333;\">;<\/span><\/pre>\n<p><span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span>j<span style=\"color: #333333;\">=<\/span>N<span style=\"color: #333333;\">&#8211;<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">;<\/span> j<span style=\"color: #333333;\">&gt;<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> j<span style=\"color: #333333;\">&#8211;)<\/span> <span style=\"color: #333333;\">{<\/span><br \/>\n<span style=\"color: #008800; font-weight: bold;\">for<\/span><span style=\"color: #333333;\">(<\/span>i<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">&lt;<\/span>j<span style=\"color: #333333;\">;<\/span> i<span style=\"color: #333333;\">++){<\/span><br \/>\n<span style=\"color: #008800; font-weight: bold;\">if<\/span><span style=\"color: #333333;\">(<\/span>a<span style=\"color: #333333;\">[<\/span>i<span style=\"color: #333333;\">]&gt;<\/span> a<span style=\"color: #333333;\">[<\/span>i<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">])<\/span><br \/>\nswap<span style=\"color: #333333;\">(<\/span>a<span style=\"color: #333333;\">,<\/span> i<span style=\"color: #333333;\">,<\/span> i<span style=\"color: #333333;\">+<\/span><span style=\"color: #0000dd; font-weight: bold;\">1<\/span><span style=\"color: #333333;\">);<\/span><br \/>\n<span style=\"color: #333333;\">}<\/span><br \/>\n<span style=\"color: #333333;\">}<\/span><br \/>\n<span style=\"color: #333333;\">}<\/span><\/p>\n<p>Listing 1: BubbleSort function<\/p>\n<p><ins style=\"display: block; text-align: center;\" data-ad-client=\"ca-pub-7041870931346451\" data-ad-format=\"fluid\" data-ad-layout=\"in-article\" data-ad-slot=\"8227894917\"><\/ins><br \/>\n<b>Swap():<\/b> This function performs a pairwise swap of two array elements. It take three parameters: the first is the array, the second and third are the indices of the elements to be swapped.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\">    <span style=\"color: #888888;\">\/\/FUNCTION TO PERFORM SWAP OF TWO ITEMS<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">swap<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span><span style=\"color: #333333;\">[]<\/span> ar<span style=\"color: #333333;\">,<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> x<span style=\"color: #333333;\">,<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> y<span style=\"color: #333333;\">)<\/span>\r\n    <span style=\"color: #333333;\">{<\/span>\r\n        <span style=\"color: #333399; font-weight: bold;\">int<\/span> k <span style=\"color: #333333;\">=<\/span> ar<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">];<\/span>\r\n        ar<span style=\"color: #333333;\">[<\/span>x<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">=<\/span> ar<span style=\"color: #333333;\">[<\/span>y<span style=\"color: #333333;\">];<\/span>\r\n        ar<span style=\"color: #333333;\">[<\/span>y<span style=\"color: #333333;\">]<\/span> <span style=\"color: #333333;\">=<\/span> k<span style=\"color: #333333;\">;<\/span>\r\n    <span style=\"color: #333333;\">}<\/span><\/pre>\n<p>Listing 2: Swap Function<\/p>\n<p>&nbsp;<\/p>\n<p><b>Display(): <\/b>This function displays the elements of the array to the output by looping through the array elements and calling System.out.println().<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"line-height: 125%; margin: 0;\">    <span style=\"color: #888888;\">\/\/FUNCTION TO DISPLAY ARRAY ITEMS TO THE OUTPUT<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">Display<\/span><span style=\"color: #333333;\">(<\/span><span style=\"color: #333399; font-weight: bold;\">int<\/span> a<span style=\"color: #333333;\">[])<\/span>\r\n    <span style=\"color: #333333;\">{<\/span>\r\n        <span style=\"color: #333399; font-weight: bold;\">int<\/span> i<span style=\"color: #333333;\">=<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">;<\/span>\r\n        <span style=\"color: #008800; font-weight: bold;\">while<\/span><span style=\"color: #333333;\">(<\/span>i <span style=\"color: #333333;\">!=<\/span> a<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">length<\/span><span style=\"color: #333333;\">)<\/span>\r\n        <span style=\"color: #333333;\">{<\/span>\r\n            System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span>a<span style=\"color: #333333;\">[<\/span>i<span style=\"color: #333333;\">]);<\/span>\r\n            i<span style=\"color: #333333;\">++;<\/span>\r\n        <span style=\"color: #333333;\">}<\/span>       \r\n    <span style=\"color: #333333;\">}<\/span><\/pre>\n<p>Listing 3: Swap Function<\/p>\n<p>&nbsp;<\/p>\n<p><b>Main():<\/b> Runs the program by calling the each of the functions.<\/p>\n<pre style=\"line-height: 125%; margin: 0;\">    <span style=\"color: #888888;\">\/\/MAIN PROGRAM<\/span>\r\n    <span style=\"color: #008800; font-weight: bold;\">public<\/span> <span style=\"color: #008800; font-weight: bold;\">static<\/span> <span style=\"color: #333399; font-weight: bold;\">void<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span><span style=\"color: #333333;\">(<\/span>String<span style=\"color: #333333;\">[]<\/span> args<span style=\"color: #333333;\">)<\/span> <span style=\"color: #333333;\">{<\/span>\r\n      <span style=\"color: #333399; font-weight: bold;\">int<\/span> a<span style=\"color: #333333;\">[]<\/span> <span style=\"color: #333333;\">=<\/span> <span style=\"color: #333333;\">{<\/span><span style=\"color: #0000dd; font-weight: bold;\">5<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">3<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">8<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">0<\/span><span style=\"color: #333333;\">,<\/span><span style=\"color: #0000dd; font-weight: bold;\">2<\/span><span style=\"color: #333333;\">};<\/span>    <span style=\"color: #888888;\">\/\/INITIALIZE AN ARRAY WE ARE GOING TO SORT<\/span>\r\n      Display<span style=\"color: #333333;\">(<\/span>a<span style=\"color: #333333;\">);<\/span>                 <span style=\"color: #888888;\">\/\/DISPLAY THE UNSORTED ARRAY<\/span>\r\n      BubbleSort<span style=\"color: #333333;\">(<\/span>a<span style=\"color: #333333;\">);<\/span>              <span style=\"color: #888888;\">\/\/SORT THE ARRAY USING BUBBLESORT     <\/span>\r\n      System<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">out<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">println<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"The Sorted list is given below:\"<\/span><span style=\"color: #333333;\">);<\/span>\r\n      Display<span style=\"color: #333333;\">(<\/span>a<span style=\"color: #333333;\">);<\/span>                 <span style=\"color: #888888;\">\/\/DISPLAY THE SORTED ARRAY<\/span>\r\n    <span style=\"color: #333333;\">}<\/span><\/pre>\n<p>Listing 4: Main Program<\/p>\n<p>&nbsp;<\/p>\n<p>This short video explains how to put everything together and run it in Netbeans<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/poI2PmPnSAw\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This would be a very simple explanation with the program in Java on how the Bubble Sort Algorithms works. Bubble Sort works by iterating through &hellip; <\/p>\n","protected":false},"author":2,"featured_media":306,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[35],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":5,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions\/405"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media\/306"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}