#include <iostream>
#include <vector>
using namespace std;
class bubblesort{
private:
int a[100];
int n;
public:
bubblesort(int arr[], int size){
n = size;
for (int i=0; i < n; i++) {
a[i] = arr[i];
}
for (int i=0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j+1]) {
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
void display() {
cout << "Sorted Array: ";
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
}
};
int main() {
int size;
cout << "Enter the size of the 1-d array: ";
cin >> size;
int arr[size];
cout << "Enter all the elements separated with spaces\n";
for(int i=0; i<size; i++){
cin>>arr[i];
}
bubblesort bs(arr, size);
bs.display();
return 0;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter