Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- using namespace std;
- template <class T>
- void my_swap(T& a, T& b) {
- //cout << "IN FUN: " << a << b << endl;
- T tmp = b;
- b = a;
- a = tmp;
- //cout << "IN FUN: " << a << b << endl;
- }
- template <class T>
- void show_arr(int size, T arr[]) {
- for (int k = 0; k < size; k++)
- cout << arr[k] << " ";
- cout << endl;
- }
- template <class T>
- int find(int size, T arr[], T ell) {
- for (int k = 0; k < size; k++) {
- if (arr[k] == ell)
- return k;
- }
- return -1;
- }
- void discrete_sort(int size, int arr[]) {
- // элементы массива могут быть { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
- int frequency[10]{};
- for (int k = 0; k < size; k++) {
- frequency[arr[k]]++;
- }
- int m = 0;
- for (int k = 0; k < 10; k++) {
- for (int n = 0; n < frequency[k]; n++) {
- arr[m] = k;
- m++;
- }
- }
- show_arr(10, frequency);
- }
- template <class T>
- void bubble_sort(int size, T arr[]) {
- bool is_sorted;
- for (int k = 0; k < size; k++) {
- is_sorted = true;
- for (int n = 1; n < size; n++)
- if (arr[n] < arr[n - 1]) {
- my_swap(arr[n], arr[n - 1]);
- is_sorted = false;
- //show_arr(size, arr);
- }
- if (is_sorted)
- break;
- }
- }
- void select_sort(int size, int arr[]) {
- int min_id;
- for (int n = 0; n < size - 1; n++) {
- min_id = n;
- for (int k = n + 1; k < size; k++) {
- if (arr[k] < arr[min_id]) {
- min_id = k;
- }
- }
- my_swap(arr[n], arr[min_id]);
- show_arr(size, arr);
- }
- }
- void insert_sort(int size, int arr[]) {
- int n = 0;
- for (int k = 1; k < size; k++) {
- n = k;
- while ((n > 0) && (arr[n] < arr[n - 1])) {
- my_swap(arr[n], arr[n - 1]);
- n--;
- //show_arr(size, arr);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement