sort work

This commit is contained in:
starnakin 2023-12-13 17:03:59 +01:00
parent 27534a8198
commit 45c341d78d
2 changed files with 43 additions and 14 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <utility>
@ -13,10 +14,11 @@ std::pair<int, int>* create_pairs(const T& array, size_t len)
{
std::pair<int, int>* pairs = new std::pair<int, int>[len];
for (size_t i = 0; i != len; i++)
for (size_t i = 0; i < len; i++)
{
pairs[i].second = array[i * 2];
pairs[i].first = array[i * 2 + 1];
if (array.size() > i * 2 + 1)
pairs[i].first = array[i * 2 + 1];
}
if (len % 2)
pairs[len - 1].first = pairs[len - 1].second;
@ -24,14 +26,38 @@ std::pair<int, int>* create_pairs(const T& array, size_t len)
}
template <typename T>
void binary_search(std::pair<int, int>* pairs, size_t len, T& array)
{
(void) pairs;
(void) array;
std::vector<int> bozo;
for (size_t i = 0; i < len; i++)
{
size_t binary_search(const T& array, size_t start, size_t end, int value) {
if (array[end] < value)
return end;
while (true)
{
int mid = start + ((end - start) / 2);
int min_value = array[mid];
if (min_value == value || end - start <= 1)
{
if (min_value < value)
return mid + 1;
return mid;
}
else if (min_value < value)
start = mid;
else if (min_value > value)
end = mid;
}
}
template <typename T>
void insert_sort(const std::pair<int, int>* pairs, size_t len, T& array)
{
for (size_t i = 1; i < len; i++)
{
array.push_back(pairs[i].first);
if (pairs[i].first == pairs[i].second)
continue;
ssize_t index = binary_search(array, 0, array.size() - 1, pairs[i].second);
array.insert(array.begin() + index, pairs[i].second);
}
}
@ -42,7 +68,10 @@ void PmergeMe(T& array)
std::pair<int, int>* pairs = create_pairs(array, len);
sort_pair(pairs, len);
sort_pairs(pairs, len);
std::cout << pairs;
array.clear();
binary_search(pairs, len, array);
array.push_back(pairs[0].second);
if (pairs[0].second != pairs[0].first)
array.push_back(pairs[0].first);
insert_sort(pairs, len, array);
delete[] pairs;
}

View File

@ -1,4 +1,5 @@
#include "PmergeMe.hpp"
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <vector>
@ -15,10 +16,9 @@ int main(int ac, char** av)
std::cout << std::endl;
PmergeMe(input);
std::cout << "After: ";
for (int i = 1; i != ac; i++)
for (size_t i = 0; i != input.size(); i++)
{
input[i - 1] = atoi(av[i]);
std::cout << av[i] << " ";
std::cout << input[i] << " ";
}
std::cout << std::endl;
}