Help! My program is not performing bubblesort and I\'m not sure why. #include #i
ID: 3681995 • Letter: H
Question
Help! My program is not performing bubblesort and I'm not sure why. #include
#include
using namespace std;
const int MLS = 50;
typedef int element;
const element SENTINEL = -1;
element read_element();
class Alist {
private:
element items[MLS];
int size;
void Swap(int,int);
public:
void Read();
void Print();
void BubbleSort();
};
Alist B;
int main(){
B.Read();
B.Print();
B.BubbleSort();
B.Print(); }
void Alist::Print(){
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist has had items elements displayed to the user.
for(int i=0; i < size; i++)
B.Read();
B.Print();
B.BubbleSort();
for(int i=0; i < size; i++)
cout << items[i] << endl; }
//void Alist::Print(){
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist has had items elements displayed to the user
//for(int i=0; i < size; i++)
//cout << items[i] << endl;
//} void Alist::Read() {
//PRE: none
//POST: The N.O. Alist is valid, using data from user. element userval;
size = 0;
cout << "Enter elements," << SENTINEL << " to stop: ";
userval = read_element ();
while((userval != SENTINEL) && (size < MLS)) {
items [size] = userval; size++;
if (size < MLS) userval = read_element();
else
cout << "List is now full." << endl; }}
void Alist::BubbleSort() {
//PRE: The N.O. Alist is valid.
//POST: The N.O. Alist is unchanged, except its elements have been
//put into ascending order.
for (int i=0; i< size-1; i++) {
for (int j=0; j< size-1-i; j++)
if (items[j] > items[j+1]) Swap(j,j+1); } }
void Alist::Swap(int pos1, int pos2) {
int temp;
//PRE: The N.O. Alist is valid.
//POST: Must be >= 0 and size of pos2
//Must be >=0 and < size
element temp;
temp = items[pos1];
items[pos1] = items[pos2];
items[pos2] = temp; }
int read_element() {
int t;
cin>>t;
return t;
}
Explanation / Answer
Hi i have corrected your code. It is working now. Please test it.
#include<iostream>
using namespace std;
const int MLS = 50;
typedef int element;
const element SENTINEL = -1;
element read_element();
class Alist {
private:
element items[MLS];
int size;
void Swap(int,int);
public:
Alist();
void Read();
void Print();
void BubbleSort();
};
int main(){
Alist B;
B.Read();
cout<<"Before sorting: "<<endl;
B.Print();
B.BubbleSort();
cout<<"After sorting: "<<endl;
B.Print();
}
Alist::Alist(){
size = 0;
}
void Alist::Print(){
for(int i=0; i < size; i++)
cout << items[i] << " ";
cout<<endl;
}
void Alist::Read() {
cout << "Enter elements," << SENTINEL << " to stop: ";
int userval = read_element ();
while((userval != SENTINEL) && (size < MLS)) {
items [size] = userval; size++;
if (size < MLS)
userval = read_element();
else
cout << "List is now full." << endl;
}
}
void Alist::BubbleSort() {
for (int i=0; i< size-1; i++) {
for (int j=0; j< size-1-i; j++)
if (items[j] > items[j+1])
Swap(j,j+1);
}
}
void Alist::Swap(int pos1, int pos2) {
element temp;
temp = items[pos1];
items[pos1] = items[pos2];
items[pos2] = temp;
}
int read_element() {
int t;
cin>>t;
return t;
}
/*
Sample run:
Enter elements,-1 to stop: 7
1
6
2
5
3
5
4
9
11
-1
Before sorting:
7 1 6 2 5 3 5 4 9 11
After sorting:
1 2 3 4 5 5 6 7 9 11
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.