C++ :: Applying Bubble Sort On Linked List
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
ADVERTISEMENT
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
Feb 1, 2015
I am relatively new to C++ and am trying to bubble sort my linked list that creates 100 random integers. Everything works, but I am unsure how to continue this to include a bubble sorting method.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Node{
public:
int data; //set data
[Code] ....
View 1 Replies
View Related
Feb 16, 2013
I have a code able to import a file containing words and numbers to a linked list, but I also need to sort this linked list alphabetically. I've done research on this involving bubble sorting, but no explanationcan achieve this objective.
Below is the code that can only put the file into linked list:
Code:
#include<iostream>
#include<conio.h>
#include"U:C++WordClass2WordClass2WordClass.cpp"
#include<fstream>
#include<vector>
#include<string>
using namespace std;
[Code] .....
View 5 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 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
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
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 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
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
Feb 19, 2014
You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.
You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.
I keep getting an error that the identifier for bubbleSort and selectionSort is not found. (Error C3861) Also, I feel like I'm missing something in int main() to get it to sort properly.
# include <iostream>
using namespace std;
int main()
{
[Code].....
View 14 Replies
View Related
Feb 20, 2012
how can i use my function now in the main ... after i put the numbers for example i want them to be sorted in ascending or descending order
PHP Code:
# include <iostream>
using namespace std;
int main () {
int array [10];
cout << " enter numbers : ";
for ( int i=0; i<10; i++)
[code]....
View 4 Replies
View Related
Mar 24, 2013
How to optimize this bubble sort program?
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main() {
long *A;
A = new long[100000000];
[Code]] .....
View 2 Replies
View Related
Nov 6, 2014
i'm trying to fill an array with random numbers and then sort them via bubblesort. it seems to work so far. the problem is, that i seem to get the same numbers for the same input. somehow the randomness isn't working.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int a, b, c, d, e, f;
}
[code]....
View 3 Replies
View Related
Mar 6, 2014
Trace the bubble sort using the following integers,which represent the elements in an array.
5,7,3,8,6,7,3
View 1 Replies
View Related
Oct 29, 2013
I have to sort golfers using bubble sort. Here my code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;
void tellUser();
double calcAvg( int, int, int, int, int &);
void swap( int &num1, int &num2);
[Code] ....
View 2 Replies
View Related
Jan 22, 2014
so my program reads a file type which looks like this...
Michelle 71
Marcie 99
David 42
Rebecca 83
Jonathan 79
Matthew 77
Rose 7
Melanie 75
Kimberly 73
Roger 74
Scott 76
Bradley 77
Drextell 10
Heidi 70
Alan 68
Pearl 13
Jeanne 43
Heber 55
Here is whats in the header of the class
class StudentStat {
private:
int Size;
[Code].....
So as of right now the names are stored in a Names string array and the scores are saved in a Score int array. So my question is where do I begin with my bubble sort? I need it to put the scores in descending order so from greatest score to lowest but I need the Names in the string array to still be connected to the number from the list. Never done a bubble sort before so not sure where to begin.
View 1 Replies
View Related
Dec 10, 2013
When I run the program the first int that is stored in the array keeps changing to -858993460.
#include <iostream>
using namespace std;
int main() {
int i;
int j;
int testScore[5];
int count;
[Code] .....
View 1 Replies
View Related
Sep 26, 2014
I'm trying to write a program that will print the data of 10 records from a file and then, sort them descending to ascending by year.
Here are the records:
NameYearTuition
David20111582.38
Sylvester2012728.82
Ben19920
Brandon1995500.25
Riley1997845.19
Mark2009700.05
Roberts2002450.87
Butler2005920.78
Steve20001000.00
Joe19941200.15
Here is my code so far:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
[Code] ....
How do I go about arranging it?
View 1 Replies
View Related
Feb 15, 2015
Design a C++ Program to implement the following functions:
a.)the function for bubble sorting : int bubblesort(int*a,int size).
b.)the function for merge sorting : int mergesort(int*a,int size).
c.)the function for generating array of random elements: int generate(int*a,int size) which calls the function rand() in c++.
d.)Test both bubble sorting and merge sorting with 10,100,1000,10000,100000 and 1000000,4000000 integers.
The integers are from the array generated by part c).calculate the time spent in calling each sorting.you may use a function in <time.h>to get the current time. Draw curves to compare the speed performance between the two functions. The merge sorting algorithm implementation must use recursion. you are expected to define a global array for holding result from merging two arrays.otherwise,it may cause a lot extra money in recursion.
Hint 1:use the following format to calculate the time cost for bubble sort.
{……..
Time1=Get the current time(call a function in <time.h>);
Bbblesort(….)
Tme2=Get the current time(call a function in <time.h>);
Time_cost=the difference between time 1 and tim2;
…….
}
Print your program and test results
View 1 Replies
View Related
May 15, 2014
I'll make a program that consists of "sorting and searching." To cope with the task, it should contain this: Create a field containing 50 random numbers, sort them by a method sort according to Bubble sort and then out the field before and after sorting. [URL]
View 1 Replies
View Related