Skip to content

Commit

Permalink
rework stringstream+string thread code, add thread bekery algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptGreg committed Apr 3, 2016
1 parent df524a4 commit 9f3a2a8
Show file tree
Hide file tree
Showing 9 changed files with 1,446 additions and 49 deletions.
55 changes: 37 additions & 18 deletions binarysearch.cpp
@@ -1,26 +1,45 @@
// http://en.wikipedia.org/wiki/Binary_search_algorithm

const int KEY_NOT_FOUND = -1;
int binary_search(int A[], int key, int imin, int imax)
int binary_search(int data[], int key, int min, int max)
{
// continue searching while [imin,imax] is not empty
while (imax >= imin)
{
// calculate the midpoint for roughly equal partition
auto midpoint = [] (int imin, int imax) { return (imin + imax) / 2; };
int imid = midpoint(imin, imax);
if(A[imid] == key)
// key found at index imid
return imid;
// determine which subarray to search
else if (A[imid] < key)
// change min index to search upper subarray
imin = imid + 1;
else
// change max index to search lower subarray
imax = imid - 1;
// continue searching while [min,max] is not empty
while (max >= min) {
int mid = (min + max) / 2; // calculate the midpoint for roughly equal partition
if(data[mid] == key) // key found at index mid
return mid;
// determine which half of the subarray to search
else if (data[mid] < key)
min = mid + 1; // change min index to search upper subdataay
else
max = mid - 1; // change max index to search lower subdataay
}
return KEY_NOT_FOUND; // key was not found
}

// or equivalently:
// http://www.algolist.net/Algorithms/Binary_search
/*
* searches for a value in sorted array
* data is an array to search in
* value is searched value
* low is an index of low boundary
* high is an index of high boundary
* returns position of searched value, if it presents in the dataay
* or -1, if it is absent
*/

int binarySearch(int data[], int value, int low, int high)
{
while (low <= high) {
int middle = (low + high) / 2;
if (data[middle] == value)
return middle;
else if (data[middle] > value)
high = middle - 1;
else
low = middle + 1;
}
// key was not found
return KEY_NOT_FOUND;
}

Expand Down
123 changes: 114 additions & 9 deletions sorts.cpp
@@ -1,10 +1,17 @@
// best reference:
// https://en.wikibooks.org/wiki/Special:Search/Algorithm_Implementation/Sorting/

// some sorts are also in see Numerical Recipes in C

// GB original code hacked from
// http://stackoverflow.com/questions/244252/a-good-reference-card-cheat-sheet-with-the-basic-sort-algorithms-in-c

// GB ported C-code to C++ function templates, new/delete


#include <cstring> // memcpy
#include <iostream>
#include <algorithm> // for std::make_heap, std::sort_heap

using namespace std;

Expand All @@ -17,8 +24,70 @@ static void swap(T* a, T* b) {
}
}

template <typename Iterator>
void heap_sort(Iterator begin, Iterator end)
{
std::make_heap(begin, end);
std::sort_heap(begin, end);
}

template <typename T> // function template
void heapsortCPP(T* a, int l)
{
heap_sort(a, a+l);
}

template <typename T> // function template
void heapsortC(T arr[], unsigned int N)
{
// https://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Heapsort#C
// This is a fast implementation of heapsort in C, adapted from Numerical Recipes in C
// but designed to be slightly more readable and to index from 0.

if(N==0) // check if heap is empty
return;

T t; /* the temporary value */
unsigned int n = N, parent = N/2, index, child; /* heap indexes */
/* loop until array is sorted */
while (1) {
if (parent > 0) {
/* first stage - Sorting the heap */
t = arr[--parent]; /* save old value to t */
} else {
/* second stage - Extracting elements in-place */
n--; /* make the heap smaller */
if (n == 0) {
return; /* When the heap is empty, we are done */
}
t = arr[n]; /* save lost heap entry to temporary */
arr[n] = arr[0]; /* save root entry beyond heap */
}
/* insert operation - pushing t down the heap to replace the parent */
index = parent; /* start at the parent index */
child = index * 2 + 1; /* get its left child index */
while (child < n) {
/* choose the largest child */
if (child + 1 < n && arr[child + 1] > arr[child]) {
child++; /* right child exists and is bigger */
}
/* is the largest child larger than the entry? */
if (arr[child] > t) {
arr[index] = arr[child]; /* overwrite entry with child */
index = child; /* move index to the child */
child = index * 2 + 1; /* get the left child and go around again */
} else {
break; /* t's place is found */
}
}
/* store the temporary value at its new location */
arr[index] = t;
}
}

template <typename T> // function template
void bubblesort(T* a, int l) {
void bubblesort(T* a, int l)
{
int i, j; // GB cannot be unsigned (or size_t)

for (i = l - 2; i >= 0; i--) // GB NOTE >=0 terminate condition
Expand All @@ -27,7 +96,8 @@ void bubblesort(T* a, int l) {
}

template <typename T> // function template
void selectionsort(T* a, size_t l) {
void selectionsort(T* a, size_t l)
{
size_t i, j, k;
for (i = 0; i < l; i++) {
for (j = (k = i) + 1; j < l; j++) {
Expand All @@ -39,7 +109,8 @@ void selectionsort(T* a, size_t l) {
}

template <typename T> // function template
static void hsort_helper(T* a, int i, int l) {
static void hsort_helper(T* a, int i, int l)
{
int j;

for (j = 2 * i + 1; j < l; i = j, j = 2 * j + 1) {
Expand All @@ -58,7 +129,8 @@ static void hsort_helper(T* a, int i, int l) {
}

template <typename T> // function template
void heapsort(T* a, int l) {
void heapsort(T* a, int l)
{ // see Numerical Recipes in C
int i;

for (i = (l - 2) / 2; i >= 0; i--)
Expand All @@ -71,7 +143,8 @@ void heapsort(T* a, int l) {
}

template <typename T> // function template
static void msort_helper(T* a, T* b, size_t l) {
static void msort_helper(T* a, T* b, size_t l)
{
size_t i, j, k, m;

switch (l) {
Expand All @@ -89,7 +162,8 @@ static void msort_helper(T* a, T* b, size_t l) {
}

template <typename T> // function template
void mergesort(T* a, size_t l) {
void mergesort(T* a, size_t l)
{
T *b;

if (l < 0)
Expand All @@ -104,7 +178,8 @@ void mergesort(T* a, size_t l) {
}

template <typename T> // function template
static int pivot(T* a, size_t l) {
static int pivot(T* a, size_t l)
{
size_t i, j;

for (i = j = 1; i < l; i++)
Expand All @@ -117,7 +192,8 @@ static int pivot(T* a, size_t l) {
}

template <typename T> // function template
void quicksort(T* a, size_t l) {
void quicksort(T* a, size_t l)
{
if (l <= 1)
return;

Expand All @@ -127,7 +203,8 @@ void quicksort(T* a, size_t l) {
}

template <typename T> // function template
void btreesort(T* a, size_t l) {
void btreesort(T* a, size_t l)
{
size_t i;
struct node {
T value;
Expand Down Expand Up @@ -187,6 +264,22 @@ void shellsort(T a[], const int size)// Nothing to do with shells. Named after
}
}

template <typename T> // function template
void insertionsort(T a[], const int length)
// https://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Insertion_sort
{
int i, j;
T value;

for(i = 1 ; i < length ; i++)
{
value = a[i];
for (j = i - 1; j >= 0 && a[j] > value; j--)
a[j + 1] = a[j];
a[j + 1] = value;
}
}

int main(int argc, char**argv)
{
int a[] = {42,19,2,66,1,33,8,5,19};
Expand All @@ -207,6 +300,14 @@ int main(int argc, char**argv)
heapsort(a, l);
cout << "heapsort "; for(auto e: a) cout << e << " "; cout << "\n";

memcpy(a,b,sizeof(a));
heapsortC(a, l);
cout << "heapsortC "; for(auto e: a) cout << e << " "; cout << "\n";

memcpy(a,b,sizeof(a));
heapsortCPP(a, l);
cout << "heapsortCPP "; for(auto e: a) cout << e << " "; cout << "\n";

memcpy(a,b,sizeof(a));
mergesort(a, l);
cout << "mergesort "; for(auto e: a) cout << e << " "; cout << "\n";
Expand All @@ -222,4 +323,8 @@ int main(int argc, char**argv)
memcpy(a,b,sizeof(a));
shellsort(a, l);
cout << "shellsort "; for(auto e: a) cout << e << " "; cout << "\n";

memcpy(a,b,sizeof(a));
insertionsort(a, l);
cout << "insertionsort "; for(auto e: a) cout << e << " "; cout << "\n";
}

0 comments on commit 9f3a2a8

Please sign in to comment.