Skip to content

Instantly share code, notes, and snippets.

@tiesont
Last active December 12, 2015 08:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tiesont/4748537 to your computer and use it in GitHub Desktop.
class RandomDataSet
{
private int seed, compCount;
private List<int> idataset = new List<int>();
/// <summary>
/// Default constructor.
/// </summary>
public RandomDataSet()
: this( new List<int>(), 0, System.DateTime.Now.Millisecond )
{
}
/// <summary>
/// Overloaded constructor.
/// </summary>
/// <param name="seed">An integer value for seeding the random number generator.</param>
public RandomDataSet( int seed )
: this( new List<int>(), 0, seed )
{
}
/// <summary>
/// Overloaded constructor.
/// </summary>
/// <param name="dataset">A integer List&lt;&gt; dataset.</param>
public RandomDataSet( List<int> dataset )
: this( dataset, 0, System.DateTime.Now.Millisecond )
{
}
/// <summary>
/// Overloaded constructor.
/// </summary>
/// <param name="dataset">A integer List&lt;&gt; dataset.</param>
/// <param name="capacity">A value to specify the initial capacity of the dataset.</param>
public RandomDataSet( List<int> dataset, int capacity )
: this( dataset, capacity, System.DateTime.Now.Millisecond )
{
}
/// <summary>
/// Overloaded constructor.
/// </summary>
/// <param name="dataset">A integer List&lt;&gt; dataset.</param>
/// <param name="capacity">A value to specify the initial capacity of the dataset.</param>
/// <param name="seed">An integer value for seeding the random number generator.</param>
public RandomDataSet( List<int> dataset, int capacity, int seed )
{
this.Seed = seed;
this.Capacity = capacity;
// if the dataset to be added is larger than the current Capacity,
// resize this object to be 5 elements larger than the incoming dataset.
if (this.Capacity < dataset.Capacity)
{
this.Capacity = dataset.Capacity + 5;
}
foreach (int itm in dataset)
{
idataset.Add( itm );
}
}
public List<int> View
{
get
{
return idataset;
}
}
/// <summary>
/// Property used to set the seed value for the random number generator.
/// </summary>
public int Seed
{
get
{
return this.seed;
}
set
{
this.seed = value;
}
}
/// <summary>
/// Returns the number of items currently in the RandomDataset object.
/// </summary>
public int Count
{
get
{
return this.idataset.Count;
}
}
/// <summary>
/// Gets or sets the capacity of the RandomDataset object.
/// </summary>
public int Capacity
{
get
{
return this.idataset.Capacity;
}
set
{
this.idataset.Capacity = value;
}
}
/// <summary>
/// Gets or sets the number of computations performed during sorting.
/// </summary>
public int CompCount
{
get
{
return compCount;
}
set
{
this.compCount = value;
}
}
/// <summary>
/// Clear the contents of the RandomDataset object.
/// </summary>
public void Clear()
{
this.idataset.Clear();
}
/// <summary>
/// Fill the dataset with random numbers.
/// </summary>
public void GenerateRandom()
{
this.Clear();
Random rdmgen = new Random( this.Seed );
for (int i = 0 ; i < this.Capacity ; i++)
{
int thisnum = rdmgen.Next();
idataset.Add( thisnum );
}
}
/// <summary>
/// Fill the dataset with random numbers.
/// </summary>
/// <param name="min">Lowest value of random number.</param>
/// <param name="max">Highest value of random number.</param>
public void GenerateRandom( int min, int max )
{
this.Clear();
Random rdmgen = new Random( this.Seed );
for (int i = 0 ; i < this.Capacity ; i++)
{
int thisnum = rdmgen.Next( min, max );
idataset.Add( thisnum );
}
}
/// <summary>
/// Sort the RandomDataset object using the bubble sort algorithm.
/// </summary>
public void BubbleSort()
{
int temp, upper = this.Capacity - 1;
for (int outer = upper ; outer >= 1 ; outer--)
{
for (int inner = 0 ; inner <= outer - 1 ; inner++)
{
if ((int)idataset[inner] > idataset[inner + 1])
{
temp = idataset[inner];
idataset[inner] = idataset[inner + 1];
idataset[inner + 1] = temp;
compCount++;
}
}
}
}
/// <summary>
/// Sort the RandomDataset object using the selection sort algorithm.
/// </summary>
public void SelectionSort()
{
int min, temp, upper = this.Capacity - 1;
for (int outer = 0 ; outer <= upper ; outer++)
{
min = outer;
for (int inner = outer + 1 ; inner <= upper ; inner++)
{
if (idataset[inner] < idataset[min])
{
min = inner;
compCount++;
}
}
temp = idataset[outer];
idataset[outer] = idataset[min];
idataset[min] = temp;
}
}
/// <summary>
/// Sort the RandomDataset object using the insertion sort algorithm.
/// </summary>
public void InsertionSort()
{
int inner, temp, upper = this.Capacity - 1;
for (int outer = 1 ; outer <= upper ; outer++)
{
temp = idataset[outer];
inner = outer;
while (inner > 0 && idataset[inner - 1] >= temp)
{
idataset[inner] = idataset[inner - 1];
inner -= 1;
compCount++;
}
idataset[inner] = temp;
}
}
/// <summary>
/// Sort the RandomDataset using the List&lt;&gt;&#39;s built-in Sort() method.
/// </summary>
public void Sort()
{
idataset.Sort();
}
/// <summary>
/// Search the RandomDataset object using the sequential search algorithm.
/// </summary>
/// <param name="sValue">An integer value to search the RandomDataset object for.</param>
/// <returns>The index of the found integer, or -1 if no occurance found.</returns>
public int SeqSearch( int sValue )
{
for (int index = 0 ; index < idataset.Capacity ; index++)
{
compCount++;
if (idataset[index] == sValue)
{
if (index != 0)
{
int temp = idataset[index];
idataset[index] = idataset[index - 1];
idataset[index - 1] = temp;
}
return index;
}
}
return -1;
}
/// <summary>
/// First overload. Search the RandomDataset object using the sequential search algorithm.
/// </summary>
/// <param name="sValue">An integer value to search the RandomDataset object for.</param>
/// <param name="onum">The occurance we want to return.</param>
/// <returns>The index of the found integer, or -1 if no occurance found.</returns>
public int SeqSearch( int sValue, int onum )
{
int count = 0;
for (int index = 0 ; index < idataset.Capacity ; index++)
{
compCount++;
if (idataset[index] == sValue)
{
if (index != 0)
{
int temp = idataset[index];
idataset[index] = idataset[index - 1];
idataset[index - 1] = temp;
count++;
}
if (count == onum || index==0)
{
return index;
}
}
}
return -1;
}
/// <summary>
/// Search the RandomDataset object using the binary search algorithm.
/// </summary>
/// <param name="value">An integer value to search the RandomDataset object for.</param>
/// <param name="lower">The lower index limit.</param>
/// <param name="upper">The upper index limit.</param>
/// <returns>The index of the found integer, or -1 if no occurance found.</returns>
public int binSearch( int value, int lower, int upper )
{
int upperBound, lowerBound, mid;
upperBound = upper;
lowerBound = lower;
while (lowerBound <= upperBound)
{
compCount++;
mid = (upperBound + lowerBound) / 2;
if (idataset[mid] == value)
{
return mid;
}
else
{
if (value < idataset[mid])
{
upperBound = mid - 1;
}
else
{
lowerBound = mid + 1;
}
}
}
return -1;
}
/// <summary>
/// Search the RandomDataset object using the recursive binary search algorithm.
/// </summary>
/// <param name="value">An integer value to search the RandomDataset object for.</param>
/// <param name="lower">The lower index limit.</param>
/// <param name="upper">The upper index limit.</param>
/// <returns>The index of the found integer, or -1 if no occurance found.</returns>
public int RbinSearch( int value, int lower, int upper )
{
compCount++;
if (lower > upper)
{
return -1;
}
else
{
int mid = (int)(upper + lower) / 2;
if (value < idataset[mid])
{
return RbinSearch( value, lower, mid - 1 );
}
else if (value == idataset[mid])
{
return mid;
}
else
{
return RbinSearch( value, mid + 1, upper );
}
}
}// End RbinSearch().
}// end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment