Almost a Lifetime of Computer Programming (aka Software Development) by James Pate Williams, Jr.

As I have mentioned before on this website (blog), I taught myself BASIC (Beginner’s All-purpose Symbolic Instruction Code) in the summer of 1978. I went on to undergraduate college courses in BASIC, FORTRAN (Formula Translator) IV, Intel 8085 or 8086 assembly and machine language programming, C, COBOL (Common Business Oriented Language kudos to Rear Admiral Grace Hopper), and Pascal. In between my two undergraduate careers, I taught myself Amiga BASIC, Modula-2, Motorola 68000 macro-assembly language, and Pecan Pascal on my ever-faithful 1988 Commodore Amiga 2000. After my second graduation from LaGrange College, I taught myself C++ in 1996 and client/server Internet programming in C also in 1996. As a graduate student at Auburn University during my tenure as a student, I had formal courses in Java, Common LISP, and Scheme in 1999 and Palm Operating System C. later in my studies. In the late-2000s I taught myself C#.

Procedural Languages: C, COBOL, FORTRAN IV, Pascal

Functional Languages: Common LISP (List Processor) and Scheme

In between procedural and object-oriented languages: Modula-2

Object-Oriented Languages: C++, Common LISP, and Java

Create an Index also Known as a Symbol Table Using C++ and the Vector Data Structure Designed and Implemented by James Pate Williams, Jr.

The hash node consists of a char position within a line, a line number, an ASCII character array symbol, and a hash value.  ASCII characters have the decimal value of 0 to 127. The hash function is the value of the first symbol character * 128 + the value of the second symbol character. There can be hash value collisions. The symbol table is defined as a vector<HashNode> table[128 * 128 + 128]. This is a very elementary method of handling collisions. The hash table generated by this application is as follows:

This		1	1	
The		2	12	
The		1	39	
a		1	9	
dummy		2	31	
definitions	2	37	
file		1	48	
generator	1	28	
has		1	53	
is		1	6	
is		2	28	
index		1	22	
lines		2	5	
line		2	23	
my		1	19	
of		1	16	
second		2	16	
two		2	1	
text		1	43	
test		1	11

There are two collision generating elements namely “The” and “is”. See my previous blog for the original data file. I sacrifice memory for functional complexity. Collisions require sorting of an element. I use TreeSort3. In the map-based indexer I use Quick Sort. Here is a pertinent comment:

// Insertion sort, heap sort, and quick sort algorithms
// From "Algortihms" by Thomas H. Cormen, et. al.
// Selection sort, Singleton's sort, and Tree Sort 3
// From "Sorting and Sort Systems" by Harold Lorin

Create an Index Using C++ and the Map Data Structure Designed and Implemented by James Pate Williams, Jr.

I recall that way back in the early to mid-1980s I had the pleasure of perusing a copy of the source code for a Pascal compiler. It was probably created directly under the inventor Nicklaus Wirth in Switzerland. I partially implemented a Pascal emulator for a Data General Eclipse minicomputer.

Here are some of the phases required for the creation of a Pascal computer program:

  1. Parse the source code.
  2. Create a symbol table.
  3. Interpret the symbols.
  4. Create P-Code for the interpreter.

Running the interpreter code involves translation of the P-Code to a computer readable bit string. Every computer scientist should at some time in her/his formal education should implement an assembler and a compiler.

Yesterday, April 11, 2023, I created a word index C++ application that takes a text file, parses the words, and creates an index also known as an English language symbol table. The app utilizes a C++ map that consists of integer keys and a node containing information about the words and their order in the text file. Below are the indexable text file and the symbol table (index).

This is a test of my index generator. The text file has

two lines. The second line is dummy definitions.

This is a test of my index generator. The text file has
two lines. The second line is dummy definitions.
The first number is the line number and the second the position within a line.

