Bubblesort Algorithm

Hey im working on a program that takes in times for appointments. In this program i do use a bubblesort algorithm to display the appointments in time order. I was just wondering is it possible to create a bubblesort algorithm usig pointers and not an array like i have done in mine. If so could somebody kindly show me? would appreciate it.I have provided the code for my program below, its unfinished and messy, you'l also notice iv had an attempt at what im asking.. i know a pathetic attempt and i can see why mine wouldn't work lol.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//Appointment Calendar.cpp
//Application File
//Programmed by Ricky Wild

//--Pseudocode-----------------------------------------------------------------------------
//Display menu on choices to the user while choice != exit.
//If choice == create new appointment then get user to enter
//the subject of the appointment, its location and start/end time.
//Then add that information into linked list, only if Overlap returns false.
//Otherwise display an error message.
//----------------------------------------------------------------------------------------------

#include<iostream>
#include<stdlib.h>
#include<string>

using namespace std;

struct Appointment
{
	string Subject;
	string Location;
	double StartTime;
	double EndTime;
};

struct Node
{
	Appointment appointment;
	Node* pNext;
};

//Prototypes..
bool Overlap(Node* list, Node* nodeTooAdd, int totalApp);
void Display(Node* list);
void BubbleSort(Node anArray[], int ArraySize);

void main()
{
	cout << "\n\n\n\t\t\t   Console Appointment Calander\n\t\t\t   ----------------------------\n\n\n\t\t\t";

	Node sortedList[10];	//Bubblesorted array of appointments..
	int length = sizeof(sortedList)/sizeof(sortedList[0]);
	Node* theList = 0;
	Node* nodeTooAdd;
	Node* listPtr;
	Node* tempPtr;
	char choice = 'x'; //Assign any old valid value in intialization to enter while loop..
	int totalApp = 0;	//Total appointments made..
	bool overlap;

	while (choice != 'c') //choice c is exit program..
	{
		system("cls");
		cout << "\n\n\n\t\t\t   Console Appointment Calander\n\t\t\t   ----------------------------\n\n\t\t\tTotal Appointments: ";
		cout << totalApp << "\n\n\n\t\t\t";
		cout << "[a] create new appointment\n\t\t\t[b] display appointments\n\t\t\t[c] exit\n\n\t\t\t[Enter]: ";
		cin >> choice;
		switch(choice)
		{
		case 'a':
			cout << "\n\n--------------------------------------------------------------------------------\n\n\t\t\t"; 
			cout << "Please use 24 hour timing system!\n\n\t\t\t-- Example -------- \n\n\t\t\tStartTime: 11.35\n\t\t\tEndTime:   15.12";
			cout << "\n\n\t\t\t-------------------\n\n\n\t\t\t";
			nodeTooAdd = new Node;				//Reserve memory on the heap..
			//delete nodeTooAdd;					//Free memory on the heap..
			cout << "Subject: ";
			cin >> nodeTooAdd->appointment.Subject;
			cout << "\n\t\t\tLocation: ";
			cin >> nodeTooAdd->appointment.Location;
			cout << "\n\t\t\tStartTime: ";
			cin >> nodeTooAdd->appointment.StartTime;
			cout << "\n\t\t\tEndTime: ";
			cin >> nodeTooAdd->appointment.EndTime;
			if(theList == 0x0000000)		//if adding values into the linked list for the first time..
			{
				nodeTooAdd->pNext = theList;
				theList = nodeTooAdd;
				totalApp++;
			}
			else										//otherwise check for conflicts with possible exsisting start times..
			{
				totalApp++;						//Increment before check is made as this value is required in the confliction checking..
				overlap = Overlap(theList, nodeTooAdd, totalApp);
				if(!overlap)
				{
					nodeTooAdd->pNext = theList;
					theList = nodeTooAdd;
				}
				else
				{
					totalApp--;				//Deincrement the value as after the check is made we know there is a conflict with times, therfore information isn't added..
					cout << "\n\n\t\t\tError: There was a conflict with times!\n\n\n\t\t\t";
					system("pause");
				}
			}
			break;
		case 'b':
			//Move all elements of linked list into an array and then bubblesort before displaying the information..
			for(int i = 0; i < totalApp; i++)
			{
				sortedList[i].appointment = theList->appointment;
				theList = theList->pNext;
			}
			BubbleSort(sortedList, length);
			cout << "\n\n--------------------------------------------------------------------------------\n\n\t\t\t";
			cout << "Displaying appointments . . .";
			for (int i = 0; i < totalApp; i++)
			{
				cout << "\n\n\t\t\t-- Appointment --------------------- \n";
				cout << "\n\t\t\tSubject: " << sortedList[i].appointment.Subject;
				cout << "\n\t\t\tLocation: " << sortedList[i].appointment.Location;
				cout << "\n\t\t\tStartTime: " << sortedList[i].appointment.StartTime;
				cout << "\n\t\t\tEndTime: " << sortedList[i].appointment.EndTime << "\n\n\t\t\t-------------------------------------\n\t\t\t";
			}
			//Display(listPtr);
			system("pause");
			break;
		case 'c':
			system("cls");
			cout << "\n\n\n\n\n\n\t\t\t\tExiting program . . .";
			break;
		default:
			cout << "\n\n\t\t\tError: invalid input, please try again!\n\n\t\t\t";
			system("pause");
			break;
		}
	}
	
}

