-
October 17th, 2016, 10:28 PM
#1
Bubble sort, Selection Sort, Insertion Sort
mkir100 (1)
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:
Code:
#include <iostream>
using namespace std;
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_int();
element Read_element();
public:
void Read();
void Print();
void BubbleSort();
void InsertionSort();
void SelectionSort();
};
int main() {
AList B;
B.Read();
B.Print();
B.BubbleSort();
B.InsertionSort();
B.SelectionSort();
}
void AList::Read() {
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() {
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() {
int j;
bool done;
for (int i=1; i<size; size++)
j=i;
done = false;
while ((j>=1) && (!done))
if (items[j] < items[j-1]) {
Swap (j,j-1);
j--;
}
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);
}
}
Edit & Run
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 by 2kaud; October 18th, 2016 at 01:29 AM.
Reason: Added code tags
-
October 18th, 2016, 06:33 AM
#2
Re: Bubble sort, Selection Sort, Insertion Sort
I don't think that I'm doing it in the order that needs to be done
Yep. What you're doing is reading the numbers, displaying them and them sorting them in various ways and not displaying the results of any of the sorts.
For main(), consider
Code:
int main() {
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();
}
This matches the method calls in post #1.
Note that you have an issue with the member variable size. Where is this being initialised?
Last edited by 2kaud; October 18th, 2016 at 06:35 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 18th, 2016, 12:33 PM
#3
Re: Bubble sort, Selection Sort, Insertion Sort
Ok. I'm trying to fix the issue with the variable size.
-
October 21st, 2016, 06:05 AM
#4
Re: Bubble sort, Selection Sort, Insertion Sort
I've changed my code a little bit and I'm no longer having any compile issues. However, I can't get the program to run right now.
Code:
#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;
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);
}
}
Last edited by 2kaud; October 21st, 2016 at 08:07 AM.
Reason: Added code tags
-
October 21st, 2016, 08:08 AM
#5
Re: Bubble sort, Selection Sort, Insertion Sort
Code:
cout<<"Enter elements,";
while ((userval!=SENTINEL)&&(size<MLS)) {
items[size] = userval;
size ++;
}
Where do you actually obtain an element value?. Don't you need a call to Read_Element()?
Also
Code:
while (!cin.good());
Is not really a good idea as this will loop forever if the state of the cin stream is not good.
Code:
for (int i=0; size=-1; i++) {
I don't think you mean this?
If the else clause of an if statement is not needed, rather than just having a ; the else can be omitted.
Code:
if (items[j] > items[maxpos])
maxpos = j;
else
;
becomes
Code:
if (items[j] > items[maxpos])
maxpos = j;
Last edited by 2kaud; October 21st, 2016 at 08:12 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 21st, 2016, 09:32 AM
#6
Re: Bubble sort, Selection Sort, Insertion Sort
Please explain. I'm not sure I understand.
-
October 21st, 2016, 09:42 AM
#7
Re: Bubble sort, Selection Sort, Insertion Sort
Originally Posted by amkir100
Please explain. I'm not sure I understand.
Which don't you understand?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 21st, 2016, 10:22 AM
#8
Re: Bubble sort, Selection Sort, Insertion Sort
Code:
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;
I thought that I had called out Read_element in this statement. I was trying to make it so once the program did run, the only thing that could be entered is a whole number.
Code:
public:
void Read_element();
void Read();
void Print();
void BubbleSort();
void InsertionSort();
void SelectionSort();
Is that what you mean? I guess that I just need it broken down more.
Last edited by amkir100; October 21st, 2016 at 10:26 AM.
-
October 21st, 2016, 12:35 PM
#9
Re: Bubble sort, Selection Sort, Insertion Sort
Read_element() as you say is trying to ensure that only a whole number could be entered. But this function has to be called to obtain a number.
Code:
public:
void Read_element();
void Read();
What these are are class function declarations. This doesn't actually call the function. Read_element() declared here is not the same Read_element() as defined elsewhere - as the one defined is not a class function.
From post #5, Read_element() is declared as
Code:
private:
element items[MLS];
int size;
void Swap(int pos1, int pos2);
element Read_element();
so the definition should be scoped for the class
Code:
element AList::Read_element()
Also Read_element() needs to deal properly with invalid integer input. Consider something like
Code:
element AList::Read_element()
{
element userval;
while (!(cin >> userval)) {
cout << "invalid choice please enter a whole number" << endl;
cin.clear();
cin.ignore(80, '\n');
}
return userval;
}
Within Read() you need to call Read_element() to obtain a number. Something like
Code:
void AList::Read()
{
element userval;
cout << "Enter elements,";
for (size = 0; ((userval = Read_element()) != SENTINEL) && (size < MLS); items[size++] = userval);
}
Once you have fixed the bubble sort() issue highlighted in post #5, you should find that the program then works as expected.
PS You don't need to code your own swap function. You can use the c++ swap(), You need to include algorithm. See http://www.cplusplus.com/reference/algorithm/swap/
Last edited by 2kaud; October 21st, 2016 at 12:46 PM.
Reason: PS
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|