update
This commit is contained in:
parent
5911856dc6
commit
29ee27dbab
2 changed files with 35 additions and 59 deletions
33
src/io.h
33
src/io.h
|
@ -1,8 +1,37 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <iostream> // for std::cout
|
||||||
void add_two_numbers();
|
void add_two_numbers();
|
||||||
int read_number();
|
int read_number();
|
||||||
void write_answer(int x);
|
void write_answer(int x);
|
||||||
void convert_char_to_ascii();
|
void convert_char_to_ascii();
|
||||||
void print_type_size_bytes();
|
void print_type_size_bytes();
|
||||||
void bubble_sort(int *a);
|
|
||||||
|
template <class T>
|
||||||
|
void bubble_sort(T *a)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < sizeof(&a) / sizeof(a); i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < 7 - 1 - i; j++)
|
||||||
|
{
|
||||||
|
if (a[j] > a[j + 1])
|
||||||
|
{
|
||||||
|
T temp{a[j]};
|
||||||
|
a[j] = a[j + 1];
|
||||||
|
a[j + 1] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
template <typename T>
|
||||||
|
void bubble_sort_print(int size, T *a)
|
||||||
|
{
|
||||||
|
std::cout << "unsorted Array: ";
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
std::cout << a[i] << ", ";
|
||||||
|
std::cout << "\n";
|
||||||
|
bubble_sort(a);
|
||||||
|
std::cout << "sorted Array: ";
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
std::cout << a[i] << ", ";
|
||||||
|
std::cout << "\n";
|
||||||
|
}
|
61
src/main.cpp
61
src/main.cpp
|
@ -1,69 +1,16 @@
|
||||||
#include <iostream> // for std::cout
|
#include <iostream> // for std::cout
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
|
||||||
#define ARRAY_SIZE 7
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
void BubbleSort_T(T *a)
|
|
||||||
{
|
|
||||||
for (size_t i = 0; i < 7; i++)
|
|
||||||
{
|
|
||||||
for (size_t j = 0; j < 7 - 1 - i; j++)
|
|
||||||
{
|
|
||||||
if (a[j] > a[j + 1])
|
|
||||||
{
|
|
||||||
T temp{a[j]};
|
|
||||||
a[j] = a[j + 1];
|
|
||||||
a[j + 1] = temp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void bubble_sort_demo()
|
|
||||||
{
|
|
||||||
int demo[ARRAY_SIZE]{11, 14, 3, 18, 8, 17, 43};
|
|
||||||
for (int i = 0; i < 7; i++)
|
|
||||||
std::cout << demo[i] << ", ";
|
|
||||||
std::cout << "\n";
|
|
||||||
bubble_sort(demo);
|
|
||||||
for (int i = 0; i < ARRAY_SIZE; i++)
|
|
||||||
std::cout << demo[i] << ", ";
|
|
||||||
}
|
|
||||||
|
|
||||||
void bubble_sort_T_demo()
|
|
||||||
{
|
|
||||||
int demo[ARRAY_SIZE]{11, 14, 3, 18, 8, 17, 43};
|
|
||||||
double demo2[ARRAY_SIZE]{11.2, 14.3, 3.2, 18.6, 8.7, 17.9, 43.3};
|
|
||||||
std::cout << "Demo: ";
|
|
||||||
for (int i = 0; i < 7; i++)
|
|
||||||
std::cout << demo[i] << ", ";
|
|
||||||
std::cout << "\n";
|
|
||||||
std::cout << "Demo2: ";
|
|
||||||
|
|
||||||
for (int i = 0; i < 7; i++)
|
|
||||||
std::cout << demo2[i] << ", ";
|
|
||||||
std::cout << "\n";
|
|
||||||
|
|
||||||
BubbleSort_T(demo);
|
|
||||||
BubbleSort_T(demo2);
|
|
||||||
std::cout << "Demo: ";
|
|
||||||
for (int i = 0; i < 7; i++)
|
|
||||||
std::cout << demo[i] << ", ";
|
|
||||||
std::cout << "\n";
|
|
||||||
std::cout << "Demo2: ";
|
|
||||||
|
|
||||||
for (int i = 0; i < 7; i++)
|
|
||||||
std::cout << demo2[i] << ", ";
|
|
||||||
std::cout << "\n";
|
|
||||||
}
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
int demo[]{11, 14, 3, 18, 8, 17, 43, 6, 5, 4};
|
||||||
|
double demo2[]{11.2, 14.3, 3.2, 18.6, 8.7, 17.9, 43.3};
|
||||||
// add_two_numbers();
|
// add_two_numbers();
|
||||||
// convert_char_to_ascii();
|
// convert_char_to_ascii();
|
||||||
print_type_size_bytes();
|
print_type_size_bytes();
|
||||||
|
|
||||||
bubble_sort_T_demo();
|
bubble_sort_print(sizeof(demo) / sizeof(demo[0]), demo);
|
||||||
|
bubble_sort_print(sizeof(demo2) / sizeof(demo2[0]), demo2);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue