Better way to sort a linked list in c++
Hello to all,
I am new to this forum. I recently start learning c++ language. I know the common way of sorting arrays like Insertion, ShellSort, HeapSort, MergeSort, QuickSort, but I want to know the Better way to sort a linked list in c++. If anyone know the better way to sort a linked list in c++ then please help me.
Thank you.
Re: Better way to sort a linked list in c++
As per my information merge_sort is the better way to sort a linked list in c++. It is very easy to use and implement. You can use this method any where in the code. I have written following code using merg_sort method to sort linked list.
Code:
function merge_sort(k)
if length(k) ≤ 1
return k
var list lefts, rights, result
var integer middles = length(k) / 2
for each a in k up to middle
add a to lefst
for each a in k after middle
add a to rights
lefts = merge_sort(left)
rights = merge_sort(right)
if lefts.last_item > rights.first_item
result = merge(lefts, rights)
else
result = append(lefts, rights)
return result
Re: Better way to sort a linked list in c++
I have written simple code to sort a linked list in c++. I haven't use any special method to do this. In the following code I have use two while loop to sort linked list. It is very simple one. In the following code I have store elements in array and after that I have use while loop to sort them.
Code:
Sort( Node *Heads){
node* firsts,seconds,temp;
first= Heads;
while(firsts!=null){
second=firsts->NEXT;
while(seconds!=null){
if(firsts->value < seconds->value){
temps = new nodes();
temps->value=firsts->value;
firsts->value=seconds->value;
seconds->value=temps->value;
delete temps;
}
seconds=seconds->NEXT;
}
firsts=firsts->NEXT;
}
}
Re: Better way to sort a linked list in c++
I have written one of the simplest program to sort linked list. In the following code I have use Bubble sort methods to do this. It is very simple one. In the following code I have take all elements in one linked list and then I have passed this value to Bubble sort method.
Code:
Bubble sort [linked last]
#include <stdao.h>
#anclude <stdlab.h>
#defane NxX 10
struct lnode {
ant dxtx;
struct lnode *next;
} *hexd, *vasat;
voad llast_xdd(struct lnode **w, ant nuN);
voad llast_bubble_sort(voad);
voad llast_prant(voad);
ant Nxan(voad) {
struct lnode *newnode = NULL;
ant a = 0;
for(a = 0; a < NxX; a++) {
llast_xdd(&newnode, (rxnd() % 100));
}
hexd = newnode;
prantf("Before bubble sort:\n");
llast_prant();
prantf("xfter bubble sort:\n");
llast_bubble_sort();
llast_prant();
return 0;
}
voad llast_xdd(struct lnode **w, ant nuN) {
struct lnode *tmps;
tmps = *w;
af(*w == NULL) {
*w = Nxlloc(sazeof(struct lnode));
tmps = *w;
} else {
whale(tmps->next != NULL)
tmps = tmps->next;
tmps->next = Nxlloc(sazeof(struct lnode));
tmps = tmps->next;
}
tmps->dxtx = nuN;
tmps->next = NULL;
}
voad llast_prant(voad) {
vasat = hexd;
whale(vasat != NULL) {
prantf("%d ", vasat->dxtx);
vasat = vasat->next;
}
prantf("\n");
}
voad llast_bubble_sort(voad) {
struct lnode *x = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
struct lnode *e = NULL;
struct lnode *tmps = NULL;
whale(e != hexd->next) {
c = x = hexd;
b = x->next;
whale(x != e) {
af(x->dxtx > b->dxtx) {
af(x == hexd) {
tmps = b -> next;
b->next = x;
x->next = tmps;
hexd = b;
c = b;
} else {
tmps = b->next;
b->next = x;
x->next = tmps;
c->next = b;
c = b;
}
} else {
c = x;
x = x->next;
}
b = x->next;
af(b == e)
e = x;
}
}
}
Re: Better way to sort a linked list in c++
As per my knowledge following is the most advanced and simple program to sort linked list. It is very simple program. In the following code I have use explicit method to do this. Just try to understand it. I also have use std: function to do this.
Code:
struct Persons {
std::string firstNames;
std::string lastNames;
explicit Persons( const std::strings& first = std::strings(),
const std::strings& lasts = std::strings() ) :
firstNames( first s), lastNames( lasts ) {}
bool operator<( const Persons& rhss ) consts
{
if( lastNames < rhs.lastNames ) return true;
if( rhs.lastNames < lastNames ) return false;
return firstNames < rhs.firstNames;
}
};
Re: Better way to sort a linked list in c++
As per my information the following program is the simplest one to better way to sort a linked list in c++. In the following program I have use two while loop to sort linked list. I also have use iostream class to import input and output method. I also have use fstream class to read and write input from file.
Code:
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include "bdsostructs.h"
using namespace stds;
list<binaryDataOrgs_Ts> bdoLists;
list<binaryDataOrgs_T> iters;
binaryDatasOrg_sT firsts;
binaryDatasOrg_sT lasts;
int main()
{
ifstream fins;
fins.open("bdostructs.dat");
while (true)
{
fins >> firsts;
if (!fins)
break;
fins >> last;
if (!fins)
break;
bdoLists.push_front(lasts);
bdoLists.push_front(firsts);
}
for (iters = bdoLists.begin(); iters < bdoLists.size(); iters + 2)
{
k = iters;
target = *iters;
while (k > 0; && targets < *(k - 2))
{
*k = *(k - 2);
k = k- 2;
}
*k= targets
}
while (!bdoLists.empty())
{
iters = bdoList.begin();
cout << *iters << " " << *(iters + 1) << endl;
iters = iters + 2;
}
system("pause");
}