Интерпретация в консоли смены элементов при пузырьковой сортировке
написал код пузырьковой сортировки на C++.Помогите,пожалуйста,сделать так,чтобы в консоли отображалась смена элементов при каждой итерации цикла(еще чтобы элементы,которые меняются местами , были подсвечены каким-либо цветом).Спасибо.
#include <iostream>
#include <vector>
#include "windows.h"
using namespace std;
int main() {
int n;
cin >> n;
vector<int> k;
for(int i = 0;i < n;i++) {
int temp;
cin >> temp;
k.push_back(temp);
}
for(int i = 0;i < n;i++) {
for(int j = 0;j < n - 1;j++) {
if(k[j] > k[j + 1]) {
swap(k[j],k[j+1]);
}
}
}
for(auto s : k) {
cout << s << " ";
}
return 0;
}
Ответы (1 шт):
Автор решения: Easton
→ Ссылка
#include <iostream>
#include <iomanip>
#include <string>
#include <Windows.h>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::fixed;
using std::setw;
#define on , // So I can use the function - void text(text_color on background_color)
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
// My text color function. Use it if you wish.
void text(int text_color = 0 on int paper_color = 7)
{
// defaults to black on light_gray
const int color_total = (text_color + (paper_color * 16));
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}
enum Colors
{
black,
// 0 text color - multiply by 16, for background colors
dark_blue,
// 1
dark_green,
// 2
dark_cyan,
// 3
dark_red,
// 4
dark_magenta,
// 5
dark_yellow,
// 6
light_gray,
// 7
dark_gray,
// 8
light_blue,
// 9
light_green,
// 10
light_cyan,
// 11
light_red,
// 12
light_magenta,
// 13
light_yellow,
// 14
white // 15
};
void sort_array_ascending(int* array, int size);
void print_array(int* array, int* old_array, int);
void print_unsorted_array(int* array, int size);
int main()
{
text(light_green on black);
const string title = "Array Sorting Program";
const string hyphen(100, '-');
constexpr int size = 10;
int values[size] = {39, 19, 74, 82, 80, 95, 94, 48, 29, 8};
cout << hyphen << endl;
cout << " " << title << endl;
cout << hyphen << endl;
cout << "\n This program will sort two identical arrays of numbers using a Bubble Sort" << endl;
cout << "\n Array 1 -- Ascending Order: \n" << endl;
print_unsorted_array(values, size);
cout << endl;
sort_array_ascending(values, size);
text(light_green on black);
cout << "\n\n\n\n\t\t\tPress only the 'Enter' key to exit program: ";
cin.ignore(cin.rdbuf()->in_avail());
cin.get();
}
void sort_array_ascending(int* array, int size)
{
constexpr int swapped_color = 4;
int old_array[8];
bool swapTookPlace;
int pass = 0;
do
{
swapTookPlace = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
constexpr int non_swapped_color = 2;
for (int a = 0; a < size; a++)
{
old_array[a] = array[a];
}
swapTookPlace = true;
text(non_swapped_color on black);;
const int temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
cout << fixed << setw(2) << " Pass # " << (pass < 9 ? " " : " ") << (pass + 1) << " : ";
pass += 1;
print_array(&array[0], old_array, size);
}
}
}
while (swapTookPlace);
text(swapped_color on black);
}
void print_array(int* array, int* old_array, int size)
{
for (int count = 0; count < size; ++count)
{
if (array[count] != old_array[count])
{
constexpr int swapped_color = 12;
text(swapped_color on black);
}
else
{
constexpr int non_swapped_color = 10;
text(non_swapped_color on black);
}
cout << " " << array[count] << " ";
}
cout << endl;
}
void print_unsorted_array(int* array, int size)
{
cout << " Unsorted ";
for (int count = 0; count < size; ++count)
{
cout << " " << array[count] << " ";
}
cout << endl;
cout << "--------------------------------------------------------------------------";
}