{"id":16634,"date":"2020-07-11T18:19:06","date_gmt":"2020-07-11T12:49:06","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/"},"modified":"2024-09-03T11:24:51","modified_gmt":"2024-09-03T05:54:51","slug":"bubble-sort","status":"publish","type":"post","link":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/","title":{"rendered":"What is Bubble Sort Algorithm Using C,C++, Java and Python"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"what-is-bubble-sort\"><strong>What is Bubble Sort <\/strong><\/h2>\n\n\n\n<p>Bubble sort is one of the easiest and brute force sorting algorithm. It is used to sort elements in either ascending or descending order. Every element is compared with every other element in bubble sort.<\/p>\n\n\n\n<p>It basically does swapping of elements if they are not in the right order depending on their value and the intended order. A nested loop will be used to implement this algorithm.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-pseudocode\"><strong>Bubble Sort Pseudocode<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>We are given with an input array which is supposed to be sorted in ascending order<\/li>\n\n\n\n<li>We start with the first element and i=0 index and check if the element present at i+1 is greater then we swap the elements at index i and i+1.<\/li>\n\n\n\n<li>If above is not the case, then no swapping will take place.<\/li>\n\n\n\n<li>Now&nbsp; \u201c i \u201d gets incremented and the above 2 steps happen again until the array is exhausted.<\/li>\n\n\n\n<li>We will ignore the last index as it is already sorted.<\/li>\n\n\n\n<li>Now the largest element will be at the last index of the array.<\/li>\n\n\n\n<li>Now we will again set i=0 and continue with the same steps that will eventually place second largest at second last place in the array. Now the last 2 indexes of the array are sorted.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-algorithm\"><strong>Bubble Sort Algorithm<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Bubble Sort(arr, size)\n\t\tfor i=0 to n-i-1\n\t\t\tfor j=0 to n-i-2\n\t\t\t\tif arr&#091;j]&gt;arr&#091;j+1]\n\t\t\t\t\tSwap arr&#091;j] and arr&#091;j+1]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-algorithm-dry-run\"><strong>Bubble Sort Algorithm Dry Run<\/strong><\/h3>\n\n\n\n<p>input:<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>23<\/td><td>10<\/td><td>16<\/td><td>11<\/td><td>20<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=0\t<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>16<\/td><td>11<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=1<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td>16<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=2<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td>16<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=3<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td>16<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>After i=4<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>0<\/td><td>1<\/td><td>2<\/td><td>3<\/td><td>4<\/td><\/tr><tr><td>10<\/td><td>11<\/td><td>16<\/td><td>20<\/td><td>23<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-time-complexity\"><strong>Bubble Sort Time Complexity<\/strong><br><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each and every element is compared with the other elements for array which takes n time<\/li>\n\n\n\n<li>And the above steps continues for n iterations<\/li>\n\n\n\n<li>Best Time Complexity: O(n^2)<\/li>\n\n\n\n<li>Average Time Complexity: O(n^2)<\/li>\n\n\n\n<li>Worst Time Complexity: O(n^2)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-space-complexity\"><strong>Bubble Sort Space Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No auxiliary space is required in bubble sort implementation<\/li>\n\n\n\n<li>Hence space complexity is: O(1)<\/li>\n<\/ul>\n\n\n\n<p>Now we are going to implement Bubble sort in different programming languages such as C,C++, <a rel=\"noreferrer noopener\" aria-label=\"Python, (opens in a new tab)\" href=\"https:\/\/www.mygreatlearning.com\/blog\/python-tutorial-for-beginners-a-complete-guide\/\" target=\"_blank\">Python,<\/a> and Java<\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-java-programming-1.png\"><a href=\"https:\/\/www.mygreatlearning.com\/academy\/learn-for-free\/courses\/java-programming-in-hindi\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"1000\" height=\"242\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-java-programming-1.png\" alt=\"\" class=\"wp-image-16660\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-java-programming-1.png 1000w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-java-programming-1-300x73.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-java-programming-1-768x186.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-java-programming-1-696x168.png 696w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-in-c\"><strong>Bubble Sort in C&nbsp;<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid bubbleSort(int arr&#091;], int size) {\n\n \n  for (int j = 0; j &lt; size - 1; ++j) {\n    for (int i = 0; i &lt; size - j - 1; ++i) {\n      \n      if (arr&#091;i] &gt; arr&#091;i + 1]) {\n        \n        int temp = arr&#091;i];\n        arr&#091;i] = arr&#091;i + 1];\n        arr&#091;i + 1] = temp;\n      }\n    }\n  }\n}\n\nvoid display(int arr&#091;], int size) {\n  for (int i = 0; i &lt; size; ++i) {\n    printf(\"%d  \", arr&#091;i]);\n  }\n  printf(\"n\");\n}\n\nint main() {\n  int arr&#091;] = {-2, 45, 0, 11, -9};\n  int size = sizeof(arr) \/ sizeof(arr&#091;0]);\n  bubbleSort(arr, size);\n  printf(\"Sorted array:n\");\n  display(arr, size);\n}\n\n<\/code><\/pre>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy PRO<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-from-scratch\" class=\"courses-cta-title-link\">C Programming Course: Master C with Hands-on Projects<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Join our C Programming Course and learn C syntax, operators, expressions, control flow, functions, pointers, structures, file handling, memory management, and modular programming. Build real-world skills through practical projects!<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>2 Projects<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>10 Hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-from-scratch\" class=\"courses-cta-button\">\n                C Programming Course with Certificate\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output-of-the-program\"><strong>Output of the program:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted array:\n-9  -2  0  11  45  \n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-in-java\"><strong>Bubble Sort in JAVA<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\nclass BubbleSort {\n  void bubbleSort(int arr&#091;]) {\n    int size = arr.length;\n    \n   \n    for (int i = 0; i &lt; size - 1; i++)\n      for (int j = 0; j &lt; size - i - 1; j++)\n\n        if (arr&#091;j] &gt; arr&#091;j + 1]) {\n\n          int temp = arr&#091;j];\n          arr&#091;j] = arr&#091;j + 1];\n          arr&#091;j + 1] = temp;\n        }\n  }\n\n  void display(int arr&#091;]) {\n     int size = arr.length;\n\t for (int i = 0; i &lt; size; i++)\n\t\tSystem.out.println(arr&#091;i]+\" \");\n\t\n   }\n  public static void main(String args&#091;]) {\n    int&#091;] arr = { -2, 45, 0, 11, -9 };\n    BubbleSort bs = new BubbleSort();\n    bs.bubbleSort(arr);\n    System.out.println(\"Sorted array::\");\n    bs.display(arr);\n  }\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output-of-the-program\"><strong>Output of the program:<\/strong><br><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted array:\n-9 \n-2 \n0 \n11 \n45\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-in-c\"><strong>Bubble Sort in C++&nbsp;<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nvoid bubbleSort(int arr&#091;], int size) {\n\n\n  for (int j = 0; j &lt; size - 1; ++j) {\n    for (int i = 0; i &lt; size - j - 1; ++i) {\n\n      if (arr&#091;i] &gt; arr&#091;i + 1]) {\n\n        int temp = arr&#091;i];\n        arr&#091;i] = arr&#091;i + 1];\n        arr&#091;i + 1] = temp;\n      }\n    }\n  }\n}\n\nvoid display(int arr&#091;], int size) {\n  for (int i = 0; i &lt; size; ++i) {\n    cout &lt;&lt; \"  \" &lt;&lt; arr&#091;i];\n  }\n  cout &lt;&lt; \"n\";\n}\n\nint main() {\n  int arr&#091;] = {-2, 45, 0, 11, -9};\n  int size = sizeof(arr) \/ sizeof(arr&#091;0]);\n  bubbleSort(arr, size);\n  cout &lt;&lt; \"Sorted array :n\";\n  display(arr, size);\n}\n<\/code><\/pre>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-title-link\">C++ Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Beginner to Advanced Level<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>8.1 hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"output-of-the-program\"><strong>Output of the program:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted array :\n  -9  -2  0  11  45\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"bubble-sort-in-python\"><strong>Bubble Sort in Python<\/strong><br><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def bubbleSort(array): \n    length = len(array) \n  \n\n    for i in range(length-1): \n        for j in range(0, length-i-1): \n   \n            if array&#091;j] &gt; array&#091;j+1] : \n                array&#091;j], array&#091;j+1] = array&#091;j+1], array&#091;j] \n\narr = &#091;10 7 8 9 1 5 ] \nprint (\"Elements of array before sorting:\") \nElements of array before sorting:\nfor i in range(len(arr)): \n    print (\"%d\" %arr&#091;i]), \n\nbubbleSort(arr) \n  \nprint (\"Elements of array after sorting:\") \nfor i in range(len(arr)): \n    print (\"%d\" %arr&#091;i]),  \n\n<\/code><\/pre>\n\n\n\n<p>Output of the program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Elements of array before sorting:\n10 7 8 9 1 5 \nElements of array after sorting:\n1 5 7 8 9 10 <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"modified-bubble-sort\"><strong>Modified Bubble Sort&nbsp;<\/strong><br><\/h2>\n\n\n\n<p>Bubble sort complexities remain o(n2) is all cases including sorted array. We can reduce the time complexity to O(n) if the array is already sorted.Also,we need to introduce a flag variable to stop the bubble sort as soon as it becomes sorted.<\/p>\n\n\n\n<p>The flag variable is initialized as true in every iteration and in for loop if array goes in for swapping we will initialize it to false. But if no swapping takes place in the inner loop then its value will remain true and we will have an if condition after the nested loop that will check the flag value and if flag value remains true, we will break<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modified-bubble-sort-algorithm\"><strong>Modified Bubble Sort Algorithm<\/strong><br><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>bubbleSort(arr)\n  flag = false\n  for i=0 to n-1\n    for j=0 to n-1-i\n\t  if leftEle &gt; rightEle\n\t\tswap leftEle and rightEle\n\t\tflag =true\n\t  if flag is true\n\t\tbreak\nend \n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modified-bubble-sort-time-complexity\"><strong>Modified Bubble Sort Time Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Best Time Complexity : O(n), i.e when the elements in the given array are sorted.So, only once the every element is accessed or traversed.&nbsp;<\/li>\n\n\n\n<li>Average Time Complexity : O(n^2)<\/li>\n\n\n\n<li>Worst Time Complexity : O(n^2)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modified-bubble-sort-space-complexity\"><strong>Modified Bubble Sort Space Complexity<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No auxiliary space is required in bubble sort implementation<\/li>\n\n\n\n<li>Hence space complexity is : O(1)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modified-bubble-sort-in-c\"><strong>Modified Bubble Sort in C&nbsp;<\/strong><br><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n\nvoid bubbleSort(int arr&#091;], int size) {  \/\/ sorting function\n  for (int j = 0; j &lt; size - 1; ++j) {\n\n    int flag = 0;\n\n    for (int i = 0; i &lt; size - j - 1; ++i) {\n\n      if (arr&#091;i] &gt; arr&#091;i + 1]) {\n        \n        int temp = arr&#091;i];\n        arr&#091;i] = arr&#091;i + 1];\n        arr&#091;i + 1] = temp;\n        flag = 1;\n      }\n    }\n\n    if (flag == 0)\n      break;\n  }\n}\n\nvoid display(int arr&#091;], int size) {\n  for (int i = 0; i &lt; size; ++i) {\n    printf(\"%d  \", arr&#091;i]);\n  }\n  printf(\"n\");\n}\n\nint main() { \/\/ main function or driver function\n  int arr&#091;] = {-2, 45, 0, 11, -9};\n printf(\"Elements before Sorting:n\");\n  display(arr, size);\n\n  int size = sizeof(arr) \/ sizeof(arr&#091;0]);\n  bubbleSort(arr, size);\n printf(\"Elements after Sorting:n\");\n  display(arr, size);\n}<\/code><\/pre>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy PRO<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-from-scratch\" class=\"courses-cta-title-link\">C Programming Course: Master C with Hands-on Projects<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Join our C Programming Course and learn C syntax, operators, expressions, control flow, functions, pointers, structures, file handling, memory management, and modular programming. Build real-world skills through practical projects!<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>2 Projects<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>10 Hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-from-scratch\" class=\"courses-cta-button\">\n                C Programming Course with Certificate\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p>Output of the program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Elements before Sorting:\n-2 45 0 11 -9 10\nElements after Sorting:\n-9  -2  0 10 11  45  <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modified-bubble-sort-in-java\"><strong>Modified Bubble Sort in JAVA<\/strong><br><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class BubbleSort {\n  void bubbleSort(int arr&#091;]) {   \/\/sorting method\n    int size = arr.length;\n\n  \n    for (int i = 0; i &lt; size - 1; i++) {\n \n      boolean flag = true;\n      for (int j = 0; j &lt; size - i - 1; j++) {\n\n        if (arr&#091;j] &gt; arr&#091;j + 1]) {\n          \n          int temp = arr&#091;j];\n          arr&#091;j] = arr&#091;j + 1];\n          arr&#091;j + 1] = temp;\n          \n          flag = false;\n        }\n      }\n      \n      if (flag == true)\n        break;\n    }\n  }\n  void display(int arr&#091;]) {   \/\/method for displaying the elements\n     int size = arr.length;\n\t for (int i = 0; i &lt; size; i++)\n\t\tSystem.out.println(arr&#091;i]+\" \");\n\t\n   }\n\n  public static void main(String args&#091;]) {   \/\/main method or driver method\n    int&#091;] arr = { -2, 45, 0, 11, -9 };\n\n    BubbleSort  bs = new BubbleSort();\n    System.out.println(\"Elements before Sorting:\");\n    bs.display(arr);\n    bs.bubbleSort(arr);\n    System.out.println(\"Elements after Sorting:\");\n    bs.display(arr);\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output of the program:<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Elements before Sorting:\n-2 \n45\n0\n11\n-9\nElements after Sorting:\n-9 \n-2 \n0 \n11 \n45\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modified-bubble-sort-in-c\"><strong>Modified Bubble Sort in C++&nbsp;<\/strong><br><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nvoid bubbleSort(int arr&#091;], int size) {  \/\/sorting function\n  for (int j = 0; j &lt; size - 1; ++j) {\n\n    int flag = 0;\n    for (int i = 0; i &lt; size - j - 1; ++i) {\n      if (arr&#091;i] &gt; arr&#091;i + 1]) {\n\n        int temp = arr&#091;i];\n        arr&#091;i] = arr&#091;i + 1];\n        arr&#091;i + 1] = temp;\n        flag = 1;\n      }\n    }\n\n    if (flag == 0)\n      break;\n  }\n}\n\nvoid display(int arr&#091;], int size) { \/\/ display function\n  for (int i = 0; i &lt; size; ++i) {\n    cout &lt;&lt; \"  \" &lt;&lt; arr&#091;i];\n  }\n  cout &lt;&lt; \"n\";\n}\n\nint main() {                   \/\/ main function or driver function\n  int arr&#091;] = {-2, 45, 0, 11, -9};\n  int size = sizeof(arr) \/ sizeof(arr&#091;0]);\n  bubbleSort(arr, size);\n  cout &lt;&lt; \"Sorted array :n\";\n  display(arr, size);\n}\n<\/code><\/pre>\n\n\n\n    <div class=\"courses-cta-container\">\n        <div class=\"courses-cta-card\">\n            <div class=\"courses-cta-header\">\n                <div class=\"courses-learn-icon\"><\/div>\n                <span class=\"courses-learn-text\">Academy Pro<\/span>\n            <\/div>\n            <p class=\"courses-cta-title\">\n                <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-title-link\">C++ Programming Course<\/a>\n            <\/p>\n            <p class=\"courses-cta-description\">Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool.<\/p>\n            <div class=\"courses-cta-stats\">\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-user-icon\"><\/div>\n                    <span>Beginner to Advanced Level<\/span>\n                <\/div>\n                <div class=\"courses-stat-item\">\n                    <div class=\"courses-stat-icon courses-star-icon\"><\/div>\n                    <span>8.1 hrs<\/span>\n                <\/div>\n            <\/div>\n            <a href=\"https:\/\/www.mygreatlearning.com\/academy\/premium\/learn-c-programming-for-beginners-to-advanced\" class=\"courses-cta-button\">\n                Start Free Trial\n                <div class=\"courses-arrow-icon\"><\/div>\n            <\/a>\n        <\/div>\n    <\/div>\n\n\n\n<p><strong>Output of the program:<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted array :\n  -9  -2  0  11  45\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"modified-bubble-sort-in-python\"><strong>Modified Bubble Sort in Python<\/strong><br><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>def bubbleSort(arr):\n    \n  \n    for i in range(len(arr)):\n        \n        flag = True\n        for j in range(0, len(arr) - i - 1):\n\n            if arr&#091;j] &gt; arr&#091;j + 1]:\n\n                (arr&#091;j], arr&#091;j + 1]) = (arr&#091;j + 1], arr&#091;j])\n                flag = False\n                \n                if flag:\n                    break\n\n\narr = &#091;-2, 45, 0, 11, -9]\nbubbleSort(arr)\nprint('Sorted array:')\nprint(arr)\n<\/code><\/pre>\n\n\n\n<p><strong>Output of the program:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sorted array:\n&#091;-9, -2, 0, 11, 45]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"bubble-sort-example\"><strong>Bubble Sort Example<\/strong><\/h2>\n\n\n\n<p><strong>Example1:<\/strong><\/p>\n\n\n\n<p>If the input array is sorted, return 1 else return 0. Assuming sorting here is in ascending order.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Input: <\/li>\n\n\n\n<li>1 2 3 4 5 6 <\/li>\n\n\n\n<li>90 80 55 70 40 10<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Output<\/li>\n\n\n\n<li>1<\/li>\n\n\n\n<li>0<br><\/li>\n<\/ul>\n\n\n\n<p>Code for implementation<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.* ;\n\nclass Main { \n  \n   \n    static boolean check(int arr&#091;], int n) \n    { \n  \n        if (n == 0 || n == 1) \n            return true; \n  \n        for (int i = 1; i &lt; n; i++) \n  \n            if (arr&#091;i - 1] &gt; arr&#091;i]) \n                return false; \n  \n        return true; \n    } \n  \n    public static void main(String&#091;] args) \n    { \n  \t\tScanner in=new Scanner(System.in);\n\t\tint n=in.nextInt();\n\t\tint arr&#091;] = new int&#091;n];\n\t\tfor(int i=0;i&lt;n;i++)\n\t\t    arr&#091;i]=in.nextInt();\n\t\t\n        if (check(arr, n)) \n            System.out.print(\"Array is sorted\"); \n        else\n            System.out.print(\"Array is unsorted\"); \n    } \n} \n<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Input:12 45 78 90 45 10\n\nOutput:Array is unsorted\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-selection-bubble-and-insertion-sort\"><strong>Difference between Selection, Bubble and Insertion Sort<\/strong><br><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of algorithm<\/strong>\n<ul class=\"wp-block-list\">\n<li>In Bubble sort, adjacent elements are compared and sorted if they are in the wrong order.<\/li>\n\n\n\n<li>In Selection Sort, we select the smallest element and swap it with the 0th index element in the first iteration. This selection continues for n-1 elements and the single element is already sorted and we will have array sorted by 1 element in every iteration<\/li>\n\n\n\n<li>In Insertion sort, we create partitions of sorted and unsorted parts. One by one element from the sorted art is taken and sent to the unsorted part for checking and placing it to the right position in sorting using swaps.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of time and space complexity<\/strong>\n<ul class=\"wp-block-list\">\n<li>All 3 sort have O(n2) time complexity. But via flag variable we can reduce the time complexity of bubble and insertion to O(n) is the best case.<\/li>\n\n\n\n<li>Space Complexity is the same for all i.e., O(1)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><br><\/td><td><strong>Best<\/strong><\/td><td><strong>Average<\/strong><\/td><td><strong>Worst<\/strong><\/td><td><strong>Space<\/strong><\/td><\/tr><tr><td><strong>Selection<\/strong><\/td><td>O(n2)<\/td><td>O(n2)<\/td><td>O(n2)<\/td><td>O(1)<\/td><\/tr><tr><td><strong>Bubble<\/strong><\/td><td>O(n)<\/td><td>O(n2)<\/td><td>O(n2)<\/td><td>O(1)<\/td><\/tr><tr><td><strong>Insertion<\/strong><\/td><td>O(n)<\/td><td>O(n2)<\/td><td>O(n2)<\/td><td>O(1)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of speed<\/strong>\n<ul class=\"wp-block-list\">\n<li>Bubble sort is slower than the maximum sort algorithm.<\/li>\n\n\n\n<li>There are a lot of swaps that might take place in the worst case.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of in-place<\/strong>\n<ul class=\"wp-block-list\">\n<li>In-place states that the algorithm is in-place if it does not need extra memory barring some variable creation which counts to constant space.<\/li>\n\n\n\n<li>Selection, Bubble and Insertion are in-place algorithms and do not require any auxiliary memory.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>In terms of stability<\/strong>\n<ul class=\"wp-block-list\">\n<li>Stability states that the algorithm is stable if the relative ordering of the same elements in the input and output array remains the same.<\/li>\n\n\n\n<li>Bubble and Insertion are stable algorithms but the naive selection is not as swapping may cost stability.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>Selection<\/strong><\/td><td><strong>Bubble<\/strong><\/td><td><strong>Insertion<\/strong><\/td><\/tr><tr><td>Select smallest in every iteration do single swap<\/td><td>An adjacent swap of every element with the other element where ordering is incorrect<\/td><td>Take and put the element one by one and put it in the right place in the sorted part.<\/td><\/tr><tr><td>Best case time complexity is O(n2)<\/td><td>Best case time complexity is O(n)<\/td><td>Best case time complexity is O(n)<\/td><\/tr><tr><td>Works better than bubble as no of swaps are significantly low<\/td><td>Worst efficiency as too many swaps are required in comparison to selection and insertion<\/td><td>Works better than bubble as no of swaps are significantly low<\/td><\/tr><tr><td>It is in-place<\/td><td>It is in-place<\/td><td>It is in-place<\/td><\/tr><tr><td>Not stable<\/td><td>Stable<\/td><td>Stable<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This brings us to the end of this article where we learned about bubble sort and its implementation in different languages. Also, we learned how to optimize this algorithm to get better performance. I would highly suggest to take up this free course for data structures and algorithms to improve your coding skills.<\/p>\n\n\n<figure class=\"wp-block-image size-large zoomable\" data-full=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1.png\"><img decoding=\"async\" width=\"1000\" height=\"242\" src=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1.png\" alt=\"Bubble sort algorithm with Great learning academy\" class=\"wp-image-16657\" srcset=\"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1.png 1000w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1-300x73.png 300w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1-768x186.png 768w, https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/June-29-banner-for-GL-data-structures-1-696x168.png 696w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Bubble Sort Bubble sort is one of the easiest and brute force sorting algorithm. It is used to sort elements in either ascending or descending order. Every element is compared with every other element in bubble sort. It basically does swapping of elements if they are not in the right order depending on [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":16792,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36858],"content_type":[],"class_list":["post-16634","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-sorting-algorithm"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>What is Bubble Sort Algorithm Using C,C++, Java,Python | Great Learning<\/title>\n<meta name=\"description\" content=\"What is Bubble Sort: Bubble sort is one of the easiest and brute force sorting algorithm used to sort elements in either ascending or descending order.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is Bubble Sort Algorithm Using C,C++, Java and Python\" \/>\n<meta property=\"og:description\" content=\"What is Bubble Sort: Bubble sort is one of the easiest and brute force sorting algorithm used to sort elements in either ascending or descending order.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-11T12:49:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-03T05:54:51+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"700\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Great Learning Editorial Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/Great_Learning\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Great Learning Editorial Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"What is Bubble Sort Algorithm Using C,C++, Java and Python\",\"datePublished\":\"2020-07-11T12:49:06+00:00\",\"dateModified\":\"2024-09-03T05:54:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/\"},\"wordCount\":1076,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Info-graphics-1000X700-04.jpg\",\"keywords\":[\"sorting algorithm\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/\",\"name\":\"What is Bubble Sort Algorithm Using C,C++, Java,Python | Great Learning\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Info-graphics-1000X700-04.jpg\",\"datePublished\":\"2020-07-11T12:49:06+00:00\",\"dateModified\":\"2024-09-03T05:54:51+00:00\",\"description\":\"What is Bubble Sort: Bubble sort is one of the easiest and brute force sorting algorithm used to sort elements in either ascending or descending order.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Info-graphics-1000X700-04.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/Blog-Info-graphics-1000X700-04.jpg\",\"width\":1000,\"height\":700,\"caption\":\"BubbleSort_GL\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/bubble-sort\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"IT\\\/Software Development\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/software\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"What is Bubble Sort Algorithm Using C,C++, Java and Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"What is Bubble Sort Algorithm Using C,C++, Java,Python | Great Learning","description":"What is Bubble Sort: Bubble sort is one of the easiest and brute force sorting algorithm used to sort elements in either ascending or descending order.","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:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/","og_locale":"en_US","og_type":"article","og_title":"What is Bubble Sort Algorithm Using C,C++, Java and Python","og_description":"What is Bubble Sort: Bubble sort is one of the easiest and brute force sorting algorithm used to sort elements in either ascending or descending order.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","article_published_time":"2020-07-11T12:49:06+00:00","article_modified_time":"2024-09-03T05:54:51+00:00","og_image":[{"width":1000,"height":700,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg","type":"image\/jpeg"}],"author":"Great Learning Editorial Team","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/Great_Learning","twitter_site":"@Great_Learning","twitter_misc":{"Written by":"Great Learning Editorial Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"What is Bubble Sort Algorithm Using C,C++, Java and Python","datePublished":"2020-07-11T12:49:06+00:00","dateModified":"2024-09-03T05:54:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/"},"wordCount":1076,"commentCount":0,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg","keywords":["sorting algorithm"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/","url":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/","name":"What is Bubble Sort Algorithm Using C,C++, Java,Python | Great Learning","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg","datePublished":"2020-07-11T12:49:06+00:00","dateModified":"2024-09-03T05:54:51+00:00","description":"What is Bubble Sort: Bubble sort is one of the easiest and brute force sorting algorithm used to sort elements in either ascending or descending order.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg","width":1000,"height":700,"caption":"BubbleSort_GL"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/bubble-sort\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"IT\/Software Development","item":"https:\/\/www.mygreatlearning.com\/blog\/software\/"},{"@type":"ListItem","position":3,"name":"What is Bubble Sort Algorithm Using C,C++, Java and Python"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg",1000,700,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04-150x150.jpg",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04-300x210.jpg",300,210,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04-768x538.jpg",768,538,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg",1000,700,false],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg",1000,700,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg",1000,700,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg",640,448,false],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg",96,67,false],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2020\/07\/Blog-Info-graphics-1000X700-04.jpg",150,105,false]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"What is Bubble Sort Bubble sort is one of the easiest and brute force sorting algorithm. It is used to sort elements in either ascending or descending order. Every element is compared with every other element in bubble sort. It basically does swapping of elements if they are not in the right order depending on&hellip;","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=16634"}],"version-history":[{"count":21,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16634\/revisions"}],"predecessor-version":[{"id":111497,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/posts\/16634\/revisions\/111497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/16792"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=16634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=16634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=16634"},{"taxonomy":"content_type","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/content_type?post=16634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}