473,396 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

bubble sort

31
related to my earlier post, I have a linked list of polynomials and I am trying to do a bubble sort to arrange the polynomials in descending order based on the degree.

Here's the code I have:

Expand|Select|Wrap|Line Numbers
  1. void Polynomial::orderDescending(){
  2.     Polynomial newPoly;
  3.     newPoly.head = this->head;
  4.  
  5.     Term * temp1 = this->head;
  6.     Term * temp2 = temp1->next;
  7.  
  8.  
  9.     for (int pass = 1; pass < this->numTerms; pass++) {
  10.         for (int count = 0; count < this->numTerms-1; count++) {
  11.             if (temp1->deg < temp2->deg) {
  12.                 temp1->next = temp2->next;
  13.                 temp2->next = temp1;
  14.  
  15.                 if (count = 0) {
  16.                     this->head = temp2;
  17.                 }
  18.  
  19.                 temp2 = temp1->next;
  20.             }
  21.             else{
  22.                 temp1 = temp1->next;
  23.                 temp2 = temp2->next;
  24.             }
  25.         }
  26.  
  27.         temp1 = this->head;
  28.         temp2 = temp1->next;
  29.     }
  30. }
  31.  
the program ran into a run-time error because the code didn't allow me to reset the temp pointer's head into the (now) swapped linked list in the outer loop.

Specifically, line 16 didn't seem to change the linked list which I wanted to implement in line 27.

For example I have the linked list (the data denotes the degree I want to arrange):

4 -> 5 -> 7

After the first inner loop ends, I want the linked list to be changed to

5 -> 7 -> 4.

This doesn't happen in the code.

Instead the list became
7->4 only.

Can anyone check how to correct my code?
Thanks
Oct 13 '07 #1
1 1517
blackx
31
related to my earlier post, I have a linked list of polynomials and I am trying to do a bubble sort to arrange the polynomials in descending order based on the degree.

Here's the code I have:

Expand|Select|Wrap|Line Numbers
  1. void Polynomial::orderDescending(){
  2.     Polynomial newPoly;
  3.     newPoly.head = this->head;
  4.  
  5.     Term * temp1 = this->head;
  6.     Term * temp2 = temp1->next;
  7.  
  8.  
  9.     for (int pass = 1; pass < this->numTerms; pass++) {
  10.         for (int count = 0; count < this->numTerms-1; count++) {
  11.             if (temp1->deg < temp2->deg) {
  12.                 temp1->next = temp2->next;
  13.                 temp2->next = temp1;
  14.  
  15.                 if (count = 0) {
  16.                     this->head = temp2;
  17.                 }
  18.  
  19.                 temp2 = temp1->next;
  20.             }
  21.             else{
  22.                 temp1 = temp1->next;
  23.                 temp2 = temp2->next;
  24.             }
  25.         }
  26.  
  27.         temp1 = this->head;
  28.         temp2 = temp1->next;
  29.     }
  30. }
  31.  
the program ran into a run-time error because the code didn't allow me to reset the temp pointer's head into the (now) swapped linked list in the outer loop.

Specifically, line 16 didn't seem to change the linked list which I wanted to implement in line 27.

For example I have the linked list (the data denotes the degree I want to arrange):

4 -> 5 -> 7

After the first inner loop ends, I want the linked list to be changed to

5 -> 7 -> 4.

This doesn't happen in the code.

Instead the list became
7->4 only.

Can anyone check how to correct my code?
Thanks
I got my code to work now, hooray!
Oct 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
1
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
9
by: mosullivan | last post by:
I can't figure out how to print after every pass through the bubble sort. I'm supposed to display the sort after every pass through the loop. Below is what I have so far. #include <stdio.h>...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.