{"id":81585,"date":"2021-07-08T09:00:36","date_gmt":"2021-07-08T03:30:36","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81585"},"modified":"2021-07-08T09:00:36","modified_gmt":"2021-07-08T03:30:36","slug":"bubble-sort-in-data-structure","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/","title":{"rendered":"Bubble Sort in Data Structure"},"content":{"rendered":"<p>Oftentimes, we have a huge amount of data. It is sometimes difficult to deal with such data especially when it is placed randomly. In such cases, sorting that data becomes of utmost importance. Sorting is required so that searching of data becomes easy.<\/p>\n<p>There are multiple types of sorting algorithms in data structures. Sorting algorithms are broadly classified into two categories:<\/p>\n<p>1. Comparison based<br \/>\n2. Non-comparison based<\/p>\n<p>In comparison-based sorting, we compare the elements, whereas, in non-comparison-based sorting, we don\u2019t compare elements of the list.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image01.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-81840 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image01.jpg\" alt=\"Sorting Types\" width=\"756\" height=\"603\" \/><\/a><\/p>\n<h3>What is Bubble Sort?<\/h3>\n<p>Bubble Sort is one of the simplest sorting algorithms. It is also known as <strong>Sorting by exchange<\/strong>. It is a comparison-based algorithm. Its time complexity is of the order O(n<sup>2<\/sup>) where n is the number of elements.<\/p>\n<h3>Working of Bubble Sort<\/h3>\n<p>In bubble sort, we compare adjacent elements and whenever we get a pair that is out of order, we swap them.<\/p>\n<p>For n elements, there are <strong>n-1<\/strong> iterations or passes. In every iteration, the largest element reaches its highest iteration.<\/p>\n<p>The steps followed by the bubble sort algorithm are:<\/p>\n<p><strong>1<\/strong>: Start comparing each element with its adjacent element from the starting index.<br \/>\n<strong>2:<\/strong> If the current and the next element are out of order, swap them.<br \/>\n<strong>3<\/strong>: Repeat step 2 for all the elements of the array\/list.<br \/>\n<strong>4<\/strong>: Repeat steps 1, 2, and 3 until we have reached the final sorted state of the array.<\/p>\n<h3>Bubble Sort Pseudo-code<\/h3>\n<p>The pseudo-code for the bubble sort algorithm is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">procedure Bubble_sort(int array)\n    for(pass=1; pass&lt;=n; pass++)\n        for(index=0; index &lt;= n-1-pass; index++)\n            if(array[index] &gt; array[index+1])\n                swap(array[index], array[index+1])\n            end if\n        end for\n    end for\nend procedure\n<\/pre>\n<p><strong>Bubble Sort Example<\/strong><\/p>\n<p>Let us understand the working of bubble sort with the help of an example.<\/p>\n<p>Suppose we wish to sort the elements of the word \u201cTECHVIDVAN\u201d in alphabetical order.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image02.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81841\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image02.jpg\" alt=\"Bubble Sort Example\" width=\"545\" height=\"103\" \/><\/a><\/p>\n<p>After the first iteration, the letter \u2018V\u2019 will reach the last index.<\/p>\n<p>Iteration 1:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image03.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81842\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image03.jpg\" alt=\"Bubble Sort First Iteration\" width=\"572\" height=\"1094\" \/><\/a><\/p>\n<p>Iteration 2:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image04.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81843\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image04.jpg\" alt=\"Bubble Sorting Second Iteration\" width=\"572\" height=\"930\" \/><\/a><\/p>\n<p>Iteration 3:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image05.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81844\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image05.jpg\" alt=\"Third Iteration in Bubble Sort\" width=\"572\" height=\"766\" \/><\/a><\/p>\n<p>Iteration 4:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image06.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81845\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image06.jpg\" alt=\"Bubble Sort iteration\" width=\"572\" height=\"656\" \/><\/a><\/p>\n<p>Iteration 5:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image07.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81846\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image07.jpg\" alt=\"Bubble Sort Working\" width=\"572\" height=\"602\" \/><\/a><\/p>\n<p>Iteration 6:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image08.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81847\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image08.jpg\" alt=\"Iterations in Bubble Sort\" width=\"572\" height=\"520\" \/><\/a><\/p>\n<p>Iteration 7:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image09.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81848\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image09.jpg\" alt=\"Iterations in Bubble Sort\" width=\"572\" height=\"364\" \/><\/a><\/p>\n<p>Iteration 8:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image10.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81849\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image10.jpg\" alt=\"Bubble Sorting\" width=\"572\" height=\"279\" \/><\/a><\/p>\n<p>Iteration 9:<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image11.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81850\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2021\/06\/TechVidvan-Bubble-sort-normal-image11.jpg\" alt=\"Bubble Sorting Working\" width=\"572\" height=\"107\" \/><\/a><\/p>\n<p>Finally, after the 9th iteration, we have got our array in ascending order.<\/p>\n<h3>Stability of Bubble Sort<\/h3>\n<p>A sorting algorithm is said to be stable if it preserves the order of the duplicate elements. In the above example, we have the letter \u2018V\u2019 occurring twice. In such cases, the stable algorithms keep the order of appearance of \u2018V\u2019 same in both sorted and unsorted arrays.<\/p>\n<p>Bubble sort is also a stable algorithm.<\/p>\n<h3>Implementation of Bubble Sort in C<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nint main() \n{\n   int Arr[] = {12, 34, 42, 1, 4, 81, 9, 25, 6, 21};\n  \n   int length = 10;\n for (int i = 0; i &lt; length-1; i++) \n  {\n      \n    for (int j = 0; j &lt; length-i-1; j++) \n    {\n    \n      if (Arr[j] &gt; Arr[j + 1]) \n      {\n      \n       int temp = Arr[j];\n        Arr[j] = Arr[j + 1];\n        Arr[j + 1] = temp;\n      }\n    }\n  }\n  printf(\" Final sorted Array:\\n\");\n   for (int i = 0; i &lt; len-1; i++) \n  {\n      printf(\"%d \",Arr[i]);\n  }\n  return 0;\n}\n<\/pre>\n<h3>Implementation of Bubble Sort in C++<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;bits\/stdc++.h&gt;\nusing namespace std;\nvoid Bubble_sort(int Arr, int n)\n{\n    for (int i = 0; i &lt; n-1; i++) \n  {\n      \n    for (int j = 0; j &lt; n-i-1; j++) \n    {\n    \n      if (Arr[j] &gt; Arr[j + 1]) \n      {\n      \n       int temp = Arr[j];\n        arr[j] = Arr[j + 1];\n        Arr[j + 1] = temp;\n      }\n    }\n  }\n}\n\nint main() \n{\n    int Arr[] = {12, 34, 42, 1, 4, 81, 9, 25, 6, 21};\n  \n    int len = 10;\n    Bubble_sort(Arr, len)\n    cout &lt;&lt; \"Sorted Array:\\n\";\n    for (int i = 0; i &lt; len - 1; i++) \n      cout &lt;&lt; \" \" &lt;&lt; arr[i];\n    return 0;\n}\n<\/pre>\n<h3>Bubble Sort Implementation in Java<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class BubbleSort \n{\n  static void Bubble_sort(int Arr[]) \n  {\n      int i,j,len;\n      len = arr.length;\n      Boolean swap;\n    for (i = 0; i &lt; len-1; i++)\n{\n      for (j = 0; j &lt; len-i-1; j++)\n    {\n        if (Arr[j] &gt; Arr[j + 1]) \n        {\n          int temp = Arr[j];\n          Arr[j] = Arr[j + 1];\n          Arr[j + 1] = temp;\n        }\n    }\n}\nSystem.out.println(\"Final sorted Array:\");\nfor (i = 0; i &lt; len - 1; i++){\n      System.out.print(Arr[i]+\" \");\n  }\n  public static void main(String args[]) \n  {\n      \n    int[] data = {12, 34, 42, 1, 4, 81, 9, 25, 6, 21};\n    BubbleSort.Bubble_sort(data);\n  }\n}\n<\/pre>\n<h3>Implementation of Bubble Sort in Python<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def Bubble_sort(Array):\n   \n  for i in range(len(Array)):\n    for j in range(0, len(Array)-i-1):\n      if Array[j] &gt; Array[j + 1]:\n        temp = Array[j]\n        Array[j] = Array[j+1]\n        Array[j+1] = temp\nlist = [12, 34, 42, 1, 4, 81, 9, 25, 6, 21]\nBubble_sort(list)\nprint('Final sorted Array:')\nprint(list)\n<\/pre>\n<h3>Optimized Bubble Sort<\/h3>\n<p>In the above example, we can clearly see that the comparisons will be done every time, even if the list is already sorted. This leads to too many extra iterations which are completely unnecessary. These additional iterations increase the execution time of the code.<\/p>\n<p>Thus, we need to modify the bubble sort algorithm. We can optimize this algorithm by using a binary variable \u2018flag\u2019 whose initial value is set to False. This binary variable changes its value to true even if one swap occurs in the whole iteration.<\/p>\n<p>At the end of any iteration, if the value of \u2018flag\u2019 is still false, it means that the array is now sorted and there is no need for further execution. In this way, it helps in reducing the number of comparisons when the array becomes sorted.<\/p>\n<h3>Algorithm for Optimized Bubble Sort<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Step 1: For i = 0 to N-1, Repeat:\n             flag = false\nStep 2: For j = i + 1 to N - I,Repeat:\nStep 3: If Arr[ j ] &gt; Arr[ i ]\n             swap Arr[ j ] and Arr[ i ]\n             flag = true\nStep 4: If flag == false\n             Goto step 5\nStep 5: stop\n<\/pre>\n<h3>Pseudo-code for Optimized Bubble Sort<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">procedure Bubble_sort(int Array)\n   len = Array.length;\n   \n   for i = 0 to loop-1 do:\n      flag = false  \n      for j = 0 to loop-1 do:\n         if Array[j] &gt; Array[j+1] then\n            swap( list[j], list[j+1] )  \n            flag = true   \n         end if         \n      end for \n      If flag == false\n      break; \n   end for\n return Array\nend procedure\n<\/pre>\n<h3>Optimized Bubble Sort Implementation in C<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nvoid main() \n{\n   int Arr[] = {12, 34, 42, 1, 4, 81, 9, 25, 6, 21};\n  \n   int len = 10, flag;\n for (int i = 0; i &lt; len-1; i++) \n  {\n      flag=0;\n      for (int j = 0; j &lt; len-i-1; j++) \n      {\n        if (Arr[j] &gt; Arr[j + 1]) \n        {\n            int temp = Arr[j];\n            Arr[j] = Arr[j + 1];\n            Arr[j + 1] = temp;\n            flag =1;\n      }\n    }\n    if (flag == 0)\n        break;\n  }\n  printf(\"Final sorted Array:\\n\");\n   for (int i = 0; i &lt; len - 1; i++) \n  {\n      printf(\"%d \",Arr[i]);\n  }\n}\n<\/pre>\n<h3>Implementation of Optimized Bubble Sort in C++<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\nint main() \n{\n    int arr[] = {12, 34, 42, 1, 4, 81, 9, 25, 6, 21};\n    int len = 10;\n    int flag;\n    for (int i = 0; i &lt; len-1; i++) \n    {\n        flag=0;\n        for (int j = 0; j &lt; len - i - 1; j++) \n        {\n            if (Arr[j] &gt; Arr[j + 1]) \n            {\n                int temp = Arr[j];\n                Arr[j] = Arr[j + 1];\n                Arr[j + 1] = temp;\n                flag =1;\n            }\n       }\n       if (flag == 0)\n            break;\n    }\n  cout &lt;&lt; \"Final sSorted Array:\\n\";\n   for (int i = 0; i &lt; len - 1; i++) \n  {\n      cout &lt;&lt; \" \" &lt;&lt; Arr[i];\n  }\n}\n<\/pre>\n<h3>Optimized Bubble Sort Implementation in Java:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">public class BubbleSort \n{\n  static void Bubble_sort(int Arr[]) \n  {\n      int i,j,len;\n      len = arr.length;\n      Boolean flag;\n    for (i = 0; i &lt; len-1; i++)\n{\n      flag = false;\n      for (j = 0; j &lt; len-i-1; j++)\n{\n        if (Arr[j] &gt; Arr[j + 1]) \n        {\n          int temp = Arr[j];\n          Arr[j] = Arr[j + 1];\n          Arr[j + 1] = temp;\n          flag = true;\n        }\n      \n    }\nif (flag == false)\nbreak;\n}\nSystem.out.println(\"Final sorted Array:\");\n      for (i = 0; i &lt; len - 1; i++)\n      System.out.print(Arr[i]+\" \");\n  }\n  public static void main(String args[]) \n  {\n      \n    int[] data = {12, 34, 42, 1, 4, 81, 9, 25, 6, 21};\n    BubbleSort.Bubble_sort(data);\n  }\n}\n}\n<\/pre>\n<h3>Optimized Bubble Sort Implementation in Python<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def Bubble_sort(Array):\n   \n  for i in range(len(Array)):\n    flag = false\n    for j in range(0, len(Array) - i - 1):\n      if Array[j] &gt; Array[j + 1]:\n        temp = Array[j]\n        Array[j] = Array[j+1]\n        Array[j+1] = temp\n        flag = true;\n    if flag == false:\n        break;\nlist = [12, 34, 42, 1, 4, 81, 9, 25, 6, 21]\nBubble_sort(list)\nprint('Sorted Array:')\nprint(list)\n<\/pre>\n<h3>Complexity of Bubble Sort<\/h3>\n<h4>Time complexity:<\/h4>\n<p>Time complexity in bubble sort is mainly due to two operations:<\/p>\n<p>1. Comparisons<br \/>\n2. Swaps<\/p>\n<p>In bubble sort, we compare adjacent elements. Thus, the number of comparisons will be:<\/p>\n<table style=\"height: 536px\" width=\"423\">\n<tbody>\n<tr>\n<td><b>Iteration\u00a0<\/b><\/td>\n<td><b>Number of comparisons<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><span style=\"font-weight: 400\">(n-1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><span style=\"font-weight: 400\">(n-2)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><span style=\"font-weight: 400\">(n-3)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">.<\/span><\/td>\n<td><span style=\"font-weight: 400\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">.<\/span><\/td>\n<td><span style=\"font-weight: 400\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">.<\/span><\/td>\n<td><span style=\"font-weight: 400\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">n-2<\/span><\/td>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">n-1<\/span><\/td>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Thus, the total number of comparisons are:<br \/>\n(n-1) + (n-2) + (n-3) + \u2026\u2026\u2026\u2026 + 2 + 1 = n(n-1)\/2<br \/>\n= O(<span style=\"font-weight: 400\">n<\/span><sup><span style=\"font-weight: 400\">2<\/span><\/sup>)<\/p>\n<ul>\n<li>In the best case, the array is already sorted. Thus, there will be zero swaps.<\/li>\n<li>In the average case, the array is jumbled. The number of swaps will be of O(n<sup>2<\/sup>).<\/li>\n<li>In the worst case, the number of swaps will be equal to the number of comparisons i.e. O(n<sup>2<\/sup>).<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><b>Scenario\u00a0<\/b><\/td>\n<td><b>Time complexity<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Best case<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Average case<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<sup>2<\/sup><\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Worst case<\/span><\/td>\n<td><span style=\"font-weight: 400\">O(n<sup>2<\/sup><\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Space complexity of Bubble Sort<\/h4>\n<p>Only one extra variable \u2018temp\u2019 is used. Thus the space complexity will be of O(1).<\/p>\n<h3>Advantages of Bubble Sort<\/h3>\n<ul>\n<li>Easy to implement and understand.<\/li>\n<li>Stable sorting algorithm.<\/li>\n<li>Useful in computer graphics to resolve very small errors.<\/li>\n<li>Its space complexity is very less in comparison to other sorting algorithms.<\/li>\n<\/ul>\n<h3>Disadvantages of Bubble Sort:<\/h3>\n<ul>\n<li>It is the high time complexity of O(<span style=\"font-weight: 400\">n<\/span><sup><span style=\"font-weight: 400\">2<\/span><\/sup>) and therefore higher execution time.<\/li>\n<li>It is not preferable for a large amount of data.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>The bubble sort algorithm is the easiest out of all sorting algorithms in terms of implementation. However, it takes a lot of time for execution when the amount of data is large. Thus, when the data set is small, bubble sort is one of the best algorithms to apply.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Oftentimes, we have a huge amount of data. It is sometimes difficult to deal with such data especially when it is placed randomly. In such cases, sorting that data becomes of utmost importance. Sorting&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":81839,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"quote","meta":{"footnotes":""},"categories":[3555],"tags":[3739,3740,3741,3742,3743,3744,3745,3746,3748],"class_list":["post-81585","post","type-post","status-publish","format-quote","has-post-thumbnail","hentry","category-data-structure","tag-advantages-of-bubble-sort","tag-algorithm-for-optimized-bubble-sort","tag-bubble-sort","tag-bubble-sort-algorithm","tag-bubble-sort-pseudo-code","tag-complexity-of-bubble-sort","tag-implementation-of-bubble-sort","tag-optimized-bubble-sort","tag-space-complexity-of-bubble-sort","post_format-post-format-quote"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bubble Sort in Data Structure - TechVidvan<\/title>\n<meta name=\"description\" content=\"In bubble sort, we compare adjacent elements and whenever we get a pair that is out of order, we swap them. Learn more about this sorting way\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bubble Sort in Data Structure - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"In bubble sort, we compare adjacent elements and whenever we get a pair that is out of order, we swap them. Learn more about this sorting way\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-08T03:30:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/TV-Bubble-sort-in-DS.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bubble Sort in Data Structure - TechVidvan","description":"In bubble sort, we compare adjacent elements and whenever we get a pair that is out of order, we swap them. Learn more about this sorting way","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/","og_locale":"en_US","og_type":"article","og_title":"Bubble Sort in Data Structure - TechVidvan","og_description":"In bubble sort, we compare adjacent elements and whenever we get a pair that is out of order, we swap them. Learn more about this sorting way","og_url":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-08T03:30:36+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/TV-Bubble-sort-in-DS.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Bubble Sort in Data Structure","datePublished":"2021-07-08T03:30:36+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/"},"wordCount":816,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/TV-Bubble-sort-in-DS.jpg","keywords":["Advantages of Bubble Sort","Algorithm for Optimized Bubble Sort","Bubble Sort","Bubble sort algorithm","Bubble Sort Pseudo-code","Complexity of Bubble Sort","Implementation of Bubble Sort","Optimized Bubble Sort","Space complexity of Bubble Sort"],"articleSection":["Data Structure Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/","url":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/","name":"Bubble Sort in Data Structure - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/TV-Bubble-sort-in-DS.jpg","datePublished":"2021-07-08T03:30:36+00:00","description":"In bubble sort, we compare adjacent elements and whenever we get a pair that is out of order, we swap them. Learn more about this sorting way","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/TV-Bubble-sort-in-DS.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/TV-Bubble-sort-in-DS.jpg","width":1200,"height":628,"caption":"Bubble sort in DS"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/bubble-sort-in-data-structure\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Bubble Sort in Data Structure"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81585"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/81839"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}