Bubble Sort On Doubly Linked List
Last Updated :
11 Jan, 2023
Sort the given doubly linked list using bubble sort.
Examples:
Input : 5 4 3 2 1
Output : 1 2 3 4 5
Input : 2 1 3 5 4
Output :1 2 3 4 5
Explanation: As we do in the bubble sort, here also we check elements of two adjacent nodes whether they are in ascending order or not, if not then we swap the element. We do this until every element gets its original position.
In the 1st pass, the largest element gets its original position, and in the 2nd pass 2nd largest element gets its original position and in the 3rd pass, the 3rd largest element gets its original position, and so on.
And finally, the whole list gets sorted.
Note: If the list is already sorted then it will do only one pass.
Implementation:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
void insertAtTheBegin( struct Node **start_ref, int data)
{
struct Node *ptr1 = new Node;
ptr1->data = data;
ptr1->next = *start_ref;
if (*start_ref != NULL)
(*start_ref)->prev = ptr1;
*start_ref = ptr1;
}
void printList( struct Node *start)
{
struct Node *temp = start;
cout << endl;
while (temp!=NULL)
{
cout << temp->data << " " ;
temp = temp->next;
}
}
void bubbleSort( struct Node *start)
{
int swapped, i;
struct Node *ptr1;
struct Node *lptr = NULL;
if (start == NULL)
return ;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->next != lptr)
{
if (ptr1->data > ptr1->next->data)
{
swap(ptr1->data, ptr1->next->data);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}
int main()
{
int arr[] = {12, 56, 2, 11, 1, 90};
int list_size, i;
struct Node *start = NULL;
for (i = 0; i< 6; i++)
insertAtTheBegin(&start, arr[i]);
printf ( "\n Linked list before sorting " );
printList(start);
bubbleSort(start);
printf ( "\n Linked list after sorting " );
printList(start);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node prev;
Node next;
};
static Node insertAtTheBegin( Node start_ref, int data)
{
Node ptr1 = new Node();
ptr1.data = data;
ptr1.next = start_ref;
if (start_ref != null )
(start_ref).prev = ptr1;
start_ref = ptr1;
return start_ref;
}
static void printList( Node start)
{
Node temp = start;
System.out.println();
while (temp != null )
{
System.out.print( temp.data + " " );
temp = temp.next;
}
}
static Node bubbleSort( Node start)
{
int swapped, i;
Node ptr1;
Node lptr = null ;
if (start == null )
return null ;
do
{
swapped = 0 ;
ptr1 = start;
while (ptr1.next != lptr)
{
if (ptr1.data > ptr1.next.data)
{
int t = ptr1.data;
ptr1.data = ptr1.next.data;
ptr1.next.data = t;
swapped = 1 ;
}
ptr1 = ptr1.next;
}
lptr = ptr1;
}
while (swapped != 0 );
return start;
}
public static void main(String args[])
{
int arr[] = { 12 , 56 , 2 , 11 , 1 , 90 };
int list_size, i;
Node start = null ;
for (i = 0 ; i < 6 ; i++)
start=insertAtTheBegin(start, arr[i]);
System.out.printf( "\n Linked list before sorting " );
printList(start);
start = bubbleSort(start);
System.out.printf( "\n Linked list after sorting " );
printList(start);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .prev = None
self . next = None
def insertAtTheBegin(start_ref, data):
ptr1 = Node(data)
ptr1.data = data;
ptr1. next = start_ref;
if (start_ref ! = None ):
(start_ref).prev = ptr1;
start_ref = ptr1;
return start_ref
def printList(start):
temp = start;
print ()
while (temp ! = None ):
print (temp.data, end = ' ' )
temp = temp. next ;
def bubbleSort(start):
swapped = 0
lptr = None ;
if (start = = None ):
return ;
while True :
swapped = 0 ;
ptr1 = start;
while (ptr1. next ! = lptr):
if (ptr1.data > ptr1. next .data):
ptr1.data, ptr1. next .data = ptr1. next .data, ptr1.data
swapped = 1 ;
ptr1 = ptr1. next ;
lptr = ptr1;
if swapped = = 0 :
break
if __name__ = = '__main__' :
arr = [ 12 , 56 , 2 , 11 , 1 , 90 ]
start = None ;
for i in range ( 6 ):
start = insertAtTheBegin(start, arr[i]);
print ( "Linked list before sorting " , end = '');
printList(start);
bubbleSort(start);
print ( "\nLinked list after sorting " , end = '');
printList(start);
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node prev;
public Node next;
};
static Node insertAtTheBegin( Node start_ref, int data)
{
Node ptr1 = new Node();
ptr1.data = data;
ptr1.next = start_ref;
if (start_ref != null )
(start_ref).prev = ptr1;
start_ref = ptr1;
return start_ref;
}
static void printList( Node start)
{
Node temp = start;
Console.WriteLine();
while (temp != null )
{
Console.Write( temp.data + " " );
temp = temp.next;
}
}
static Node bubbleSort( Node start)
{
int swapped;
Node ptr1;
Node lptr = null ;
if (start == null )
return null ;
do
{
swapped = 0;
ptr1 = start;
while (ptr1.next != lptr)
{
if (ptr1.data > ptr1.next.data)
{
int t = ptr1.data;
ptr1.data = ptr1.next.data;
ptr1.next.data = t;
swapped = 1;
}
ptr1 = ptr1.next;
}
lptr = ptr1;
}
while (swapped != 0);
return start;
}
public static void Main(String []args)
{
int []arr = {12, 56, 2, 11, 1, 90};
int i;
Node start = null ;
for (i = 0; i < 6; i++)
start=insertAtTheBegin(start, arr[i]);
Console.Write( "\n Linked list before sorting " );
printList(start);
start = bubbleSort(start);
Console.Write( "\n Linked list after sorting " );
printList(start);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
this .prev = null ;
}
}
function insertAtTheBegin(start_ref, data) {
var ptr1 = new Node();
ptr1.data = data;
ptr1.next = start_ref;
if (start_ref != null ) start_ref.prev = ptr1;
start_ref = ptr1;
return start_ref;
}
function printList(start) {
var temp = start;
document.write( "<br>" );
while (temp != null ) {
document.write(temp.data + " " );
temp = temp.next;
}
}
function bubbleSort(start) {
var swapped;
var ptr1;
var lptr = null ;
if (start == null ) return null ;
do {
swapped = 0;
ptr1 = start;
while (ptr1.next != lptr) {
if (ptr1.data > ptr1.next.data) {
var t = ptr1.data;
ptr1.data = ptr1.next.data;
ptr1.next.data = t;
swapped = 1;
}
ptr1 = ptr1.next;
}
lptr = ptr1;
} while (swapped != 0);
return start;
}
var arr = [12, 56, 2, 11, 1, 90];
var i;
var start = null ;
for (i = 0; i < 6; i++) start = insertAtTheBegin(start, arr[i]);
document.write( "Linked list before sorting " );
printList(start);
start = bubbleSort(start);
document.write( "<br> Linked list after sorting " );
printList(start);
</script>
|
Output
Linked list before sorting
90 1 11 2 56 12
Linked list after sorting
1 2 11 12 56 90
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...