added Bubblesort
This commit is contained in:
parent
79644bad85
commit
7f0548cd69
3 changed files with 32 additions and 2 deletions
15
src/io.cpp
15
src/io.cpp
|
@ -37,4 +37,19 @@ void print_type_size_bytes()
|
||||||
std::cout << std::setw(16) << "float:" << sizeof(float) << " bytes\n";
|
std::cout << std::setw(16) << "float:" << sizeof(float) << " bytes\n";
|
||||||
std::cout << std::setw(16) << "double:" << sizeof(double) << " bytes\n";
|
std::cout << std::setw(16) << "double:" << sizeof(double) << " bytes\n";
|
||||||
std::cout << std::setw(16) << "long double:" << sizeof(long double) << " bytes\n";
|
std::cout << std::setw(16) << "long double:" << sizeof(long double) << " bytes\n";
|
||||||
|
}
|
||||||
|
void bubble_sort(int *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])
|
||||||
|
{
|
||||||
|
int temp{a[j]};
|
||||||
|
a[j] = a[j + 1];
|
||||||
|
a[j + 1] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
3
src/io.h
3
src/io.h
|
@ -4,4 +4,5 @@ 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);
|
16
src/main.cpp
16
src/main.cpp
|
@ -1,12 +1,26 @@
|
||||||
#include <iostream> // for std::cout
|
#include <iostream> // for std::cout
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
|
||||||
|
#define ARRAY_SIZE 7
|
||||||
|
|
||||||
// Definition of function main()
|
// Definition of function main()
|
||||||
|
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] << ", ";
|
||||||
|
}
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// add_two_numbers();
|
// add_two_numbers();
|
||||||
// convert_char_to_ascii();
|
// convert_char_to_ascii();
|
||||||
print_type_size_bytes();
|
print_type_size_bytes();
|
||||||
|
|
||||||
|
bubble_sort_demo();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue