Skip to content

Commit

Permalink
Insertion and Tail Call
Browse files Browse the repository at this point in the history
  • Loading branch information
arkiny committed Sep 3, 2018
1 parent a3f439d commit f6f0c6b
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 101 deletions.
135 changes: 97 additions & 38 deletions dclb/Sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,48 @@ namespace dc
}

template<class RandomIt, class Compare>
constexpr auto partition(RandomIt lo, RandomIt hi, Compare comp)
constexpr void insertionSort(RandomIt arr, size_t low, size_t n, Compare comp)
{
if (lo == hi) return lo;

auto pivot = *hi;
auto i = lo;

for (auto j = lo; j < hi; j++)
for (size_t i = low + 1; i <= n; i++)
{
if (*j < pivot)
auto value = *(arr + i);
auto j = i;
while (j > low && comp(value, *(arr + (j - 1))))
{
dc::swap(*i, *j);
i++;
*(arr + j) = *(arr + (j - 1));
j--;
}
*(arr + j) = value;
}

dc::swap(*i, *hi);
return i;
};
}

template<class RandomIt, class Compare>
constexpr void quicksort(RandomIt lo, RandomIt hi, Compare comp)
constexpr auto partition(RandomIt arr, size_t start, size_t end, Compare comp)
{
if (lo < hi)
auto pivot = *(arr + ((start + end) / 2));
auto i = start - 1;
auto j = end + 1;

while (true)
{
auto pivot = dc::partition(lo, hi, comp);
if (lo < pivot)
quicksort(lo, pivot - 1, comp);
if (hi > pivot)
quicksort(pivot + 1, hi, comp);
do
{
++i;
} while (comp(*(arr+i), pivot));
do
{
--j;
} while (comp(pivot, *(arr+j)));

if (i >= j)
return j;

dc::swap(*(arr+i), *(arr+j));
}
}
};

template<class RandomIt, class Compare>
constexpr void siftDown(RandomIt first, RandomIt last, unsigned int start, Compare comp)
constexpr void siftDown(RandomIt first, RandomIt last, size_t start, Compare comp)
{
auto rootIndex = start;
auto rootItr = first+start;
Expand Down Expand Up @@ -119,33 +126,39 @@ namespace dc
}

template<class RandomIt, class Compare>
constexpr void introsort(RandomIt lo, RandomIt hi, Compare comp, unsigned int maxdepth)
constexpr void introsort(RandomIt arr, size_t start, size_t end, Compare comp)
{
if (lo < hi)
{
auto pivot = dc::partition(lo, hi, comp);
if (start >= end) return;

if (maxdepth == 0)
while (start < end)
{
if (end - start < 10)
{
heapsort(lo, hi, comp);
dc::insertionSort(arr, start, end, comp);
break;
}
else
{
if (lo < pivot)
dc::introsort(lo, pivot - 1, comp, maxdepth - 1);
if (hi > pivot)
dc::introsort(pivot + 1, hi, comp, maxdepth - 1);
auto partition = dc::partition(arr, start, end, comp);
if (partition - start < end - partition)
{
dc::introsort(arr, start, partition, comp);
start = partition + 1;
}
else
{
dc::introsort(arr, partition + 1, end, comp);
end = partition - 1;
}
}
}
}
}

template<class RandomIt, class Compare>
constexpr void introsort(RandomIt first, RandomIt last, Compare comp)
{
auto maxdepth = static_cast<unsigned int>(log(last - first) * 2);

// because iterator.end() is not the element of array
dc::introsort(first, --last, comp, maxdepth);
auto count = last - first;
dc::introsort(first, 0, count-1 , comp);
}

template<class RandomIt, class Compare>
Expand All @@ -169,4 +182,50 @@ namespace dc

//template< class ExecutionPolicy, class RandomIt, class Compare >
//void sort(ExecutionPolicy&& policy, RandomIt first, RandomIt last, Compare comp);

template<class T, class Compare>
constexpr size_t partition_a(T* arr, size_t start, size_t end, Compare comp)
{
auto pivot = arr[(start + end) / 2];
auto i = start-1;
auto j = end+1;

while (true)
{
do
{
++i;
} while (comp(arr[i], pivot));
do
{
--j;
} while (comp(pivot, arr[j]));

if (i >= j)
return j;

dc::swap(arr[i], arr[j]);
}
}

template<class T, class Compare>
constexpr void asort(T* arr, size_t start, size_t end, Compare comp)
{
if (start >= end) return;
auto partition = dc::partition_a(arr, start, end, comp);

dc::asort(arr, start, partition, comp);
dc::asort(arr, partition+1, end, comp);
}

template<class T>
constexpr void asort(T* arr, size_t start, size_t end)
{
auto DefaultComparator = [](auto lhs, auto rhs)
{
return lhs < rhs;
};

dc::asort(arr, start, end, DefaultComparator);
}
}
4 changes: 4 additions & 0 deletions dclb/dclb.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -96,6 +97,7 @@
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand All @@ -110,6 +112,8 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
77 changes: 14 additions & 63 deletions dclb/samplemain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,17 @@

auto main() -> int
{
std::srand(std::time(nullptr)); // use current time as seed for random generato

int intArray[] = { 5, 3, 32, -1, 1, 104, 53 };
int intArrayStd[] = { 5, 3, 32, -1, 1, 104, 53 };

int arraysize = sizeof(intArray) / sizeof(intArray[0]);
dc::sort(intArray, intArray+ arraysize);
std::sort(intArrayStd, intArrayStd + arraysize);

for (auto n : intArray)
{
std::cout << n << " ";
}
std::cout << std::endl;

for (auto n : intArrayStd)
{
std::cout << n << " ";
}

std::cout << std::endl;


std::vector<int> VectorTest = { 5, 3, 32, -1, 1, 104, 53 };
std::vector<int> VectorTestStd = { 5, 3, 32, -1, 1, 104, 53 };
dc::sort(VectorTest.begin(), VectorTest.end());
std::sort(VectorTestStd.begin(), VectorTestStd.end());

for (auto n : VectorTest)
{
std::cout << n << " ";
}
std::cout << std::endl;

for (auto n : VectorTestStd)
{
std::cout << n << " ";
}
std::cout << std::endl;

std::vector<int> UnsortedVectordcSmall(10);
for (int i = 0; i < 10; ++i)
{
UnsortedVectordcSmall.push_back(std::rand());
}
std::vector<int> UnsortedVectorstdSml = UnsortedVectordcSmall;
dc::sort(UnsortedVectordcSmall.begin(), UnsortedVectordcSmall.end());
std::sort(UnsortedVectorstdSml.begin(), UnsortedVectorstdSml.end());
for (auto n : UnsortedVectordcSmall)
{
std::cout << n << " ";
}
std::cout << std::endl;

for (auto n : UnsortedVectorstdSml)
{
std::cout << n << " ";
}
std::cout << std::endl;


std::vector<int> UnsortedVectordc(200000000);
for (int i = 0; i < 200000000; ++i)
{
UnsortedVectordc.push_back(std::rand());
UnsortedVectordc.push_back(std::rand() % 200000);
}
std::vector<int> UnsortedVectorstd = UnsortedVectordc;
std::vector<int> UnsortedVectorstd(200000000);
UnsortedVectorstd = UnsortedVectordc;

std::vector<int> UnsortedVectorarr(200000000);
UnsortedVectorarr = UnsortedVectordc;

auto stdstart = std::chrono::high_resolution_clock::now();
std::sort(UnsortedVectorstd.begin(), UnsortedVectorstd.end());
Expand All @@ -81,13 +26,19 @@ auto main() -> int

std::cout << "Std Sorting Took " << std_ms.count() << "ms" << std::endl;

auto arrstart = std::chrono::high_resolution_clock::now();
JFFoundation::Sort(&UnsortedVectorarr[0], 0, UnsortedVectorarr.size()-1);
auto arrend = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> arr_ms = arrend - arrstart;
std::cout << "Jaraffe Sorting Took " << arr_ms.count() << "ms" << std::endl;

auto dcstart = std::chrono::high_resolution_clock::now();
dc::sort(UnsortedVectordc.begin(), UnsortedVectordc.end());
auto dcend = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> dc_ms = dcend - dcstart;
std::cout << "dc Sorting Took " << dc_ms.count() << "ms" << std::endl;


system("pause");
return 0;
}

0 comments on commit f6f0c6b

Please sign in to comment.