Bubblesort linked list duplicating not sorting
Apr 4, 2020 at 8:16pm UTC
Can someone point out where my bubblesort is not working. Its been awhile since I worked with bubblesort and cant find where the bug is.
As you see in the results report, I printed the eventList, bubblesorted, and printed the eventList again and it duplicates but also sorts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// ********************** bubbleSort event list **********************
void Process::bubbleSort()
{
eventNode *start = headEvent;
int swapped;
struct eventNode *ptr1;
struct eventNode *lptr = NULL;
/* Checking for empty list */
if (start == NULL)
return ;
do
{
swapped = 0;
ptr1 = start;
while (ptr1->nextEvent != lptr && ptr1->nextEvent != NULL)
{
if (ptr1->eventTime > ptr1->nextEvent->eventTime)
{
swap(ptr1, ptr1->nextEvent);
swapped = 1;
}
ptr1 = ptr1->nextEvent;
}
lptr = ptr1;
}
while (swapped);
}
void Process::swap(struct eventNode *a, struct eventNode *b)
{
float tempTime = a->eventTime;
int tempType = a->type;
struct processNode *tempLink = a->link;
a->eventTime = b->eventTime;
a->type = b->type;
a->link = b->link;
b->eventTime = tempTime;
b->type = tempType;
b->link = tempLink;
}
** Presort **
0 0
0.0280236 1
0.0779468 1
0.151887 1
0.0387333 1
0.00675372 1
0.0655049 1
0.336471 1
0.0635444 1
0.486677 1
0.270542 1
0.0280236 2
0.0779468 2
0.151887 2
0.0387333 2
0.00675372 2
0.0655049 2
0.336471 2
0.0635444 2
0.486677 2
**** Found bug before code that duplicated data, new results ****
**** Works as intended ****
0 0
0.00675372 1
0.0280236 1
0.0387333 1
0.0619352 2
0.0635444 1
0.0642583 2
0.0655049 1
0.076615 2
0.0779468 1
0.0874702 2
0.115727 2
0.149731 2
0.151887 1
0.270542 1
0.322952 2
0.335286 2
0.336471 1
0.486677 1
0.512198 2
Last edited on Apr 4, 2020 at 8:41pm UTC
Apr 4, 2020 at 8:33pm UTC
What's wrong with it? The original list had plenty of duplicates in it, so once you sort that's what you get.
Apr 4, 2020 at 8:37pm UTC
Just realized I have a bug before this that overrides good data as duplicate data from before and didnt realize. This post can be deleted if need be, sorry.
Topic archived. No new replies allowed.