<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>Sorting 
Comparison</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is <span lang="en-ca">first edition of fourth</span> assignment of <span lang="en-ca">comp352. And it is quite incomplete. I post it only for version </span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca">purpose.</span></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <font FACE="CenturySchoolbook-BoldItalic" SIZE="3"><i><b></b></i></font>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  กก</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<pre>#include &lt;iostream&gt;
#include &lt;time.h&gt;
#include &quot;utility.h&quot;


using namespace std;

int compCount=0;
int moveCount=0;
int threshold=1;
int minComp, maxComp;
int minMove, maxMove;
long totalComp, totalMove;
int threshArray[4];
int buffer[80000];

char* sortName[7] = {&quot;shell&quot;, &quot;quick1&quot;, &quot;merge&quot;, &quot;quick2&quot;, &quot;bubble&quot;, &quot;insert&quot;, &quot;select&quot;  };

char* sortString[3] = {&quot;shell&quot;, &quot;quick1&quot;, &quot;merge&quot;};

template&lt;class Elem, class Comp&gt;
void inssort(Elem A[], int n);

template&lt;class Elem, class Comp&gt;
void selsort(Elem A[], int n);

template&lt;class Elem, class Comp&gt;
void bubsort(Elem A[], int n);

template &lt;class Elem, class Comp&gt;
void shellsort(Elem A[], int n);

template &lt;class Elem, class Comp&gt;
void quksort(Elem A[], int n); 

template &lt;class Elem, class Comp&gt;
void quksort2(Elem A[], int n);

template &lt;class Elem, class Comp&gt;
void mersort(Elem* array, int n);

void fillArray(int array[], int size);


template &lt;class Elem, class Comp&gt;
void print(Elem A[], int size);

void test(int size, bool toShow=false);

void sortBy(int index, int size);

void testSort();
void output(int index, int size);

void experiment(int size, int index);

int min(int i, int j);
int max(int i, int j);

int main()
{
	srand(time(0));
//	testSort();
//	test(10000);
//	test(20000, true);
//	test(40000);
//	test(80000);
	experiment(10000, 0);
	return 0;
}

long do10Test(int size)
{
	long total=0;
	for (int i=0; i&lt;10l; i++)
	{
		fillArray(buffer, size);
		quksort&lt;int, Compare&gt;(buffer, size);
		total+=compCount;
		total+=moveCount;
	}
	return total/10;
}



void experiment(int size, int index)
{
	long do10Test(int size);

	int minimum=0, current;
	for (int i=1; i&lt;size; i+=5)//threshold starting at one
	{
		threshold = i;
		current = do10Test(size);
		cout&lt;&lt;&quot;i=&quot;&lt;&lt;i&lt;&lt;endl;
		cout&lt;&lt;&quot;current=&quot;&lt;&lt;current&lt;&lt;&quot; and minimum=&quot;&lt;&lt;minimum&lt;&lt;endl;
		if (i==1)
		{
			minimum = current;
		}
		else
		{			
			if (current&lt;minimum)
			{
				threshArray[index]=i;
				minimum = current;
				cout&lt;&lt;threshArray[index]&lt;&lt;&quot; is threshold&quot;&lt;&lt;endl;
			}
		}
	}
	cout&lt;&lt;threshArray[index]&lt;&lt;&quot; is threshold&quot;&lt;&lt;endl;
}
			




int min(int i, int j)
{
	return (i&gt;j)?j:i;
}

int max(int i, int j)
{
	return (i&lt;j)?j:i;
}

void output(int index, int size)
{
	cout&lt;&lt;sortString[index]&lt;&lt;&quot;\t&quot;&lt;&lt;&quot;number of comparison&quot;&lt;&lt;&quot;\tnumber of moves&quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;input size&quot;&lt;&lt;&quot;\tbest\tworst\taverage\t||\tbest\tworst\taverage&quot;&lt;&lt;endl;
	cout&lt;&lt;&quot;N=&quot;&lt;&lt;size&lt;&lt;&quot;\t&quot;&lt;&lt;minComp&lt;&lt;&quot;\t&quot;&lt;&lt;maxComp&lt;&lt;&quot;\t&quot;&lt;&lt;totalComp/10&lt;&lt;&quot;\t&quot;
		&lt;&lt;minMove&lt;&lt;&quot;\t&quot;&lt;&lt;maxMove&lt;&lt;&quot;\t&quot;&lt;&lt;totalMove/10&lt;&lt;endl;
}

