
C/C++
Помогите пожалуйста решить задачу по с++

#include <iostream>
#include <span>
using namespace std;
struct BaseSort {
virtual void sort(int* box, const size_t len) = 0;
};
struct BubbleSort : virtual public BaseSort {
void sort(int* box, const size_t n) override {
auto first = box;
auto last = box + n;
while (first < --last) {
for (auto begin = first; begin < last; ++begin) {
if (*(begin + 1) < *begin) {
swap(*begin, *(begin + 1));
}
}
}
}
};
struct SelectionSort : virtual public BaseSort {
void sort(int* box, const size_t n) override {
for (int i = 0; i < n - 1; ++i) {
int m = i;
for (int j = i + 1; j < n; j++) {
if (box[j] < box[m]) {
m = j;
}
}
swap(box[i], box[m]);
}
}
};
struct InsertionSort : virtual public BaseSort {
void sort(int* box, const size_t n) override {
auto begin = box;
auto end = box + n;
if (!(begin < end)) return;
for (auto current = begin + 1; current != end; ++current) {
for (auto prev = current; prev != begin && *prev < *(prev - 1); --prev) {
iter_swap(prev - 1, prev);
}
}
}
};
void show(span<int> box) {
for (auto x : box) cout << x << ' ';
puts("");
}
int main() {
int a[]{ 3, 5, 2, 4, 6, 9, 7, 8, 1 };
show(a);
BubbleSort bs;
bs.sort(a, size(a));
show(a);
puts("");
int c[]{ 3, 5, 2, 4, 6, 9, 7, 8, 1 };
show(c);
InsertionSort is;
is.sort(c, size(c));
show(c);
puts("");
int b[]{ 3, 5, 2, 4, 6, 9, 7, 8, 1 };
show(b);
SelectionSort ss;
ss.sort(b, size(b));
show(b);
system("pause > nul");
}
#include <span>
using namespace std;
struct BaseSort {
virtual void sort(int* box, const size_t len) = 0;
};
struct BubbleSort : virtual public BaseSort {
void sort(int* box, const size_t n) override {
auto first = box;
auto last = box + n;
while (first < --last) {
for (auto begin = first; begin < last; ++begin) {
if (*(begin + 1) < *begin) {
swap(*begin, *(begin + 1));
}
}
}
}
};
struct SelectionSort : virtual public BaseSort {
void sort(int* box, const size_t n) override {
for (int i = 0; i < n - 1; ++i) {
int m = i;
for (int j = i + 1; j < n; j++) {
if (box[j] < box[m]) {
m = j;
}
}
swap(box[i], box[m]);
}
}
};
struct InsertionSort : virtual public BaseSort {
void sort(int* box, const size_t n) override {
auto begin = box;
auto end = box + n;
if (!(begin < end)) return;
for (auto current = begin + 1; current != end; ++current) {
for (auto prev = current; prev != begin && *prev < *(prev - 1); --prev) {
iter_swap(prev - 1, prev);
}
}
}
};
void show(span<int> box) {
for (auto x : box) cout << x << ' ';
puts("");
}
int main() {
int a[]{ 3, 5, 2, 4, 6, 9, 7, 8, 1 };
show(a);
BubbleSort bs;
bs.sort(a, size(a));
show(a);
puts("");
int c[]{ 3, 5, 2, 4, 6, 9, 7, 8, 1 };
show(c);
InsertionSort is;
is.sort(c, size(c));
show(c);
puts("");
int b[]{ 3, 5, 2, 4, 6, 9, 7, 8, 1 };
show(b);
SelectionSort ss;
ss.sort(b, size(b));
show(b);
system("pause > nul");
}
Нурлан Еламанов
Ооо, огромное вам спасибо вы не представляете как вы мне помогли.
Есть примерно полтора десятка разных алгоритмов сортировки, не считая разновидностей каждого способа. Что, все писать? :))
Вот самый простой:
#include "iostream"
#include "algorithm"
using namespace std;
int main(){
int a[]={5,3,7,2,5,6}; for(int &i:a)cout<<i<<' '; cout<< endl;
sort(a,a+sizeof a/sizeof *a); for(int &i:a)cout<<i<<' '; cout<< endl;
reverse(a,a+sizeof a/sizeof *a); for(int &i:a)cout<<i<<' '; cout<< endl;}
Вот самый простой:
#include "iostream"
#include "algorithm"
using namespace std;
int main(){
int a[]={5,3,7,2,5,6}; for(int &i:a)cout<<i<<' '; cout<< endl;
sort(a,a+sizeof a/sizeof *a); for(int &i:a)cout<<i<<' '; cout<< endl;
reverse(a,a+sizeof a/sizeof *a); for(int &i:a)cout<<i<<' '; cout<< endl;}
Нурлан Еламанов
Я дополнила, спасибо за замечание.
Нурлан Еламанов
Это какой метод?
Сурет Мусаев
Это встроенный в C++ алгоритм сортировки Хаара.
Сурет Мусаев
А вот дополнение следовало написать сразу. Нужные программы ты легко найдешь в Интернете, я их искать и копировать не буду.
Похожие вопросы
- Помогите пожалуйста решить задачу на языке С#.
- Помогите пожалуйста решить задачу на Си
- Программирование С++. Помогите, пожалуйста, решить задачу.
- Помогите, пожалуйста, решить задачу.
- Помогите пожалуйста решить задачу на языке СИ или С# !!!
- Помогите пожалуйста решить задачу на C ИЛИ C#
- Помогите пожалуйста решить задачу на С#
- Помогите пожалуйста решить задачу
- Помогите, пожалуйста, решить задачу C++.
- Помогите пожалуйста решить задачу по С++