The         1             39          
The         2             12          
This        1             1            
a              1             9            
definitions           2             37          
dummy 2             31          
file          1             48          
generator            1             28          
has         1             53          
index     1             22          
is             2             28          
is             1             6            
line         2             23          
lines       2             5            
my          1             19          
of            1             16          
second  2             16          
test        1             11          
text        1             43          
two        2             1

Prime Number Binary Search Tree Implemented by James Pate Williams, Jr.

#pragma once

#include <cinttypes>
#include <vector>
using namespace std;

// See "Introduction to Algorithms"
// Thomas H. Cormen Among Others
// Chapter 13 Binary Search Trees
// Translated from Pascal found in
// "Applied Data Structures Using
// Pascal" by Guy J. Hale and
// Richard J. Easton Chapter 6
// Introduction to Trees

typedef struct treeNode
{
	uint32_t key;
	treeNode* lt, * rt;
} TREENODE, * PTREENODE;

class BinarySearchTree
{
public:
	static void InitTree(
		PTREENODE root, uint32_t key);
	static void CreateTree(
		PTREENODE& root, vector<uint32_t>& data,
		unsigned int seed, size_t bound);
	static void InOrderTreeWalk(PTREENODE x);
	static PTREENODE TreeSearch(PTREENODE x, uint32_t k,
		uint32_t& depth);
	static PTREENODE IterativeTreeSearch(
		PTREENODE x, uint32_t k);
	static PTREENODE TreeMinimum(PTREENODE x);
	static PTREENODE TreeMaximum(PTREENODE x);
	static PTREENODE TreeSuccessor(PTREENODE x);
	static void InsertTree(
		PTREENODE &root, uint32_t key);
};
#include "pch.h"
#include "BinarySearchTree.h"
#include "SieveOfEratosthenes.h"
#include <iostream>
#include <vector>
using namespace std;

void BinarySearchTree::InitTree(
	PTREENODE root, uint32_t key)
{
	root->key = key;
	root->lt = root->rt = NULL;
}

void BinarySearchTree::CreateTree(
	PTREENODE& root, vector<uint32_t>& keys,
	unsigned int seed, size_t bound)
{
	SieveOfEratosthenes::Initialization(bound);

	for (uint32_t i = 0; i < bound; i++)
	{
		uint32_t prime = SieveOfEratosthenes::GetNextPrime(bound);

		if (prime != -1)
			keys.push_back(prime);
	}

	srand(seed);

	unsigned int number;
	uint32_t key = -1;

	number = rand() % keys.size();
	key = keys[number];
	InitTree(root, key);
	keys.erase(keys.begin() + number, keys.begin() + number + 1);
	size_t count = 0, start = keys.size();

	for (size_t count = 1; count < start; count++)
	{
		bool found = false;

		do
		{
			number = rand() % start;

			if (number < keys.size())
			{
				key = keys[number];
				found = true;
			}
		} while (!found);
		
		keys.erase(keys.begin() + number, keys.begin() + number + 1);
		BinarySearchTree::InsertTree(root, key);
	}

	delete[] SieveOfEratosthenes::sieve;
}

void BinarySearchTree::InOrderTreeWalk(PTREENODE x)
{
	if (x != NULL)
	{
		InOrderTreeWalk(x->lt);
		cout << x->key << endl;
		InOrderTreeWalk(x->rt);
	}
}

PTREENODE BinarySearchTree::TreeSearch(
	PTREENODE x, uint32_t k, uint32_t &depth)
{
	depth++;

	if (x == NULL || x->key == k)
		return x;
	if (k < x->key)
		return TreeSearch(x->lt, k, depth);

	return TreeSearch(x->rt, k, depth);
}

PTREENODE BinarySearchTree::IterativeTreeSearch(
	PTREENODE x, uint32_t k)
{
	while (x != NULL && x->key != k)
	{
		if (k < x->key)
			x = x->lt;
		else
			x = x->rt;
	}

	return x;
}

PTREENODE BinarySearchTree::TreeMinimum(PTREENODE x)
{
	while (x->lt != NULL)
		x = x->lt;
	return x;
}

PTREENODE BinarySearchTree::TreeMaximum(PTREENODE x)
{
	while (x->rt != NULL)
		x = x->rt;
	return x;
}

PTREENODE BinarySearchTree::TreeSuccessor(PTREENODE x)
{
	if (x->rt != NULL)
		return TreeSuccessor(x->rt);
	PTREENODE y = x;
	while (y != NULL && x == y->rt)
		x = y;
	return x;
}

void BinarySearchTree::InsertTree(
	PTREENODE& root, uint32_t key)
{
	bool inserted = false;
	PTREENODE node = new TREENODE();
	PTREENODE oneNode = root;

	while (!inserted)
	{
		if (key <= oneNode->key)
		{
			if (oneNode->lt != NULL)
				oneNode = oneNode->lt;
			else
			{
				oneNode->lt = node;
				inserted = true;
			}
		}

		else
		{
			if (oneNode->rt != NULL)
				oneNode = oneNode->rt;
			else
			{
				oneNode->rt = node;
				inserted = true;
			}
		}
	}

	node->key = key;
	node->lt = node->rt = NULL;
}
// PrimeNumberBST.cpp : This file contains the 'main' function.
// Program execution begins and ends there.
// Create a prime number search tree
// James Pate Wiliams, Jr. (c) 2023

#include "pch.h"
#include "BinarySearchTree.h"
#include "SieveOfEratosthenes.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	while (true)
	{
		uint32_t bound = 50;
		size_t start = 0;
		unsigned int seed = 1;
		PTREENODE root = new TREENODE();

		cout << "Key Bound = ";
		cin >> bound;

		if (bound == 0)
			break;

		cout << "PRNG seed = ";
		cin >> seed;
		cout << endl;

		vector<uint32_t> data;
		uint32_t depth = 0, key;
		BinarySearchTree::CreateTree(
			root, data, seed, bound);
		BinarySearchTree::InOrderTreeWalk(root);
		cout << endl;
		cout << "Search key = ";
		cin >> key;

		PTREENODE x = BinarySearchTree::TreeSearch(
			root, key, depth);

		cout << "Search depth = ";
		cout << depth << endl;
		cout << "Found key = ";

		if (x != NULL)
			cout << x->key << endl;
		else
			cout << "Key not found" << endl;
	
		cout << endl;
	}

	return 0;
}

C++ Binary Search Tree Software Translated from Pascal Algorithms by James Pate Williams, Jr.

#pragma once
#include <cinttypes>
#include <vector>
using namespace std;

// See "Introduction to Algorithms"
// Thomas H. Cormen Among Others
// Chapter 13 Binary Search Trees
// Translated from Pascal found in
// "Applied Data Structures Using
// Pascal" by Guy J. Hale and
// Richard J. Easton Chapter 6
// Introduction to Trees

typedef struct treeNode
{
	uint32_t key;
	treeNode* lt, * rt;
} TREENODE, * PTREENODE;

class BinaryTree
{
public:
	static void InitTree(PTREENODE root, uint32_t key);
	static void InsertInTree(PTREENODE& root, uint32_t key);
	static void CreateTree(PTREENODE& root, vector<uint32_t> &data,
		unsigned int seed, size_t number);
	static void TravInOrder(PTREENODE node);
	static void TravPreOrder(PTREENODE node);
	static void TravPostOrder(PTREENODE node);
};
#include "BinaryTree.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;

void BinaryTree::InitTree(
	PTREENODE root, uint32_t key)
{
	root->key = key;
	root->lt = root->rt = NULL;
}

void BinaryTree::InsertInTree(
	PTREENODE& root, uint32_t key)
{
	bool inserted = false;
	PTREENODE node = new TREENODE();
	PTREENODE oneNode = root;
	
	while (!inserted)
	{
		if (key <= oneNode->key)
		{
			if (oneNode->lt != NULL)
				oneNode = oneNode->lt;
			else
			{
				oneNode->lt = node;
				inserted = true;
			}
		}

		else
		{
			if (oneNode->rt != NULL)
				oneNode = oneNode->rt;
			else
			{
				oneNode->rt = node;
				inserted = true;
			}
		}
	}

	node->key = key;
	node->lt = node->rt = NULL;
}

void BinaryTree::CreateTree(PTREENODE& root, vector<uint32_t>& data,
	unsigned int seed, size_t number)
{
	srand(seed);
	uint32_t key = rand() % 1000;

	data.push_back(key);
	root = new TREENODE();
	InitTree(root, key);

	for (size_t i = 1; i < number; i++)
	{
		bool found = false;
		uint32_t next = rand() % 1000;
		
		while (!found)
		{
			for (size_t j = 0; !found && j < data.size(); j++)
				found = data[j] == next;

			next = rand() % 1000;
		}

		data.push_back(next);
	}

	for (size_t i = 1; i < data.size(); i++)
	{
		BinaryTree::InsertInTree(root, data[i]);
	}
}

void BinaryTree::TravInOrder(PTREENODE node)
{
	if (node != NULL)
	{
		TravInOrder(node->lt);
		cout << node->key << endl;
		TravInOrder(node->rt);
	}
}

void BinaryTree::TravPreOrder(PTREENODE node)
{
	if (node != NULL)
	{
		cout << node->key << endl;
		TravPreOrder(node->lt);
		TravPreOrder(node->rt);
	}
}

void BinaryTree::TravPostOrder(PTREENODE node)
{
	if (node != NULL)
	{
		TravPostOrder(node->lt);
		TravPostOrder(node->rt);
		cout << node->key << endl;
	}
}
// DataStructuresFromPascal.cpp : This file contains the 'main'
// function. Program execution begins and ends there.
// James Pate Williams, Jr. (c) 2023

#include "BinaryTree.h"
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	PTREENODE root = NULL;
	vector<uint32_t> data;
	unsigned int seed = 1;
	size_t number;

	cout << "# keys = ";
	cin >> number;
	cout << "PRNG seed = ";
	cin >> seed;
	cout << endl;
	BinaryTree::CreateTree(root, data, seed, number);
	BinaryTree::TravInOrder(root);
	cout << endl;
	BinaryTree::TravPreOrder(root);
}

Multiple Integration Using Simpson’s Rule – Calculation of the Ground State Energies of the First Five Non-Relativistic Multiple Electron Atoms by James Pate Williams, Jr.

We calculate the ground state energies for Helium (Z = 2), Lithium (Z = 3), Beryllium (Z = 4), Boron (Z = 5), and Carbon (Z = 6). Currently, only three of the five atoms are implemented.

Multiple Integration Using Simpson’s Rule – Calculation of the Ground State of the Non-Relativistic Helium Atom by James Pate Williams, Jr.

The six-dimensional Cartesian coordinate wavefunction is calculated by the C# method:

public double Psi(double[] x, double[] alpha)
        {
            double r1 = Math.Sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
            double r2 = Math.Sqrt(x[3] * x[3] + x[4] * x[4] + x[5] * x[5]);
            double r12 = Math.Sqrt(Math.Pow(x[0] - x[3], 2.0) +
                Math.Pow(x[1] - x[4], 2.0) + Math.Pow(x[2] - x[5], 2.0));
            double exp1 = Math.Exp(-alpha[0] * r1);
            double exp2 = Math.Exp(-alpha[1] * r2);
            double exp3 = Math.Exp(-alpha[2] * r12);

            return exp1 * exp2 * exp3;
        }

        public double Psi2(double[] x, double[] alpha)
        {
            double psi = Psi(x, alpha);

            return psi * psi;
        }

The wavefunction normalization method is:

