C# Sample: RandomDataSet Class

Filed under: Code Comments: 0

This is a class I had to code for a project to demonstrate a handful of sort and search algorithms. It is a self-contained class that can be used to build a List of integers. You could easily modify this class to manipulate pretty much any Collection.

RandomDataSet.cs

Here's the code:

    class RandomDataSet
    {
        private int seed, compCount;
        private List<int> idataset = new List<int>();
 
        /// <summary>
        /// Default constructor.
        /// </summary>
        public RandomDataSet()
        {
            this.Seed = 0;
        }
 
        /// <summary>
        /// Overloaded constructor.
        /// </summary>
        /// <param name="seed">An integer value for seeding the random number generator.</param>
        public RandomDataSet( int seed )
        {
            this.Seed = seed;
        }
 
        /// <summary>
        /// Overloaded constructor.
        /// </summary>
        /// <param name="dataset">A integer List&lt;&gt; dataset.</param>
        public RandomDataSet( List<int> dataset )
            : this()
        {
            // 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 );
            }
        }
 
        /// <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 )
        {
            this.Capacity = capacity;
        }
 
        /// <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( dataset, capacity )
        {
            this.Seed = seed;
        }
 
        public List<int> View
        {
            get
            {
                return idataset;
            }
 
        }
 
        /// <summary>
        /// Property used to set the seed value for the random number generator.nn 
        /// Accepts an integer value.
        /// </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;
            }
        }
 
        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++;
                    }
                }
                //this.DisplayElements();
            }
        }
 
        /// <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 )
        {
            int index;
            for (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 index, count = 0;
            for (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;
                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

Download

Click on this link to download the source code for this class: Download the code