bool Overlap(Node* list, Node* nodeTooAdd, int totalApp)
{
	for (int i = 0; i < totalApp; i++)
	{
		if (list != 0)
		{
			if(list->appointment.StartTime == nodeTooAdd->appointment.StartTime)
			{
				list = list->pNext;
				return true;
			}
			list = list->pNext;
		}
	}
	return false;
}

void BubbleSort(Node anArray[], int ArraySize)
{
	Node temp;

	for (int outer = ArraySize - 2; outer >= 0 ; outer--)  
	{																		
		for (int inner = 0; inner <= outer; inner++) 
		{
			if(anArray[inner + 1].appointment.StartTime != -9.2559631349317831e+061) //-9.2559631349317831e+061
			{
				if (anArray[inner].appointment.StartTime > anArray[inner + 1].appointment.StartTime) 
				{
					temp.appointment = anArray[inner].appointment;
					anArray[inner].appointment = anArray[inner+1].appointment;
					anArray[inner + 1].appointment = temp.appointment;
				}
			}
		}
	}
}

//Node* BubbleSort(Node* theList, int totalApp)
//{
//	Node temp;
//	for(int outer = totalApp; outer >= 0; outer--)
//	{
//		for(int inner = 0; inner <= outer; inner++)
//		{
//			if(theList != 0)
//			{
//				if(theList->appointment.StartTime != -9.2559631349317831e+061)
//				{
//					if(theList->appointment.StartTime > theList->pNext->appointment.StartTime)
//					{
//						temp.appointment = theList->appointment;
//						theList->appointment = theList->pNext->appointment;
//						theList->pNext->appointment = temp.appointment;
//					}
//				}
//			}
//		}
//	}
//	return theList;
//}

void Display(Node* list)
{
	if (list != 0)
	{
		Display(list->pNext);
		cout << "\n\n\t\t\t-- Appointment --------------------- \n";
		cout << "\n\t\t\tSubject: " << list->appointment.Subject;
		cout << "\n\t\t\tLocation: " << list->appointment.Location;
		cout << "\n\t\t\tStartTime: " << list->appointment.StartTime;
		cout << "\n\t\t\tEndTime: " << list->appointment.EndTime << "\n\n\t\t\t-------------------------------------\n\t\t\t";
		list = list->pNext;
	}
}
first use i think it is insertion sort not bubble, insertion has a lower run time constant than bubble sort.

also when you pass an array to a function it interprets it as a pointer but arrays and pointers are interchangeable, try using vectors and iterators instead. This might also work faster and have simpler code if you have two pointers per node. the second pointing at the previous node, this is called a doubly linked list and the extra space necessary doesn't matter as I don't think you will be using more than a couple megs of RAM at the end of this project.
I suppose using the second pointer would be pretty useful, i think i might just stick to using the method im already using. Thanks for the input though!
Topic archived. No new replies allowed.