Created
April 27, 2025 17:55
-
-
Save Filimon4/bb654ba90c5b5b02830bc015e1e3f055 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <fstream> | |
#include <string> | |
std::vector<int> bubbleSort(std::vector<int> list) { | |
int listSize = list.size()-1; | |
if (listSize <= 1) return list; | |
for (int i = 0; i < listSize; i++) { | |
int swapped = 0; | |
for (int j = 0; j < listSize; j++) { | |
if (list[j] > list[j + 1]) { | |
int tmpInt = list[j]; | |
list[j] = list[j + 1]; | |
list[j + 1] = tmpInt; | |
swapped = 1; | |
} | |
} | |
if (swapped == 0) break; | |
} | |
return list; | |
} | |
//сортировка выбором | |
std::vector<int> selectSort(std::vector<int> list) { | |
int listSize = list.size(); | |
if (listSize <= 1) return list; | |
for (int i = 0; i < listSize; i++) { | |
int currValue = list[i]; | |
int maxIndx = i; | |
for (int j = i; j < listSize; j++) { | |
if (currValue > list[j]) { | |
maxIndx = j; | |
} | |
} | |
int tmpValue = list[maxIndx]; | |
list[maxIndx] = list[i]; | |
list[i] = tmpValue; | |
} | |
return list; | |
} | |
// сортировка вставкой | |
std::vector<int> insertSort(std::vector<int> list) { | |
int listSize = list.size(); | |
if (listSize <= 1) return list; | |
for (int i = 1; i < listSize; i++) { | |
int currIndx = i; | |
for (int j = i - 1; j >= 0; j--) { | |
if (list[j] > currIndx) { | |
int tmpValue = list[j]; | |
list[j] = list[currIndx]; | |
list[currIndx] = tmpValue; | |
} | |
} | |
} | |
return list; | |
} | |
// quick sort | |
int partition(std::vector<int>& list, int start, int pivot) | |
{ | |
int i = start; | |
while (i < pivot) | |
{ | |
if (list[i] > list[pivot] && i == pivot - 1) | |
{ | |
int temp = list[i]; | |
list[i] = list[pivot]; | |
list[pivot] = temp; | |
pivot--; | |
} | |
else if (list[i] > list[pivot]) | |
{ | |
int temp1 = list[pivot - 1]; | |
list[pivot - 1] = list[pivot]; | |
list[pivot] = temp1; | |
int temp2 = list[i]; | |
list[i] = list[pivot]; | |
list[pivot] = temp2; | |
pivot--; | |
} | |
else | |
{ | |
i++; | |
} | |
} | |
return pivot; | |
} | |
void quickSort(std::vector<int>& list, int start, int end) | |
{ | |
if (start < end) | |
{ | |
int pivot = partition(list, start, end); | |
quickSort(list, start, pivot - 1); | |
quickSort(list, pivot + 1, end); | |
} | |
} | |
// сортировка слиянием | |
void merge(std::vector<int>& list, int start, int mid, int end) { | |
int length = end - start + 1; | |
if (length == 1) return; | |
if (length == 2) { | |
if (list[start] > list[end]) { | |
std::swap(list[start], list[end]); | |
} | |
return; | |
} | |
int* newList = new int[length]; | |
int i = start; | |
int j = mid + 1; | |
int k = 0; | |
while (i <= mid && j <= end) { | |
if (list[i] > list[j]) { | |
newList[k++] = list[i++]; | |
} | |
else { | |
newList[k++] = list[j++]; | |
} | |
} | |
while (i <= mid) { | |
newList[k++] = list[i++]; | |
} | |
while (j <= end) { | |
newList[k++] = list[j++]; | |
} | |
for (int v = 0; v < length; v++) { | |
list[start + v] = newList[v]; | |
} | |
delete[] newList; | |
} | |
void mergeSort(std::vector<int>& list, int start, int end) { | |
if (start < end) { | |
int mid = (start + end) / 2; | |
mergeSort(list, start, mid); | |
mergeSort(list, mid+1, end); | |
merge(list, start, mid, end); | |
} | |
} | |
// сортировки Шелла | |
void insertionSortWithGap(std::vector<int>& arr, int size, int gap) { | |
for (int i = gap; i < size; ++i) { | |
int current = arr[i]; | |
int j = i; | |
while (j >= gap && arr[j - gap] > current) { | |
arr[j] = arr[j - gap]; | |
j -= gap; | |
} | |
arr[j] = current; | |
} | |
} | |
void shellSort(std::vector<int>& arr) { | |
int size = arr.size(); | |
for (int gap = size / 2; gap > 0; gap /= 2) { | |
insertionSortWithGap(arr, size, gap); | |
} | |
} | |
void iotest() { | |
const std::string fileName = "test.txt"; | |
std::ofstream outputfile(fileName); // , std::ios::app в конец, без отчиски | |
if (outputfile.is_open()) { | |
outputfile << "Hello my name is EFIM\n"; | |
outputfile.close(); | |
} | |
std::ifstream inputfile(fileName); | |
if (inputfile.is_open()) { | |
for (std::string line; std::getline(inputfile, line); ) { | |
std::cout << line << std::endl; | |
} | |
} | |
} | |
// overload example | |
void print(int num, int num2) { | |
std::cout << "2 numbers: " << num << num2 << std::endl; | |
} | |
void print(int number) { | |
std::cout << "number: " << number << std::endl; | |
} | |
void print(std::string str) { | |
std::cout << "string: " << str << std::endl; | |
} | |
// template example | |
template <typename T> | |
void print(const T& value) { | |
std::cout << value << std::endl; | |
} | |
// Specialization for arrays | |
template <typename T, std::size_t N> | |
void print(const T(&array)[N]) { | |
for (std::size_t i = 0; i < N; ++i) { | |
std::cout << array[i] << " "; | |
} | |
std::cout << std::endl; | |
} | |
// Specialization for vectors | |
template <typename T> | |
void print(const std::vector<T>& vec) { | |
for (const auto& elem : vec) { | |
std::cout << elem << " "; | |
} | |
std::cout << std::endl; | |
} | |
int main() | |
{ | |
/*std::vector<int> list = { 20,10,3,234,6,7,3,2,4563,345,234,3,64,56 }; | |
shellSort(list); | |
for (int i = 0; i< list.size(); i++) { | |
std::cout << list[i] << std::endl; | |
}*/ | |
int list[] = {1,2,2,3}; | |
print<int, 4>(list); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment