Bubble sort, Selection sort & Insertion sort


So I'm new to C++ and am needing a little help. All feedback is welcome.
I am suppose to create an AList object and then use that AList object to make the following method calls in order:

Read
Print
BubbleSort
Print
Read
Print
InsertionSort
Print
Read
Print
SelectionSort
Print


This is what I have so far:
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
#include <iostream>
using namespace std;
/*

	
	This program will create an AList object and will then make some method calls.
	*/
	
const int MLS = 50;
typedef int element;
const element SENTINEL = -1;

class AList {
	private:
		element items [MLS];
		int size;
		void Swap(int pos1,int pos2);
		int Read_element();
		
		public:
			void Read();
			void Print();
			void BubbleSort();
			void InsertionSort();
			void SelectionSort();
			
		};
		int main (){
		int a;
			AList B;
			
			B.Read();
			B.Print();
			B.BubbleSort();
			B.Print();
			
			B.Read();
			B.Print();
			B.InsertionSort();
			B.Print();
			
			B.Read();
			B.Print();
			B.SelectionSort();
			B.Print();
		}
		element Read_element() {
			element userval;
			cout <<"Enter a whole number:";
			cin>>userval;
			while (!cin.good());
			cout<<"invalid choice please enter a whole number";
			cin.clear();
			cin.ignore(80, '\n');
			cin>>userval;
			return userval;
		}
		void AList::Read() {
			//PRE: None
			//POST: the N.O. Alist is valid
			//using elements provided by the user
			element userval;
			cout<<"Enter elements,";	
			
		while ((userval!=SENTINEL)&&(size<MLS)) {
			items[size] = userval;
			size ++;
		}
	}
		void AList::Print() {
			for (int i=0;i<size;i++)
				cout<<items[i]<<endl;
			}
		void AList::BubbleSort(){
			//PRE: the N.O. List is void
			//Post: the N.O. Alist is unchanged except
			//its elements are now in ascending order
			for (int i=0; size=-1; i++) {
				for (int j=0; j<size-1-i; j++)
					if (items[j]>items[j+1])
				Swap (j,j+1);
				else
					;
				}
			}
		
		void AList::Swap(int pos1,int pos2) {
			element temp;
			temp = items[pos2];
			items[pos2] = items[pos1];
			items[pos1] = temp;
		}
		
		void AList::InsertionSort() {
			//Pre: the N.O. Alist is valid
			//Post: the N.O. Alist is unchanged, except that
			//its elements are now in ascending order
			
			int j;
			bool done;
			
			for (int i = 1; i<size; i++) {
				j=i;
				done=false;
				while ((j>=1)&&(!done))
				if (items[j] < items[j-1]){
				
				Swap(j, j-1);
				j-=1;
			}
			else
				done = true;
			}
		}
		void AList::SelectionSort() {
			
			int maxpos;
			
			for (int i = size - 1; i > 0; i--) {
				maxpos = 0;
				for (int j = 1; j <= i; j++)
					if (items[j] > items[maxpos])
						maxpos = j;
					else
						;
				Swap(maxpos, i);
				}
			}





I don't think that I'm doing it in the order that needs to be done and I'm also having a problem printing everything. Please help!
Last edited on
I recommend you to edit your post and past all the code again (with proper indenting) and use the code-tags. Just select your (properly indented) code and click the "<>" button next to the input field.

It will add line numbers to your code, so people can refer to it.

For now, I think you should repair the InsertionSort for loop:
- i is not being incremented
- it is an infinate loop
- it is equal to just j=size, but a lot less efficient

In addition, I think you want to fix the else statement in SelectionSort, now swap will always be done.

Finally I would recommend you to use the curly brackets even it they may not be necessary, as they make it obvious what the intended scopes are. Personally I think you left out a couple too many.

I did not try to read the rest because it was too hard without proper formatting.
I corrected it.
Topic archived. No new replies allowed.