public double Normalize(double[] alpha, int nSteps)
        {
            double[] lower = new double[6];
            double[] upper = new double[6];
            int[] steps = new int[6];

            lower[0] = 0.001;
            lower[1] = 0.001;
            lower[2] = 0.001;
            lower[3] = 0.001;
            lower[4] = 0.001;
            lower[5] = 0.001;

            upper[0] = 10.0;
            upper[1] = 10.0;
            upper[2] = 10.0;
            upper[3] = 10.0;
            upper[4] = 10.0;
            upper[5] = 10.0;

            for (int i = 0; i < 6; i++)
                steps[i] = nSteps;

            double norm = Math.Sqrt(integ.Integrate(
                lower, upper, alpha, Psi2, 6, steps));

            return norm;
        }

The two kinetic energy integrands are encapsulated in the following two methods:

private double Integrand1(double[] x, double[] alpha)
        {
            double r1 = Math.Sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
            double r2 = Math.Sqrt(x[3] * x[3] + x[4] * x[4] + x[5] * x[5]);
            double r12 = Math.Sqrt(Math.Pow(x[0] - x[3], 2.0) +
                Math.Pow(x[1] - x[4], 2.0) + Math.Pow(x[2] - x[5], 2.0));
            double term = -2.0 * alpha[0] / r1 + alpha[0] * alpha[0];
            double mul1 = Math.Exp(-2.0 * alpha[0] * r1);
            double mul2 = Math.Exp(-2.0 * alpha[1] * r2);
            double mul3 = Math.Exp(-2.0 * alpha[2] * r12);

            return N * N * term * mul1 * mul2 * mul3;
        }

        private double Integrand2(double[] x, double[] alpha)
        {
            double r1 = Math.Sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
            double r2 = Math.Sqrt(x[3] * x[3] + x[4] * x[4] + x[5] * x[5]);
            double r12 = Math.Sqrt(Math.Pow(x[0] - x[3], 2.0) +
                Math.Pow(x[1] - x[4], 2.0) + Math.Pow(x[2] - x[5], 2.0));
            double term = -2.0 * alpha[1] / r1 + alpha[1] * alpha[1];
            double mul1 = Math.Exp(-2.0 * alpha[0] * r1);
            double mul2 = Math.Exp(-2.0 * alpha[1] * r2);
            double mul3 = Math.Exp(-2.0 * alpha[2] * r12);

            return N * N * term * mul1 * mul2 * mul3;
        }

The two potential energy terms use the following two integrands:

private double Integrand3(double[] x, double[] alpha)
        {
            double r1 = Math.Sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
            double r2 = Math.Sqrt(x[3] * x[3] + x[4] * x[4] + x[5] * x[5]);
            double r12 = Math.Sqrt(Math.Pow(x[0] - x[3], 2.0) +
                Math.Pow(x[1] - x[4], 2.0) + Math.Pow(x[2] - x[5], 2.0));
            double term = 1.0 / r1;
            double mul =
                Math.Exp(-2.0 * alpha[0] * r1) *
                Math.Exp(-2.0 * alpha[1] * r2) *
                Math.Exp(-2.0 * alpha[2] * r12);

            return -N * N * Z * term * mul;
        }

        private double Integrand4(double[] x, double[] alpha)
        {
            double r1 = Math.Sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
            double r2 = Math.Sqrt(x[3] * x[3] + x[4] * x[4] + x[5] * x[5]);
            double r12 = Math.Sqrt(Math.Pow(x[0] - x[3], 2.0) +
                Math.Pow(x[1] - x[4], 2.0) + Math.Pow(x[2] - x[5], 2.0));
            double term = 1.0 / r2;
            double mul =
                Math.Exp(-2.0 * alpha[0] * r1) *
                Math.Exp(-2.0 * alpha[1] * r2) *
                Math.Exp(-2.0 * alpha[2] * r12);

            return -N * N * Z * term * mul;
        }

The electron-electron repulsion integrand is given by:

private double Integrand5(double[] x, double[] alpha)
        {
            double r1 = Math.Sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
            double r2 = Math.Sqrt(x[3] * x[3] + x[4] * x[4] + x[5] * x[5]);
            double r12 = Math.Sqrt(Math.Pow(x[0] - x[3], 2.0) +
                Math.Pow(x[1] - x[4], 2.0) + Math.Pow(x[2] - x[5], 2.0));

            if (r12 == 0)
                r12 = 0.01;

            double term = 1.0 / r12;
            double mul =
                Math.Exp(-2.0 * alpha[0] * r1) *
                Math.Exp(-2.0 * alpha[1] * r2) *
                Math.Exp(-2.0 * alpha[2] * r12);

            return N * N * term * mul;
        }

The ground state non-relativistic energy is computed by the method:

public double Energy(double[] alpha, int nSteps, int Z)
        {
            double[] lower = new double[6];
            double[] upper = new double[6];
            int[] steps = new int[6];

            lower[0] = lower[1] = lower[2] = lower[3] = lower[4] = lower[5] = 0.001;
            upper[0] = upper[1] = upper[2] = upper[3] = upper[4] = upper[5] = 10.0;
            steps[0] = steps[1] = steps[2] = steps[3] = steps[4] = steps[5] = nSteps;

            N = Normalize(alpha, nSteps);

            this.Z = Z;

            double integ1 = integ.Integrate(lower, upper, alpha, Integrand1, 6, steps);
            double integ2 = integ.Integrate(lower, upper, alpha, Integrand2, 6, steps);
            double integ3 = integ.Integrate(lower, upper, alpha, Integrand3, 6, steps);
            double integ4 = integ.Integrate(lower, upper, alpha, Integrand4, 6, steps);
            double integ5 = integ.Integrate(lower, upper, alpha, Integrand5, 6, steps);
            
            return integ1 + integ2 - integ3 - integ4 + integ5;
        }

Using trial and error we calculate Alpha as: 0.535139999999

public double Integrate(double[] lower, double[] upper, double[] alpha,
            Func<double[], double[], double> f, int n, int[] steps)
        {
            double p = 1;
            double[] h = new double[n];
            double[] h2 = new double[n];
            double[] s = new double[n];
            double[] t = new double[n];
            double[] x = new double[n];
            double[] w = new double[n];

            for (int i = 0; i < n; i++)
            {
                h[i] = (upper[i] - lower[i]) / steps[i];
                h2[i] = 2.0 * h[i];
            }

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                    x[j] = lower[j];
                
                x[i] = lower[i] + h[i];

                for (int j = 1; j < steps[i]; j += 2)
                {
                    s[i] += f(x, alpha);
                    x[i] += h2[i];
                }

                for (int j = 0; j < n; j++)
                    x[j] = lower[j];

                x[i] = lower[i] + h2[i];

                for (int j = 2; j < steps[i]; j += 2)
                {
                    t[i] += f(x, alpha);
                    x[i] += h2[i];
                }

                w[i] = h[i] * (f(lower, alpha) + 4 * s[i] + 2 * t[i] + f(upper, alpha)) / 3.0;
            }

            for (int i = 0; i < n; i++)
                p *= w[i];

            return p;
        }

C++ Searching: Binary and Linear Implemented by James Pate Williams, Jr.

#pragma once

// Binary searches from the following website
// https://www.geeksforgeeks.org/binary-search/

typedef long long ll;

class Search
{
public:
	int IterativeBinarySearch(
		ll A[], ll x, int low, int high);
	int RecursiveBinarySearch(
		ll A[], ll x, int low, int high);
	int IterativeLinearSearch(
		ll A[], ll x, int low, int high);
};
#include "Search.h"

int Search::IterativeBinarySearch(
	ll A[], ll x, int low, int high)
{
	do
	{
		int middle = low + (high - low) / 2;

		if (x == A[middle])
			return middle;

		else if (x > A[middle])
			low = middle + 1;

		else if (x < A[middle])
			high = middle - 1;

	} while (high >= low);

	return -1;
}

int Search::RecursiveBinarySearch(
	ll A[], ll x, int low, int high)
{
	if (high >= low)
	{
		int middle = low + (high - low) / 2;

		if (x == A[middle])
			return middle;

		else if (x > A[middle])
			return RecursiveBinarySearch(
				A, x, middle + 1, high);

		else if (x < A[middle])
			return RecursiveBinarySearch(
				A, x, low, middle - 1);
	}
	
	return -1;
}

int Search::IterativeLinearSearch(
	ll A[], ll x, int low, int high)
{
	for (int i = low; i <= high; i++)
		if (x == A[i])
			return i;

	return -1;
}
// Searching.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "Search.h"
#include <chrono>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
using chrono::duration_cast;
using chrono::nanoseconds;

ll A[1000001];

ll RandomLongLong()
{
	ll lo = rand();
	ll hi = rand();

	return (lo << 31) | hi;
}

void GenerateArray(ll A[], int n, int seed)
{
	for (int i = 1; i <= n; i++)
		A[i] = RandomLongLong();
}

void GenerateArrayMod(ll A[], int n, int mod, int seed)
{
	Search search;

	for (int i = 1; i <= n; i++)
	{
		while (true)
		{
			ll Ai = RandomLongLong() % mod;

			if (search.IterativeLinearSearch(
				A, Ai, 1, i) == -1)
			{
				A[i] = Ai;
				break;
			}
		}
	}
}

int Partition(ll* A, int p, int r)
{
	int q = p;
	ll t;

	for (int u = p; u <= r - 1; u++)
	{
		if (A[u] <= A[r])
		{
			t = A[q];
			A[q] = A[u];
			A[u] = t;
			q++;
		}
	}

	t = A[q];
	A[q] = A[r];
	A[r] = t;

	return q;
}

void RunQuickSort(ll* A, int p, int r)
{
	if (p < r)
	{
		int q = Partition(A, p, r);

		RunQuickSort(A, p, q - 1);
		RunQuickSort(A, q + 1, r);
	}
}

void RunSearches(
	Search search,
	ll A[],
	int n, int seed)
{
	int index[4];
	ll x = RandomLongLong() % n;

	GenerateArrayMod(A, n, 2LL * n, seed);
	RunQuickSort(A, 1, n);
	
	cout << "key = " << x << endl;

	if (n <= 25)
	{		for (int i = 1; i <= n; i++)
			cout << A[i] << " ";

		cout << endl;
	}

	cout << "Runtimes in nanoseconds" << endl;
	cout << "n" << "\t" << "Iter" << "\t";
	cout << "Recur" << "\t" << "Linear" << endl;
	cout << setw(5) << n << "\t";

	auto start1 = chrono::steady_clock::now();
	index[0] = search.IterativeBinarySearch(A, x, 1, n);
	auto final1 = chrono::steady_clock::now();

	cout << setw(5) << duration_cast<nanoseconds>(final1 - start1).count();
	cout << "\t";

	auto start2 = chrono::steady_clock::now();
	index[1] = search.RecursiveBinarySearch(A, x, 1, n);
	auto final2 = chrono::steady_clock::now();

	cout << setw(5) << duration_cast<nanoseconds>(final2 - start2).count();
	cout << "\t";

	auto start3 = chrono::steady_clock::now();
	index[2] = search.IterativeLinearSearch(A, x, 1, n);
	auto final3 = chrono::steady_clock::now();

	cout << setw(5) << duration_cast<nanoseconds>(final3 - start3).count();
	cout << endl;
	cout << "Search indicies" << endl;

	for (int i = 0; i < 3; i++)
		cout << "index[" << i << "] = " << index[i] << endl;
}

int main()
{
	cout << "Comparison of Iterative Binary Search," << endl;
	cout << "Recursive Binary Search, and Linear Search" << endl;

	while (true)
	{
		int n, seed;
		Search search;

		cout << "n = ";
		cin >> n;
		
		if (n == 0)
			break;

		cout << "seed = ";
		cin >> seed;
		srand(seed);

		RunSearches(search, A, n, seed);
	}
}