sort work
This commit is contained in:
parent
27534a8198
commit
45c341d78d
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <utility>
|
#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];
|
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].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)
|
if (len % 2)
|
||||||
pairs[len - 1].first = pairs[len - 1].second;
|
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>
|
template <typename T>
|
||||||
void binary_search(std::pair<int, int>* pairs, size_t len, T& array)
|
size_t binary_search(const T& array, size_t start, size_t end, int value) {
|
||||||
{
|
if (array[end] < value)
|
||||||
(void) pairs;
|
return end;
|
||||||
(void) array;
|
|
||||||
std::vector<int> bozo;
|
|
||||||
for (size_t i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
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);
|
std::pair<int, int>* pairs = create_pairs(array, len);
|
||||||
sort_pair(pairs, len);
|
sort_pair(pairs, len);
|
||||||
sort_pairs(pairs, len);
|
sort_pairs(pairs, len);
|
||||||
std::cout << pairs;
|
|
||||||
array.clear();
|
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;
|
||||||
}
|
}
|
@ -1,4 +1,5 @@
|
|||||||
#include "PmergeMe.hpp"
|
#include "PmergeMe.hpp"
|
||||||
|
#include <cstddef>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -15,10 +16,9 @@ int main(int ac, char** av)
|
|||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
PmergeMe(input);
|
PmergeMe(input);
|
||||||
std::cout << "After: ";
|
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 << input[i] << " ";
|
||||||
std::cout << av[i] << " ";
|
|
||||||
}
|
}
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user