C++ :: Quick Sort Using Linked List?
Feb 23, 2013
I've been trying to implement a quick sort algorithm for linked list but when i try to run it with 1000000 values my compiler tells me its running too long and ends up not finishing. However when i run it with 10 values it seems to work fine.
Is there anyway to improve this run time?
Code: struct listnode *quicksort( struct listnode *data ) {
size_t length = 0;
struct listnode *pivNode, *temp = NULL, *low = NULL, *high = NULL, *ltail = NULL, *htail = NULL, *prev = NULL, *end = NULL, *next;
// Get length
for( struct listnode *cursor = data; cursor != NULL; cursor = cursor->next ) {
length++;
}
[code].....
View 2 Replies
ADVERTISEMENT
Mar 4, 2014
I am trying to sort a linked list using quick sort in C. Here is my code--Actually, first I am inserting data in the list from a file. For a small file, it's working fine. But for large file it's just not working.
Code:
struct node {
int data;
struct node *link;
struct node *plink;
[Code] .....
View 1 Replies
View Related
Oct 19, 2013
I'm trying to sort the elements in a linked list which contain a variable amount of data in any given case. In the sample code, the code is more static, but I plan on adding it to much more dynamic code once I have it figured out. My main problem is that I am not sure how to sort the linked list while still keeping the correct pointers to the nodes. I thought about writing my own custom quick sort instead of using the C-standard library function, but how I would keep the pointers to the next nodes correct eluded me. Here is my code so far :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
[Code]......
View 3 Replies
View Related
May 14, 2013
My whole code is included below, although what is really important to me is only the first two functions, the quicksort and the partition...Whenever I build this program, it keeps returning an error for the calling of the partition function, citing that the first variable is in an improper format... I don't know why it's not working though..
Whole Code:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
template <class T>
void quicksort(T arr[], int start, int end)
[code]....
View 6 Replies
View Related
May 16, 2013
I'm tinkering with a serial quick sort algorithm. Normally I use qsort() but I wanted to test this.
Somehow I loose the highest number. I can't see where I drop it.
Code:
// function to quicksort, leave them in front of qsort.
void swap(float_t *left, float_t *right){
float_t temp;
temp = *left;
*left = *right;
*right = temp;
[Code] .....
View 2 Replies
View Related
Mar 22, 2013
I know how to add a node and delete a node from the beginning and from the end of the List. Now I want to sort the List by Age. I tried everything but there must be a very little mistake that I can't find. Here is my code:
#include <iostream>
using namespace std;
struct Node {
char name[20];
int age;
double height;
[Code] ....
View 13 Replies
View Related
Feb 13, 2013
I have written this code to arrange user input array in an order.
//Recursive Quick Sort
#include<stdio.h>
#define ARRAY_SIZE 10
void quick_sort(int array[], int len)
}
[code].....
View 6 Replies
View Related
Oct 15, 2014
This is in response to the bubble sort and selection sorts for linked lists. On my system, (Intel 2600K, 3.4ghz), it sorts a list with 4,194,304 nodes containing 64 bit unsigned integers in about 1.05 seconds.
Code:
#define NUMLISTS 32
/* number of lists */
typedef unsigned long long UI64;
typedef struct NODE_{
struct NODE_ * next;
UI64 data;
[Code]....
View 6 Replies
View Related
Feb 27, 2015
I'm trying to apply a bubble sort on a linked list. It works in the first traversal, but then after the code cPtr = nPtr;, it inputs repeated digits at the end of the (semi-sorted) linked lists.
View 1 Replies
View Related
Oct 3, 2014
This is in-place merge sort, for merge function.
LinkedListNode::LinkedListNode(int value) {
this->next = NULL;
this->value = value;
}
LinkedListNode *mergeSortedLinkedLists(LinkedListNode *firstList, LinkedListNode *secondList)
[Code] ....
View 3 Replies
View Related
Oct 7, 2013
I'm having trouble getting my sort function to work,
void Linkedlist::sort(int num)
{
node *q;
node *a;
int input=num;
[Code].....
I get a crash and I've narrowed it down to the last if statement. Also this function is meant to handle new members after 1 member has been added.
View 7 Replies
View Related
Apr 3, 2013
I am making a custom linked list (for fun!) and want to implement a sort method that sorts the stored data using the stored types > and < operators (will require the type to have these operators overloaded)
What is the best way to do this? I guess the first thing one might jump to would be to compare the current node with the the node to its "right" see if one is greater than the other. Then keep iterating through the list until you don't swap any more nodes. However I am thinking there is probably a more efficient way to do this. Here is my code so far:
#ifndef LIST_HPP
#define LIST_HPP
#include <string>
[Code].....
Surely there is a way to sort with only one iteration through the list?
View 2 Replies
View Related
Apr 18, 2013
At the line number 65 that's my sort method first i sum up all the value in the nodes after that i want to sort the Nodes In ascending order but the method is not working ...
#include <iostream>
#include <conio.h>
using namespace std;
// Node Class
[Code] ....
View 3 Replies
View Related
Mar 16, 2014
is this correct? I used this sorting with numbers i don't know if it is the same with strings. When I run it, there are no errors detected, but when i try to view it, the inputs does not appear.
void add(node **h, node **t){
node *temp, *ptr;
char s[20];
temp = (node*) malloc(sizeof(node));
printf ("-INSERT-");
printf("Fruit: ");
scanf("%s", temp->fruit);
[Code] .....
View 1 Replies
View Related
Nov 30, 2013
HelI have been tasked with creating a program which (1) takes in integer values from a user (until the user enters -1) and inputs these values into a linked list. This (2)original list is then to be printed out. The program then uses the algorithm "bubble sort" to (3)order the list in descending order before finally printing it out again.
I have managed to do this but I kind of cheated since I do not quite understand how to manipulate a linked list. What did was I took the values in the linked list and transferred them into an array and then did bubble sort on that array.how to do bubble sort on a linked list as well as how to print a linked list.
Code:
#include<stdio.h>#include<stdlib.h>
typedef struct node
{
int info;
struct node *link;
}Node, *NodePointer;
void printList (NodePointer head)
}
[code]...
View 4 Replies
View Related
Aug 12, 2013
i am making a program that reads a file from .txt and print them out using linked list. However, i need to sort them from the highest price to lowest price.
Code:
/* my structs */
typedef struct{
Node *head;
Node *tail;
Node *iterator;
int size;
} List;
[Code]...
i know its long but im afraid that i might miss out some things.
View 12 Replies
View Related
Jan 11, 2013
i want to make a program that accepts 15 items in a singly linked and sorts the nodes.
View 1 Replies
View Related
Apr 29, 2013
I'm trying to write a function that takes two linked lists and creates a third one with only the common elements.
It assumes the first list (the caller) has no dups, but it doesn't seem to be working. The program doesn't crash, it just hangs when it is supposed to display L3 (the third list)..everything else runs and is displayed fine.
template <typename T>
LList <T> LList <T>:: common (LList <T> &B)//common fct
{
Node <T> *hunter1 = Head;
[Code]......
View 10 Replies
View Related
Jun 29, 2013
I have a linked list comprised of chars like so...
Code:
node1 - "p"
node2 - "o"
node3 - "p"
I need a function that will take in three perameters...node *replaceChar(node *head, char key, char *str)Stipulations of this function. head is the head of the list, 'key' and 'str' are guaranteed to contain alphanumeric characters only (A-Z, a-z, and 0-9). str can range from 1 to 1023 characters (inclusively). So if I call this function with these perameters..
Code:
node *head == /*the head of the list to be examined*/
char key == "p"char *str == "dog"The new list will look like this...
node1 - 'd'
node2 - 'o'
node3 - 'g'
node4 - 'o'
node5 - 'd'
node6 - 'o'
node7 - 'g'
All instances of 'p' were replaced with 'dog' I have a toString function which takes in a string and converts it to a linked list and returns the head. So assume that you can call the function on str = "dog" so...
Code:
toString(str) == /*this will return the head to the list made from the str*/
If it's unclear what my question is...I am stumped on how to write the replaceChar function the one that takes in three perameters..
View 3 Replies
View Related
Feb 19, 2013
i write code that's merge two linked list without sort ....
node * merage (node * list1, node * list2) {
// check witch list is empty
if (list1 == NULL) return list2;
if (list2 == NULL) return list1;
if ( list1 == NULL && list2 == NULL ) return NULL;
[Code].....
View 1 Replies
View Related
Mar 15, 2014
I am getting an error trying to convert from nodeType<Type> to nodeType<Type>* in my recursiveSort function and why this is happening.
#ifndef H_linkedListIterator
#define H_linkedListIterator
#include <iostream>
template <class Type>
struct nodeType {
Type info;
[Code] ....
View 7 Replies
View Related
Dec 31, 2014
Code:
// Write a function called insertEntry() to insert a new entry into a linked list.
Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.
// The function dveloped in exercise 2 only inserts an element after an existing element in the list, thereby prenting you from inserting a new entry at the front of the list.
(Hint: Think about setting up a special structure to point to the beginning of the list.)
#include <stdio.h
struct entry1 {
int value;
struct entry1 *next;
};
[code]...
This is a working version of the exercise, but I don't think I'm doing what's asked. I was able to add an element to the beginning of the list using an if statement, not creating a special structure that points to the beginning of the list. How would I go about creating a special structure that points to the beginning of the list to add a new element at the beginning of the list?
View 8 Replies
View Related
Feb 24, 2013
I've written code for a selection sort algorithm to sort through a singly linked list, However, it only brings the smallest node to the front, but doesnt swap positions of any of the remaining nodes, even though they are not in order.
Input list: 3 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 0 6 12 16
Sorted list: 0 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 3 6 12 16
Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int val;
struct node *next;
[Code] ......
View 1 Replies
View Related
Aug 12, 2014
What I want to create is a program that sorts through a huge list (millions of lines).
I want it to get rid of any line that contains a word that isn't in an English dictionary.
Example list:
00sdfdsf
ahdadsg
angel
ksjflsjdf
green
green000
carrot
and it would go through millions like that, giving me only:
angel
green
carrot
as my new list.
How could I go about this? What extra programs would I need?
View 9 Replies
View Related
Feb 18, 2014
I am looking for a function or algorithm to best merge and sort similar content between two lists of unordered strings each in individual files (very large files ~200mb each).
For example, these files have a common first string and are merged based on them:
File 1:
red, apple
green, truck
blue, car
yellow, ball
orange, candy
File 2:
gold, necklace
green, tree
yellow, sticker
blue, water
red, bag
I am looking for the following output:
Output:
red, apple, bag
green, truck, tree
blue, car, water
yellow, ball, sticker
orange, candy
gold, necklace
View 6 Replies
View Related
Jul 26, 2013
When I call stl sort with a vector in the following,
Code:
int main() {
vector<int> items;
sort(items.begin(), items.end(), greater<int>());
return 0;
}
It works. But if I call stl sort the same way with a list, there is compiler errors.
Code:
int main() {
list<int> items;
sort(items.begin(), items.end(), greater<int>());
return 0;
}
Why?
View 2 Replies
View Related