//Author: Frank R. Mendez
//Title: Object Oriented Bubble Sort
//Description: Accepts an int array element to sort using bubblesort in ascending order
#include <iostream>
using namespace std;
class BubbleSort {
public:
void bubble(int arr[], int size);
void driver();
void display();
int arr[50];
int array_size;
int temp;
};
void BubbleSort::driver() {
cout<<"Enter size of list:" << endl;
cin>>array_size;
cout<<"Enter " << array_size << " values" << endl;
cout << endl;
for(int b = 0; b <= array_size -1; b++) {
cout << "Value [" << b << "]:";
cin>>arr[b];
}
cout << "Elements of your unsorted list: [ ";
for(int c = 0; c <= array_size -1; c++){
cout << arr[c] << " " ;
}
cout << "]" << endl;
display();
}
void BubbleSort::bubble(int arr[], int array_size) {
for (int k = 0; k < array_size; k++)
for (int i = 0; i < array_size -k -1; i++)
if (arr[i] > arr[i +1]){
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
void BubbleSort::display() {
bubble(arr,array_size);
cout << "Elements of your sorted list: [ ";
for(int x = 0; x < array_size; x++) {
cout << arr[x] << " ";
}
cout << "]";
}
int main () {
BubbleSort frank;
frank.driver();
return 0;
}
Frank_5
0
Newbie Poster
Recommended Answers
Jump to PostIgnoring for the moment that this is a horrible example of object orientation and brings into question whether you're qualified to be giving out examples as if you know what you're doing, what exactly is the benefit of an object oriented bubble sort here?
All 3 Replies
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
rubberman
commented:
Succinctly put D. :-)
+12
ithinkunome
0
Newbie Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
NathanOliver
commented:
I like the warning
+11
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.