Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
TempBubbleDouble
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//g++ 7.4.0 //Templated Bubble Sort in Double Linked List //code credit internet // template modifications by Rezaul Hoque on March 10,2021 #include <iostream> #include <bits/stdc++.h> using namespace std; template <class T> class node { public: T val; node<T> *next; node<T> *prev; }; template <class T> void swap(node<T> * left, node<T>* right) { T temp = left->val; left->val = right->val; right->val= temp; } template <class T> void bubble(node<T>* left)//credit prodevelopertutorial { int swapon; node<T> *leftpt; node<T> *rightpt = NULL; do { swapon = 0; leftpt = left; while(leftpt->next != rightpt) { if (leftpt->val > leftpt->next->val) { swap(leftpt, leftpt->next); swapon = 1; } leftpt = leftpt->next; } rightpt = leftpt; }while(swapon); } int main() { node<int > *head = (node<int>*) malloc(sizeof( node<int>)); head->val = 2; head->prev = NULL; node<int > *l1 = (node<int>*) malloc(sizeof( node<int>)); l1->val = 8; l1->prev = head; head->next = l1; node<int> *l2 = (node<int>*) malloc(sizeof( node<int>)); l2->val = 3; l2->prev = l1; l1->next = l2; node<int>*l3 = (node<int>*) malloc(sizeof( node<int>)); l3->val = 5; l3->prev = l2; l2->next = l3; node<int> *l4 = (node<int>*) malloc(sizeof( node<int>)); l4->val = 10; l4->prev = l3; l3->next = l4; l4->next = NULL; node<char>* chead = (node<char>*)malloc(sizeof(node<char>)); chead->val='G';chead->prev = NULL; node<char>* cl1 = (node<char>*)malloc(sizeof(node<char>)); cl1->val = 'E'; cl1->prev = chead; chead->next = cl1; node<char>* cl2= (node<char>*)malloc(sizeof(node<char>)); cl2->val = 'H'; cl2->prev = cl1; cl1->next= cl2; node<char>* cl3 = (node<char>*)malloc(sizeof(node<char>)); cl3->val = 'F'; cl3->prev = cl2; cl2->next= cl3; node<char>* cl4 = (node<char>*)malloc(sizeof(node<char>)); cl4->val= 'A'; cl4->prev = cl3; cl3->next= cl4; cl4->next = NULL; cout<<"After bubble sort:\n"; bubble(head); while (head != NULL) { cout<<" "<<head->val; head = head->next; } cout <<endl; bubble(chead); while (chead != NULL) { cout<<" "<<chead->val; chead = chead->next; } return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 1.42 sec, absolute running time: 0.15 sec, cpu time: 0.01 sec, memory peak: 5 Mb, absolute service time: 1,59 sec
edit mode
|
history
|
discussion
After bubble sort: 2 3 5 8 10 A E F G H