void test(int size, bool toShow)
{

	for (int i=0; i&lt;3; i++)
	{
		if (toShow)
		{
			cout&lt;&lt;&quot;for size of &quot;&lt;&lt;size&lt;&lt;&quot; and sorting method of &quot;
				&lt;&lt;sortString[i]&lt;&lt;endl;
		}
		for (int count=0; count&lt;10; count++)
		{
			if (count==0)
			{
				minComp=maxComp=compCount;
				minMove=maxMove=moveCount;
				totalComp=totalMove=0;
			}
			fillArray(buffer, size);
			sortBy(i, size);
			
			if (toShow)
			{
				cout&lt;&lt;&quot;test no.&quot;&lt;&lt;count+1&lt;&lt;endl;
				cout&lt;&lt;&quot;comparison count:&quot;&lt;&lt;compCount&lt;&lt;endl;
				cout&lt;&lt;&quot;move count:      &quot;&lt;&lt;moveCount&lt;&lt;endl;
			}
			
			minComp = min(compCount, minComp);
			maxComp = max(compCount, maxComp);
			minMove = min(compCount, minMove);
			maxMove = max(compCount, maxMove);
		
			totalMove+=moveCount;
			totalComp+=compCount;
		}
		if (toShow)
		{
			cout&lt;&lt;&quot;total comparison count:&quot;&lt;&lt;totalComp&lt;&lt;endl;
			cout&lt;&lt;&quot;total move  count:     &quot;&lt;&lt;totalMove&lt;&lt;endl;
		}
		output(i, size);	
	}
}

template &lt;class Elem, class Comp&gt;
void print(Elem A[], int size)
{
	for (int i=0; i&lt;size; i++)
	{
		cout&lt;&lt;&quot;no.&quot;&lt;&lt;i+1&lt;&lt;&quot;: &quot;&lt;&lt;A[i]&lt;&lt;&quot;\n&quot;;
	}
}

void sortBy(int index, int size)
{
	switch(index)
	{
	case 0:
		shellsort&lt;int, Compare&gt;(buffer, size);
		break;
	case 1:
		quksort&lt;int, Compare&gt;(buffer, size);
		break;
	case 2:
		mersort&lt;int, Compare&gt;(buffer, size);
		break;
	case 3:
		quksort2&lt;int, Compare&gt;(buffer, size);
		break;
	case 4:
		bubsort&lt;int, Compare&gt;(buffer, size);
		break;
	case 5:
		inssort&lt;int, Compare&gt;(buffer, size);
		break;
	case 6:
		selsort&lt;int, Compare&gt;(buffer, size);
		break;
	}
}

void testSort()
{
	int size =10;
	for (int i=0; i&lt;7; i++)
	{
		fillArray(buffer, size);
		cout&lt;&lt;&quot;\nbefore...\n&quot;;
		print&lt;int, Compare&gt;(buffer, size);
		cout&lt;&lt;&quot;sorted by &quot;&lt;&lt;sortName[i]&lt;&lt;endl;
		sortBy(i, size);
		cout&lt;&lt;&quot;\nafter...\n&quot;;
		print&lt;int, Compare&gt;(buffer, size);
	}
}

void fillArray(int array[], int size)
{
	for (int i=0; i&lt; size; i++)
	{
		array[i] = rand();
	}
	compCount=moveCount=0;//initialize
}


template&lt;class Elem, class Comp&gt;
void bubsort(Elem A[], int n)
{
	for (int i=0; i&lt;n-1; i++)
	{
		for (int j=n-1; j&gt;i; j--)
		{
			if (Comp::lt(A[j], A[j-1]))
			{
				swap(A, j, j-1);
				moveCount+=3;
			}
			compCount++;
		}
	}
}


template &lt;class Elem&gt;
int findpivot(Elem A[], int i, int j)
{ 
	return (i+j)/2; 
}

template &lt;class Elem, class Comp&gt;
int partition(Elem A[], int l, int r, Elem&amp; pivot) 
{
	do {             // Move the bounds inward until they meet
	while (Comp::lt(A[++l], pivot))
	{
		compCount++;     // Move l right and
	}
	while ((r != 0) &amp;&amp; Comp::gt(A[--r], pivot))
	{
		compCount++; // r left
	}
	swap(A, l, r);              // Swap out-of-place values
	moveCount+=3;
	} while (l &lt; r);              // Stop when they cross
	swap(A, l, r);                // Reverse last, wasted swap
	moveCount+=3;
	return l;      // Return first position in right partition
}

template &lt;class Elem, class Comp&gt;
void qsort(Elem A[], int i, int j) 
{ // Quicksort
	if (j &lt;= i) return; // Don't sort 0 or 1 Elem
	int pivotindex = findpivot(A, i, j);
	swap(A, pivotindex, j);    // Put pivot at end
	moveCount+=3;
	// k will be the first position in the right subarray
	int k = partition&lt;Elem,Comp&gt;(A, i-1, j, A[j]);
	swap(A, k, j);             // Put pivot in place
	moveCount+=3;
	qsort&lt;Elem,Comp&gt;(A, i, k-1);
	qsort&lt;Elem,Comp&gt;(A, k+1, j);
}

