Question #35309

a C++ program to find ascending order for given numbers using class

Expert's answer

class Sorter

{

private:

static void Swap(int& a, int& b)

{

int tmp = a;

a = b;

b = tmp;

}

public:

static void BubbleSortAscending(int* array, int size)

{

if (!array || size <= 1)

return;

for (int g = 0; g < size - 1; g++)

for (int q = 1; q < size - g; q++)

if (array[q - 1] > array[q])

Swap(array[q - 1], array[q]);

}

};

#include <iostream>

#include <list>

using namespace std;

void print_array(int* array, int size)

{

for (int z = 0; z < size; z++)

cout << " " << array[z];

cout << endl;

}

int main()

{

list<int> numbers;

cout << "Enter some numbers (type any letter to end the sequence):" << endl << " ";

int tmp;

while (cin >> tmp)

numbers.push_back(tmp);

cout << endl;

int arraySize = numbers.size();

int* array = new int[arraySize];

copy(numbers.begin(), numbers.end(), array);

Sorter::BubbleSortAscending(array, arraySize);

cout << "The numbers entered in ascending order:" << endl;

print_array(array, arraySize);

return 0;

}

{

private:

static void Swap(int& a, int& b)

{

int tmp = a;

a = b;

b = tmp;

}

public:

static void BubbleSortAscending(int* array, int size)

{

if (!array || size <= 1)

return;

for (int g = 0; g < size - 1; g++)

for (int q = 1; q < size - g; q++)

if (array[q - 1] > array[q])

Swap(array[q - 1], array[q]);

}

};

#include <iostream>

#include <list>

using namespace std;

void print_array(int* array, int size)

{

for (int z = 0; z < size; z++)

cout << " " << array[z];

cout << endl;

}

int main()

{

list<int> numbers;

cout << "Enter some numbers (type any letter to end the sequence):" << endl << " ";

int tmp;

while (cin >> tmp)

numbers.push_back(tmp);

cout << endl;

int arraySize = numbers.size();

int* array = new int[arraySize];

copy(numbers.begin(), numbers.end(), array);

Sorter::BubbleSortAscending(array, arraySize);

cout << "The numbers entered in ascending order:" << endl;

print_array(array, arraySize);

return 0;

}

## Comments

## Leave a comment