samples of seven sort algorithms
Clear examples of: insertion, selection, shellsort, quicksort, mergesort, badsort Please let me know if anything wrong with it and please rate it.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std; //introduces namespace std
const int SIZE = 20;
void fill_vector(vector<int> & v);
ostream & operator<<(ostream & out , const vector<int> & v);
void selection(vector<int> & v);
void insertion(vector<int> & v);
void shellsort( vector<int> & v );
void quicksort( vector<int> & v, int low, int high);
void mergesort( vector<int> & v, int low, int high);
void merge( vector<int> & v, int low, int middle, int high);
void badsort( vector<int> & v);
bool sorted(const vector<int> & v);
//void swap( int & a,int & b);
int main ( void )
{
vector <int> v;
fill_vector(v);
cout << v << endl << "This is a test of sort\n";
//insertion (v);
//selection (v);
//sort(v.begin(), v.end()); // STL
//shellsort(v);
quicksort(v , 0, v.size()-1);
// mergesort(v, 0, v.size() - 1);
//badsort(v);
cout << v << endl;
return 0;
}
void fill_vector(vector<int> & v)
{ srand(time(0));
for (int i = 0; i < SIZE;i++)
v.push_back(rand() % 1000);
}
/* already in STL
void swap( int & a,int & b)
{int tem = a;
a = b;
b = tem;
}
*/
void selection(vector<int> & v)
{
int low;
for (int i = 0; i < v.size() - 1; i++)
{
low = i;
for (int j = i+1; j <v.size(); j++)
if (v[low] > v[j])
low = j;
if ( low != i)
swap (v[i] , v[low]);
}
}
void insertion(vector<int> & v)
{int j;
for (int i = 1; i < v.size(); i++)
{
int tem = v[i];
for ( j = i ; j > 0 && v[j - 1] > tem; j--)
v[j] = v[j-1];
if (j != i)
v[j] = tem;
}
}
void shellsort( vector<int> & v )
{
int j;
for( int gap = v.size( ) / 2; gap > 0;
gap = gap == 2 ? 1 : gap / 2.2 )
for( int i = gap; i < v.size( ); i++ )
{
int tmp = v[ i ];
for( j = i; j >= gap && tmp < v[ j - gap ]; j -= gap )
v[ j ] = v[ j - gap ];
v[ j ] = tmp;
}
}
void quicksort( vector<int> & v, int low, int high)
{
if ( low < high)
{ // choose pivot as median of v[low],v[middle],v[high]
int i , j;
int middle = (low + high) / 2;
cout << "initially low and high " << low << " " << high << endl;
for (i=low; i <= high; i++)
cout << v[i] << " ";
if(v[low] > v[middle])
swap (v[low] ,v[middle]);
if (v[low] > v[high])
swap(v[low] , v[high]);
if (v[middle] > v[high])
swap( v[middle], v[high]);
int pivot = v[middle];
swap (v[middle] , v[high-1]);
//partition
cout << "\nafter picking pivot = " << pivot << endl;
for (i=low; i <= high; i++)
cout << v[i] << " ";
if (high -low > 2
)
{
for (i = low , j = high -1; ;)
{
while (v[++i] < pivot );
while (j > 0 && v[--j] > pivot);
if ( i < j)
swap (v[i], v[j]);
else
break;
}
swap(v[i], v[high-1]);
cout << "\nafter partition " << endl;
for (int k=low; k <= high; k++)
cout << v[k] << " ";
quicksort(v , low, i-1);
quicksort(v , i+1, high);
}
}
}
void mergesort( vector<int> & v, int low, int high)
{
if (low < high)
{
int middle = (low + high) / 2;
mergesort(v, low,middle);
mergesort(v, middle+1,high);
merge( v , low , middle,high);
}
}
void merge( vector<int> & v, int low, int middle, int high)
{
cout << "merge with low = " << low << " and high = " << high << endl;
for (int i=low; i<=high;i++)
cout << v[i] << " ";
vector<int> tem(high-low+1);
int lowin = low, highin = middle + 1, temin = 0;
while (lowin <= middle && highin <= high)
{
if (v[lowin] < v[highin])
tem[temin++] = v[lowin++];
else
tem[temin++] = v[highin++];
}
while (lowin <= middle)
{
tem[temin++] = v[lowin++];
}
while ( highin <= high)
{
tem[temin++] = v[highin++];
}
for (int i=0; i < high-low+1; i++)
v[low+i] = tem[i];
for (int i=low; i<=high;i++)
cout << v[i] << " ";
cin.get();
}
void badsort( vector<int> & v)
{
while (!sorted(v))
next_permutation(v.begin(), v.end());
// random_shuffle(v.begin(),v.end());
}
bool sorted(const vector<int> & v)
{static int counter = 0;
cout << "counter = " << counter++ << endl;
bool ok = true;
for (int i=1; ok && i<v.size(); i++)
if ( v[i-1] > v[i])
ok = false;
return ok;
}
ostream & operator<<(ostream & out , const vector<int> & v)
{
for (int i=0 ; i< v.size(); i++)
out << (i%5? ' ' : '\n') << setw(8) << v[i] ;
return out;
}
<HTML>
<SCRIPT LANGUAGE="Javascript">
var xPos = 0;
var yPos = 0;
var bMark = false;
function bodyMouseDown()
{
var sDiv ='';
if (!document.all.divMark)
{
sDiv ="<DIV ID='divMark' NAME='divMark' style='border:solid;border-Width:1px;border-Color: #666666;background-Color: #ddddee;Filter:alpha(opacity=75)'></DIV>";
document.body.insertAdjacentHTML("BeforeEnd",sDiv);
document.all.item("divMark").style.position = "absolute";
document.all.item("divMark").style.posTop = event.clientX;
document.all.item("divMark").style.posLeft = event.clientY;
}
xPos = event.clientX;
yPos = event.clientY;
bMark = true;
}
function bodyMouseUp()
{
var lMax = document.body.all.length;
var sSelected="";
var lCount = 0;
var lMaxLeft = document.all.item("divMark").offsetLeft-20;
var lMaxRight = lMaxLeft + document.all.item("divMark").offsetWidth+20;
var lMaxTop = document.all.item("divMark").offsetTop-20;
var lMaxBottom = lMaxTop + document.all.item("divMark").offsetHeight+20;
while (lCount<lMax)
{
var obj = document.body.all.item(lCount);
var lLeft = obj.offsetLeft;
var lRight = obj.offsetLeft+obj.offsetWidth;
var lTop = obj.offsetTop;
var lBottom = lTop+obj.offsetHeight;
if((lMaxLeft<lLeft) && (lMaxRight>lRight) && (lMaxTop<lTop) && (lMaxBottom>lBottom))
{
sSelected=sSelected +obj.innerHTML +":";
}
lCount++;
}
bMark = false;
alert('You selected :' + sSelected);
}
function bodyMouseMove()
{
var xMouse=event.clientX;
var yMouse=event.clientY;
if (bMark==true)
{
if (xMouse<xPos)
{
document.all.item("divMark").style.posLeft=xMouse;
document.all.item("divMark").style.width=xPos-xMouse;
}
else
{
document.all.item("divMark").style.posLeft=xPos;
document.all.item("divMark").style.width=xMouse-xPos;
};
if (yMouse<yPos)
{
document.all.item("divMark").style.posTop=yMouse;
document.all.item("divMark").style.height=yPos-yMouse;
}
else
{
document.all.item("divMark").style.posTop=yPos;
document.all.item("divMark").style.height=yMouse-yPos;
};
};
}
</SCRIPT>
<BODY onMouseDown="bodyMouseDown()" onMouseUp="bodyMouseUp()" onMouseMove="bodyMouseMove()">
<div align="center">
<center>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" height="100%" width="100%" id="AutoNumber1">
<tr>
<td width="25%" align="center" bgcolor="#C0C0C0">Column 1 Row 1</td>
<td width="25%" align="center" bgcolor="#FFFF99">Column 2 Row 1</td>
<td width="25%" align="center" bgcolor="#C0C0C0">Column 3 Row 1</td>
<td width="25%" align="center" bgcolor="#FFFF99">Column 4 Row 1</td>
</tr>
<tr>
<td width="25%" align="center" bgcolor="#FFFF99">Column 1 Row 2</td>
<td width="25%" align="center" bgcolor="#C0C0C0">Column 2 Row 2</td>
<td width="25%" align="center" bgcolor="#FFFF99">Column 3 Row 2</td>
<td width="25%" align="center" bgcolor="#C0C0C0">Column 4 Row 2</td>
</tr>
<tr>
<td width="25%" align="center" bgcolor="#C0C0C0">Column 1 Row 3</td>
<td width="25%" align="center" bgcolor="#FFFF99">Column 2 Row 3</td>
<td width="25%" align="center" bgcolor="#C0C0C0">Column 3 Row 3</td>
<td width="25%" align="center" bgcolor="#FFFF99">Column 4 Row 3</td>
</tr>
<tr>
<td width="25%" align="center" bgcolor="#FFFF99">Column 1 Row 4</td>
<td width="25%" align="center" bgcolor="#C0C0C0">Column 2 Row 4</td>
<td width="25%" align="center" bgcolor="#FFFF99">Column 3 Row 4</td>
<td width="25%" align="center" bgcolor="#C0C0C0">Column 4 Row 4</td>
</tr>
</table>
</center>
</div>
</BODY>
</HTML>
Original Comments (3)
Recovered from Wayback Machine