template &lt;class Elem, class Comp&gt;
void quksort(Elem A[], int n) 
{
	qsort&lt;Elem,Comp&gt;(A, 0, n-1);
}



// Modified version of Insertion Sort for varying increments
template &lt;class Elem, class Comp&gt;
void inssort2(Elem A[], int n, int incr)
{
	for (int i=incr; i&lt;n; i+=incr)
	{
		for (int j=i; j&gt;=incr; j-=incr)
		{
			if (Comp::lt(A[j], A[j-incr]))
			{
				swap(A, j, j-incr);
				moveCount+=3;
			}
			compCount++;
		}
	}
}

template &lt;class Elem, class Comp&gt;
void shellsort(Elem A[], int n) 
{ // Shellsort
	for (int i=n/2; i&gt;2; i/=2)      // For each increment
	{
		for (int j=0; j&lt;i; j++) 
		{
			inssort2&lt;Elem,Comp&gt;(&amp;A[j], n-j, i);  // Sort each sublist
		}
	}
	inssort2&lt;Elem,Comp&gt;(A, n, 1);
}





template&lt;class Elem, class Comp&gt;
void selsort(Elem A[], int n)
{
	for (int i=0; i&lt;n; i++)
	{
		int lowIndex = i;
		for (int j=i+1; j&lt;n; j++)//i don't like loop of decrementing counter
		{
			if (Comp::lt(A[j], A[lowIndex]))
			{
				lowIndex = j;
			}
			compCount++;
		}
		//it is said that you reduce comparison but will increase swap
		//and verse vosa
		swap(A, i, lowIndex);
		moveCount+=3;
	}
}




template&lt;class Elem, class Comp&gt;
void inssort(Elem A[], int n)
{
	for (int i=1; i&lt;n; i++)
	{
		for (int j=i; j&gt;0; j--)
		{
			if (Comp::lt(A[j], A[j-1]))
			{
				swap(A, j, j-1);
				moveCount+=3;
			}
			compCount++;
		}
	}
}


template &lt;class Elem, class Comp&gt;
void qsort2(Elem A[], int i, int j)
{
	int n = j-i+1;
	if (j&lt;=i)
	{
		return;
	}
	if (n&gt;threshold)
	{
		int pivotindex = rand()%(j-i+1);
		Elem pivotElement = A[pivotindex];
		swap(A, pivotindex, j); 
		moveCount+=4;
		int k = partition&lt;Elem, Comp&gt;(A, i, j, pivotElement);
		 
		qsort2&lt;Elem, Comp&gt;(A, i, k-1);
		qsort2&lt;Elem, Comp&gt;(A, k+1, j);
	}
}


template &lt;class Elem, class Comp&gt;
void quksort2(Elem A[], int n)
{
	qsort2&lt;Elem, Comp&gt;(A, 0, n-1);
	inssort&lt;Elem, Comp&gt;(A, n);
}



template &lt;class Elem, class Comp&gt;
void mergesort(Elem A[], Elem temp[], int left, int right)
{
	int mid = (left+right)/2;
	if (left == right)
	{
		return;        // List of one element
	}
	mergesort&lt;Elem,Comp&gt;(A, temp, left, mid);
	mergesort&lt;Elem,Comp&gt;(A, temp, mid+1, right);
	for (int i=left; i&lt;=right; i++)   // Copy subarray to temp
	{
		temp[i] = A[i];
		moveCount++;
	}
	// Do the merge operation back to A
	int i1 = left; 
	int i2 = mid + 1;
	for (int curr=left; curr&lt;=right; curr++)
	{
		if (i1 == mid+1)      // Left sublist exhausted
		{
			A[curr] = temp[i2++];
		}
		else 
		{
			if (i2 &gt; right)  // Right sublist exhausted
			{
				A[curr] = temp[i1++];
			}
			else 
			{
				if (Comp::lt(temp[i1], temp[i2]))
				{
					A[curr] = temp[i1++];
				}		
				else
				{
					A[curr] = temp[i2++];
				}
				compCount++;
			}
		}
		moveCount++;//whatever result the move is always done once
	}
}

template &lt;class Elem, class Comp&gt;
void mersort(Elem* array, int n) {
	Elem* temp = NULL;
	if (temp == NULL)
	{
		temp = new Elem[n];  // Declare temp array
	}
	mergesort&lt;Elem,Comp&gt;(array, temp, 0, n-1);
	delete [] temp; 
}


</pre>

<pre>
</pre>

<pre></pre>
<pre>	
</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>