NTU-OOP-Tuts-2024-Cpp/T8Q1-1/main.cpp

39 lines
1008 B
C++
Raw Normal View History

2024-10-30 07:36:34 +08:00
//
// Created by Marcus Vinícius de Carvalho.
//
#include "main.h"
// Learn about const class objects and member functions here: https://www.learncpp.com/cpp-tutorial/const-class-objects-and-const-member-functions/
void BubbleSort::sort() const {
for (int i = _size - 2; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
if (_numArray[j] > _numArray[j + 1]) {
const int t = _numArray[j];
_numArray[j] = _numArray[j + 1];
_numArray[j + 1] = t;
}
}
}
};
int main(int argc, const char * argv[]) {
int a[100], n, i;
std::cout << "Enter number of Integers elements to be sorted: ";
std::cin >> n;
for (i = 0; i < n; i++) {
std::cout << "Enter integer value for elements no." << i + 1 << ": ";
std::cin >> a[i];
}
const BubbleSort b(a, n);
b.sort();
std::cout << "\nFinally sorted array is: ";
for (i = 0; i < n; i++) {
std::cout << a[i] << " ";
}
}