{"id":4486,"date":"2021-08-31T10:23:19","date_gmt":"2021-08-31T10:23:19","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4486"},"modified":"2023-07-27T12:29:03","modified_gmt":"2023-07-27T12:29:03","slug":"bubble-sort-for-linked-list-by-swapping-nodes","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/","title":{"rendered":"Bubble Sort in Linked List by Swapping Nodes"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png\" alt=\"\" \/><\/p>\n<p>Bubble Sort, renowned for its simplicity and elegance, stands as one of the earliest sorting algorithms. While traditionally used with arrays, this classic method gracefully extends its functionality to Linked Lists. In this concise exploration, we uncover the mechanics, advantages, and limitations of applying Bubble Sort in Linked Lists, appreciating its efficiency and timeless appeal in computer science.<\/p>\n<h2>What is Bubble sort?<\/h2>\n<p>Bubble sort is a simple and commonly used sorting algorithm in computer science. It is named &quot;bubble sort&quot; because smaller elements &quot;bubble&quot; to the top of the list while larger elements gradually &quot;sink&quot; to the bottom. It is an iterative algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.<\/p>\n<p>Bubble Sort by Swapping Nodes in Linked List<\/p>\n<h2>Swapping Nodes using Bubble Sort in Linked List<\/h2>\n<p>In this problem, we are given a singly linked list. We have to sort this list using bubble sort. <\/p>\n<p><strong>Note:<\/strong> The bubble sort will be applied on nodes instead of values i.e., We have to swap the nodes instead of values.<\/p>\n<h3>Understanding the working of Bubble Sort in Linked List<\/h3>\n<p>Let\u2019s try to understand the problem statement with the help of examples by referring the best websites to learn coding.<\/p>\n<p>Suppose the given list is 5 \u2192 1 \u2192 4 \u2192 2 \u2192 8. <\/p>\n<ul>\n<li>According to the problem statement, we have to sort the given list using bubble <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/recursive-selection-sort-for-singly-linked-list-swapping-node-links\/\" title=\"sort\">sort<\/a>. <\/li>\n<li>What do we usually do? We usually swap the node data. But here, we have to swap the nodes and not their data. <\/li>\n<li>In the first step, suppose we have to swap 5 and 1. So, we will swap the nodes and not their data. So, the linked list after the first step will be 1 \u2192 5 \u2192 4 \u2192 2 \u2192 8. In this way, swapping will happen, and our final sorted linked list will be 1 \u2192 2 \u2192 4 \u2192 5 \u2192 8<\/li>\n<\/ul>\n<p><strong>Input:<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-19.png\" alt=\"\" \/><\/p>\n<p><strong>Output:<\/strong> <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-9.png\" alt=\"\" \/><\/p>\n<p><strong>Explanation:<\/strong> As we can see, the given singly linked list has been sorted.<\/p>\n<p>This question is not a very complex one. We have to apply a normal bubble sort on the list. The only difference is that instead of swapping the data of adjacent nodes, we will swap the nodes. Let us have a glance at the approach.<\/p>\n<h2>Approach and Algorithm of bubble sort in linked list<\/h2>\n<h3>Swap() function<\/h3>\n<p>In the swap() function, we will see how we can swap two adjacent nodes. <\/p>\n<ul>\n<li>Let the two adjacent nodes be <strong>p1<\/strong> and <strong>p2<\/strong>. <\/li>\n<li>Now we will create 2 pointers <strong>ptr1<\/strong> and <strong>ptr2<\/strong>, and will make <strong>ptr1<\/strong> point to <strong>p1<\/strong> and <strong>ptr2<\/strong> point to <strong>p2<\/strong>.<\/li>\n<li>Next, we will create a pointer <strong>temp<\/strong>, and will make it point to the next of <strong>ptr2<\/strong>. <\/li>\n<li>We will make the next of <strong>ptr2<\/strong> point to <strong>ptr1<\/strong> and next of <strong>ptr1<\/strong> point to <strong>temp<\/strong>.<\/li>\n<li>In this way, following the above steps, the two adjacent nodes <strong>p1<\/strong> and <strong>p2<\/strong> will get swapped.<\/li>\n<\/ul>\n<h3>Dry Run (Swap() function)<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Bubble-Sort-for-Linked-List-by-Swapping-Nodes-1.png\" alt=\"\" \/><\/p>\n<h3>BubbleSort()<\/h3>\n<p>In this method, we will see how to perform Bubble Sort on the linked list. <\/p>\n<ul>\n<li>First, we need the count of the number of nodes in the list. The count can be found with a single list traversal. <\/li>\n<li>Now, the first loop is going to run from <strong>0<\/strong> to <strong>count-1<\/strong>. <\/li>\n<li>Inside the first loop, we will create a node pointer <strong>h<\/strong> that will point to the head and a variable <strong>swapped<\/strong>, initializing it with <strong>0<\/strong>.<\/li>\n<li>The nested loop will run from <strong>0<\/strong> to <strong>count-i-1<\/strong>. <\/li>\n<li>Inside the nested loop, we will check if the adjacent elements are following ascending order or not.\n<ul>\n<li>If any pair of adjacent nodes are not following the ascending order, we will swap the nodes and the value of swapped will become 1. <\/li>\n<\/ul>\n<\/li>\n<li>After the above, if condition, we will increment the <strong>h<\/strong>.<\/li>\n<li>Now, after the inner loop, if the value of the variable swapped remains 0, it means that the list is sorted, and we will break the loop. Otherwise we will continue with the loop.<\/li>\n<\/ul>\n<h3>Dry Run of Bubble sort<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Bubble-Sort-for-Linked-List-by-Swapping-Nodes-2.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Bubble-Sort-for-Linked-List-by-Swapping-Nodes-3.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Bubble-Sort-for-Linked-List-by-Swapping-Nodes-4.png\" alt=\"\" \/><\/p>\n<h2>Code Implementation of bubble sort in linked list<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4487 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_4487 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_4487 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4487 .wpsm_nav-tabs > li.active > a, #tab_container_4487 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4487 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_4487 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_4487 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4487 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4487 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4487 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4487 .wpsm_nav-tabs > li > a:hover , #tab_container_4487 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_4487 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_4487 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_4487 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_4487 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_4487 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_4487 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_4487 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4487 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_4487 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4487 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_4487 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_4487\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4487\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4487_1\" aria-controls=\"tabs_desc_4487_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4487_2\" aria-controls=\"tabs_desc_4487_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4487_3\" aria-controls=\"tabs_desc_4487_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4487_4\" aria-controls=\"tabs_desc_4487_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_4487\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_4487_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n  \r\n\/* structure for a node *\/\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n} Node;\r\n  \r\n\/*Function to swap the nodes *\/\r\nstruct Node* swap(struct Node* ptr1, struct Node* ptr2)\r\n{\r\n    struct Node* tmp = ptr2-&gt;next;\r\n    ptr2-&gt;next = ptr1;\r\n    ptr1-&gt;next = tmp;\r\n    return ptr2;\r\n}\r\n  \r\n\/* Function to sort the list *\/\r\nint bubbleSort(struct Node** head, int count)\r\n{\r\n    struct Node** h;\r\n    int i, j, swapped;\r\n  \r\n    for (i = 0; i &lt;= count; i++) {\r\n  \r\n        h = head;\r\n        swapped = 0;\r\n  \r\n        for (j = 0; j &lt; count - i - 1; j++) {\r\n  \r\n            struct Node* p1 = *h;\r\n            struct Node* p2 = p1-&gt;next;\r\n  \r\n            if (p1-&gt;data &gt; p2-&gt;data) {\r\n  \r\n                \/* update the link after swapping *\/\r\n                *h = swap(p1, p2);\r\n                swapped = 1;\r\n            }\r\n  \r\n            h = &amp;(*h)-&gt;next;\r\n        }\r\n  \r\n        \/* break if the loop ended without any swap *\/\r\n        if (swapped == 0)\r\n            break;\r\n    }\r\n}\r\n  \r\n\/* Function to print the list *\/\r\nvoid printList(struct Node* n)\r\n{\r\n    while (n != NULL) {\r\n        printf(&quot;%d -&gt; &quot;, n-&gt;data);\r\n        n = n-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n}\r\n  \r\n\/* Function to insert a struct Node\r\n   at the beginning of a linked list *\/\r\nvoid insertAtTheBegin(struct Node** start_ref, int data)\r\n{\r\n    struct Node* ptr1\r\n        = (struct Node*)malloc(sizeof(struct Node));\r\n  \r\n    ptr1-&gt;data = data;\r\n    ptr1-&gt;next = *start_ref;\r\n    *start_ref = ptr1;\r\n}\r\n  \r\n\/\/ Driver Code\r\nint main()\r\n{\r\n    int arr[] = { 78, 20, 10, 32, 1, 5 };\r\n    int list_size, i;\r\n  \r\n    \/* start with empty linked list *\/\r\n    struct Node* start = NULL;\r\n    list_size = sizeof(arr) \/ sizeof(arr[0]);\r\n  \r\n    \/* Create linked list from the array arr[] *\/\r\n    for (i = 0; i &lt; list_size; i++)\r\n        insertAtTheBegin(&amp;start, arr[i]);\r\n  \r\n    \/* print list before sorting *\/\r\n    printf(&quot;Linked list before sorting&#92;n&quot;);\r\n    printList(start);\r\n  \r\n    \/* sort the linked list *\/\r\n    bubbleSort(&amp;start, list_size);\r\n  \r\n    \/* print list after sorting *\/\r\n    printf(&quot;Linked list after sorting&#92;n&quot;);\r\n    printList(start);\r\n  \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4487_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;iostream&gt;\r\nusing namespace std;\r\n\r\nstruct Node \r\n{\r\n    int data;\r\n    struct Node* next;\r\n} Node;\r\n\r\nstruct Node* swap(struct Node* ptr1, struct Node* ptr2)\r\n{\r\n    struct Node* tmp = ptr2-&gt;next;\r\n    ptr2-&gt;next = ptr1;\r\n    ptr1-&gt;next = tmp;\r\n    return ptr2;\r\n}\r\n\r\nint bubbleSort(struct Node** head, int count)\r\n{\r\n    struct Node** h;\r\n    int i, j, swapped;\r\n  \r\n    for (i = 0; i &lt;  count; i++)\r\n    {\r\n  \r\n        h = head;\r\n        swapped = 0;\r\n  \r\n        for (j = 0; j &lt; count  - i -1; j++) \r\n        {\r\n  \r\n            struct Node* p1 = *h;\r\n            struct Node* p2 = p1-&gt;next;\r\n  \r\n            if (p1-&gt;data &gt; p2-&gt;data)\r\n            {\r\n\r\n                *h = swap(p1, p2);\r\n                swapped = 1;\r\n            }\r\n  \r\n            h = &amp;(*h)-&gt;next;\r\n        }\r\n\r\n        if (swapped == 0)\r\n            break;\r\n    }\r\n}\r\n\r\nvoid printList(struct Node* n)\r\n{\r\n    while (n != NULL)\r\n    {\r\n        cout &lt;&lt; n-&gt;data &lt;&lt; &quot; -&gt; &quot;;\r\n        n = n-&gt;next;\r\n    }\r\n    cout &lt;&lt; endl;\r\n}\r\n\r\nvoid insertAtTheBegin(struct Node** start_ref, int data)\r\n{\r\n    struct Node* ptr1\r\n        = (struct Node*)malloc(sizeof(struct Node));\r\n  \r\n    ptr1-&gt;data = data;\r\n    ptr1-&gt;next = *start_ref;\r\n    *start_ref = ptr1;\r\n}\r\n\r\nint main()\r\n{\r\n    int arr[] = { 8, 2, 4, 1, 5 };\r\n    int list_size, i;\r\n\r\n    struct Node* start = NULL;\r\n    list_size = sizeof(arr) \/ sizeof(arr[0]);\r\n\r\n    for (i = 0; i &lt; list_size; i++)\r\n        insertAtTheBegin(&amp;start, arr[i]);\r\n\r\n    cout &lt;&lt;&quot;Linked list before sorting&#92;n&quot;;\r\n    printList(start);\r\n\r\n    bubbleSort(&amp;start, list_size);\r\n\r\n    cout &lt;&lt;&quot;Linked list after sorting&#92;n&quot;;\r\n    printList(start);\r\n  \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4487_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n\/\/Bubble Sort For Linked List\r\n\/\/By using get higher node\r\npublic class LinkedList {\r\n\r\n  static class Node {\r\n    int data;\r\n    Node next;\r\n  }\r\n  public Node head;\r\n  \/\/Class constructors\r\n  LinkedList() {\r\n    head = null;\r\n  }\r\n  \/\/insert element\r\n  public void insert(int value) {\r\n    \/\/Create  node\r\n    Node node = new Node();\r\n    node.data = value;\r\n    node.next = null;\r\n    if (head == null) head = node;\r\n    else {\r\n      Node temp = head;\r\n      \/\/find last node\r\n      while (temp.next != null) {\r\n        temp = temp.next;\r\n      }\r\n      temp.next = node;\r\n    }\r\n\r\n  }\r\n  \/\/Display all Linked List elements\r\n  public void display() {\r\n    if (head != null) {\r\n\r\n      Node temp = head;\r\n      while (temp != null) {\r\n        System.out.print(&quot;  &quot; + temp.data);\r\n        temp = temp.next;\r\n      }\r\n    } else {\r\n      System.out.println(&quot;Empty Linked list&quot;);\r\n    }\r\n  }\r\n  \/\/perform bubble sort in single linked list\r\n  public void bubbleSort() {\r\n\r\n    if (head != null) {\r\n\r\n      Node current = null,\r\n        new_head = null,\r\n        move_node = null,\r\n        prev = null;\r\n\r\n      while (head != null) {\r\n        prev = null;\r\n        current = head;\r\n        move_node = head;\r\n        while (current != null) {\r\n\r\n          \/\/When current node value is grator than previous node\r\n          if (current.next != null &amp;&amp; current.next.data &gt; move_node.data) {\r\n            \/\/Swap node values\r\n            move_node = current.next;\r\n            prev = current;\r\n\r\n          }\r\n          current = current.next;\r\n        }\r\n        if (move_node == head) {\r\n          head = (head).next;\r\n        }\r\n\r\n        if (prev != null) {\r\n          prev.next = move_node.next;\r\n        }\r\n        move_node.next = new_head;\r\n        new_head = move_node;\r\n      }\r\n      \/\/make new head\r\n      head = new_head;\r\n\r\n    } else {\r\n      System.out.println(&quot;Empty Linked list&quot;);\r\n    }\r\n  }\r\n\r\n  public static void main(String[] args) {\r\n\r\n    LinkedList obj = new LinkedList();\r\n    \/\/insert element of linked list\r\n    obj.insert(7);\r\n    obj.insert(50);\r\n    obj.insert(9);\r\n    obj.insert(42);\r\n    obj.insert(5);\r\n    obj.insert(15);\r\n    System.out.print(&quot;Before sort : &quot;);\r\n\r\n    \/\/display all node\r\n    obj.display();\r\n\r\n    obj.bubbleSort();\r\n    System.out.print(&quot;&#92;nAfter sort  : &quot;);\r\n\r\n    obj.display();\r\n  }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4487_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n#Bubble Sort For Linked List\r\n#By using get higher node\r\nclass Node:\r\n    def __init__(self,data):\r\n        self.data=data\r\n        self.next=None\r\n\r\n#Create Class Linked    \r\nclass Linked:\r\n    def __init__(self):\r\n        #Assign default value\r\n        self.head=None\r\n\r\n    #insert new node to linked list  \r\n    def insert(self,data):\r\n        node=Node(data)\r\n        node.next=None\r\n        if self.head==None:\r\n            self.head=node\r\n        else:\r\n            temp=self.head\r\n            while temp.next!=None:\r\n                temp=temp.next\r\n            #add node    \r\n            temp.next=node\r\n\r\n    def display(self):\r\n        if(self.head==None):\r\n            print(&quot;Empty Linked List&quot;)\r\n            return\r\n\r\n        temp=self.head\r\n       \r\n        while(temp!=None):\r\n          print(temp.data,end=&quot; &quot;)\r\n          temp=temp.next\r\n    \r\n    #perform bubble sort in single linked list\r\n    def bubbleSort(self):\r\n\r\n        if(self.head!=None):\r\n\r\n          current=None\r\n          new_head=None\r\n          move_node=None\r\n          prev=None\r\n\r\n          while(self.head!=None):\r\n            prev=None\r\n            current=self.head\r\n            move_node=self.head\r\n            while(current!=None):\r\n\r\n              #When current node value is grator than previous node\r\n              if(current.next!=None and  current.next.data&gt;move_node.data):\r\n                #Swap node values\r\n                move_node=current.next\r\n                prev=current\r\n\r\n              \r\n              current=current.next\r\n            \r\n            if(move_node==self.head):\r\n              self.head=(self.head).next\r\n            \r\n\r\n            if(prev!=None):\r\n              prev.next=move_node.next\r\n            \r\n            move_node.next=new_head\r\n            new_head=move_node\r\n          \r\n          #make new head\r\n          self.head=new_head\r\n          \r\n\r\n        else:\r\n           print(&quot;Empty Linked list&quot;)\r\n\r\n\r\ndef main():\r\n    #Create Object of class Linked\r\n    obj=Linked()\r\n    #insert element of linked list\r\n    obj.insert(7)\r\n    obj.insert(50)\r\n    obj.insert(9)\r\n    obj.insert(42)\r\n    obj.insert(5)\r\n    obj.insert(15)\r\n    print(&quot;Before sort : &quot;)\r\n    \r\n    #display all node\r\n    obj.display()\r\n\r\n    obj.bubbleSort()\r\n    print(&quot;&#92;nAfter sort  : &quot;)\r\n\r\n    obj.display()\r\nif __name__==&quot;__main__&quot;:\r\n    main()\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_4487 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_4487 a\"),jQuery(\"#tab-content_4487\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h3>Output<\/h3>\n<p>Linked list before sorting<br \/>\n5 -&gt; 1 -&gt; 4 -&gt; 2 -&gt; 8 -&gt;<br \/>\nLinked list after sorting<br \/>\n1 -&gt; 2 -&gt; 4 -&gt; 5 -&gt; 8 -&gt; <\/p>\n<p><strong>Time Complexity:<\/strong> O(n<sup>2<\/sup>), as a nested traversal, is needed.<\/p>\n<p>**Conclusion**<br \/>\nBubble sort is a simple sorting algorithm that may be used to sort linked lists by exchanging nodes. Although it is not the most efficient approach for huge lists, it might be beneficial in some situations or as a learning exercise. Here are some frequently asked questions about utilising bubble sort in a linked list by exchanging nodes.<\/p>\n<p>## Frequently Asked Questions<\/p>\n<p>**Q1. How does the swapping of nodes work in bubble sort for a linked list?**<br \/>\nIn the linked list implementation of bubble sort, swapping nodes involves rearranging the pointers of adjacent nodes to change their positions within the list.<\/p>\n<p>**Q2. What is the time complexity of bubble sort in a linked list?**<br \/>\nThe time complexity of bubble sort in a linked list is O(n^2), where &#8220;n&#8221; represents the number of nodes in the list. This is because each pass through the list requires comparing and potentially swapping adjacent nodes.<\/p>\n<p>**Q3. Is bubble sort efficient for large linked lists?**<br \/>\nNo, bubble sort is not efficient for large linked lists. Its quadratic time complexity makes it less suitable for handling large datasets. Other sorting algorithms like to merge sort or quicksort are more efficient choices for larger lists.<\/p>\n<p>**Q4. What are some advantages of using bubble sort in a linked list?**<br \/>\nBubble sort is relatively easy to implement for linked lists and requires minimal memory usage. It can be useful for educational purposes or for sorting small lists where simplicity is more important than efficiency.<\/p>\n<p>**Q5. Can bubble sort be used for sorting doubly linked lists?**<br \/>\nYes, bubble sort can be applied to doubly linked lists as well. The swapping of nodes in a doubly linked list involves adjusting the pointers of the adjacent nodes to properly change their positions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bubble Sort, renowned for its simplicity and elegance, stands as one of the earliest sorting algorithms. While traditionally used with arrays, this classic method gracefully extends its functionality to Linked Lists. In this concise exploration, we uncover the mechanics, advantages, and limitations of applying Bubble Sort in Linked Lists, appreciating its efficiency and timeless appeal [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-4486","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Bubble Sort in Linked List by Swapping Nodes<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to sort a linked list by using bubble sort and swapping nodes instead of the data. This blog explains how to sort a linked list using bubble sort.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bubble Sort in Linked List by Swapping Nodes\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to sort a linked list by using bubble sort and swapping nodes instead of the data. This blog explains how to sort a linked list using bubble sort.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-31T10:23:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-27T12:29:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\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:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Bubble Sort in Linked List by Swapping Nodes\",\"datePublished\":\"2021-08-31T10:23:19+00:00\",\"dateModified\":\"2023-07-27T12:29:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/\"},\"wordCount\":992,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/\",\"name\":\"Bubble Sort in Linked List by Swapping Nodes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png\",\"datePublished\":\"2021-08-31T10:23:19+00:00\",\"dateModified\":\"2023-07-27T12:29:03+00:00\",\"description\":\"Learn the most efficient way to sort a linked list by using bubble sort and swapping nodes instead of the data. This blog explains how to sort a linked list using bubble sort.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Bubble Sort in Linked List by Swapping Nodes\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bubble Sort in Linked List by Swapping Nodes","description":"Learn the most efficient way to sort a linked list by using bubble sort and swapping nodes instead of the data. This blog explains how to sort a linked list using bubble sort.","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:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/","og_locale":"en_US","og_type":"article","og_title":"Bubble Sort in Linked List by Swapping Nodes","og_description":"Learn the most efficient way to sort a linked list by using bubble sort and swapping nodes instead of the data. This blog explains how to sort a linked list using bubble sort.","og_url":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-31T10:23:19+00:00","article_modified_time":"2023-07-27T12:29:03+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Bubble Sort in Linked List by Swapping Nodes","datePublished":"2021-08-31T10:23:19+00:00","dateModified":"2023-07-27T12:29:03+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/"},"wordCount":992,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/","url":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/","name":"Bubble Sort in Linked List by Swapping Nodes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png","datePublished":"2021-08-31T10:23:19+00:00","dateModified":"2023-07-27T12:29:03+00:00","description":"Learn the most efficient way to sort a linked list by using bubble sort and swapping nodes instead of the data. This blog explains how to sort a linked list using bubble sort.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185674947-Bubble_sort-06.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/bubble-sort-for-linked-list-by-swapping-nodes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Bubble Sort in Linked List by Swapping Nodes"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4486","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=4486"}],"version-history":[{"count":15,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4486\/revisions"}],"predecessor-version":[{"id":17381,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4486\/revisions\/17381"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4486"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}