示例#1
0
        //InsertionSort
        public T[] InsertionSort(T[] arrSort)
        {
            StopWatch a = new StopWatch();

            a.Start();

            int intNumElements = arrSort.Length;
            int j;

            for (int i = 1; i < intNumElements; i++)
            {
                j = i;
                numComparisons++;
                while ((j > 0) && (arrSort[j - 1].CompareTo(arrSort[j])) > 0)
                {
                    T temp = arrSort[j - 1];
                    arrSort[j - 1] = arrSort[j];
                    arrSort[j]     = temp;
                    j--;
                    numSwaps++;
                    numComparisons++;
                }
            }

            a.Stop();
            time      = a.GetElapsedTime();
            numSorted = arrSort.Length;

            return(arrSort);
        }
示例#2
0
        //BubbleSort
        public T[] BubbleSort(T[] arrSort)
        {
            StopWatch a = new StopWatch();

            a.Start();

            T   temp;
            int n = arrSort.Count();

            for (int i = n - 1; i >= 0; i--)
            {
                for (int j = 1; j < n; j++)
                {
                    numComparisons++;
                    if (arrSort[j].CompareTo(arrSort[j - 1]) < 0)
                    {
                        temp           = arrSort[j - 1];
                        arrSort[j - 1] = arrSort[j];
                        arrSort[j]     = temp;
                        numSwaps++;
                    }
                }
            }

            a.Stop();
            time      = a.GetElapsedTime();
            numSorted = arrSort.Length;

            return(arrSort);
        }
示例#3
0
        public T[] HeapSort(T[] sortArray)
        {
            int n = sortArray.Length;

            //start timer
            StopWatch a = new StopWatch();

            a.Start();

            for (int i = (n / 2); i > 0; i--)
            {
                shiftDown(sortArray, i, n);
                numComparisons++;
            }

            do
            {
                SwapHeap(sortArray, 0, n - 1);
                n = n - 1;
                shiftDown(sortArray, 1, n);
                numComparisons++;
            }while (n > 1);

            //stop timer and calculate difference
            a.Stop();
            time      = a.GetElapsedTime();
            numSorted = sortArray.Length;

            //return array to gui
            return(sortArray);
        }
示例#4
0
        public T[] RadixSort(T[] sortArray)
        {
            //start timer
            StopWatch a = new StopWatch();

            a.Start();

            //create new array type int of length sortArray
            int[] intSortArray = new int[sortArray.Length];
            //convert from type T to type int and put into new array
            for (int k = 0; k < sortArray.Length; k++)
            {
                intSortArray[k] = Convert.ToInt32(sortArray[k]);
            }

            //call recursive method for sort and set result = to intSortArray
            intSortArray = RadixSortAux(intSortArray, 1);

            //convert back to type T and put back into sortArray
            for (int k = 0; k < intSortArray.Length; k++)
            {
                sortArray[k] = (T)Convert.ChangeType(intSortArray[k], typeof(T));
            }

            //stop timer and calculate difference
            a.Stop();
            time      = a.GetElapsedTime();
            numSorted = sortArray.Length;

            //return to gui
            return(sortArray);
        }
示例#5
0
        //QuickSort
        public T[] QuickSort(T[] arrSort)
        {
            StopWatch a = new StopWatch();

            a.Start();

            QSort(0, arrSort.Length - 1, arrSort);
            a.Stop();

            time      = a.GetElapsedTime();
            numSorted = arrSort.Length;
            return(arrSort);
        }
示例#6
0
        public T[] MergeSort(T[] sortArray)
        {
            //starts timer
            StopWatch a = new StopWatch();

            a.Start();

            //calls method to sort
            SortArrayMerge(0, sortArray.Length - 1, sortArray);

            //stops timer and calculates difference
            a.Stop();
            time      = a.GetElapsedTime();
            numSorted = sortArray.Length;

            return(sortArray);//returns array to gui
        }
示例#7
0
        //SelectionSort
        public T[] Selection(T[] sortArray)
        {
            int smallest; //index of smallest element

            //start timer
            StopWatch a = new StopWatch();

            a.Start();

            //loop through array
            for (int i = 0; i < sortArray.Length - 1; i++)
            {
                smallest = i;//first index of remaining array

                //loop to find index of smallest element
                for (int index = i + 1; index < sortArray.Length; index++)
                {
                    numComparisons++;

                    if (sortArray[index].CompareTo(sortArray[smallest]) < 0)
                    {
                        smallest = index;
                        numComparisons++;
                    }
                }

                //calls method to swap elements
                SwapSelect(i, smallest, sortArray);
            }
            numSwaps++;

            //stops timer and calculates difference
            a.Stop();
            time      = a.GetElapsedTime();
            numSorted = sortArray.Length;

            return(sortArray);//returns array to gui
        }
示例#8
0
        //BucketSort String
        public string[] BucketSort(string[] arrSort)
        {
            StopWatch a = new StopWatch();

            a.Start();

            string[,] Bucket = new string[27, arrSort.Length];
            int o = 0;

            while (o < 12)
            {
                for (int r = 0; r < 27; r++)
                {
                    for (int c = 0; c < arrSort.Length; c++)
                    {
                        Bucket[r, c] = "";
                    }
                }
                int j = 0;
                for (int b = 0; b < arrSort.Length; b++)
                {
                    arrSort[b] = arrSort[b].ToUpper();
                }
                foreach (string element in arrSort)
                {
                    char[] arrChar = element.ToCharArray();

                    if (arrChar.Length - (o + 1) < 0)
                    {
                        int placement = (int)arrChar[0] - 64;
                        Bucket[placement, j] = element;
                        j++;
                    }
                    else
                    {
                        int index     = arrChar.Length - (o + 1);
                        int placement = (int)arrChar[index] - 64;
                        Bucket[placement, j] = element;
                        j++;
                        numSwaps++;
                        numComparisons++;
                    }
                }
                int m = 0;
                for (int r = 0; r < 27; r++)
                {
                    for (int c = 0; c < arrSort.Length; c++)
                    {
                        if (Bucket[r, c] != "")
                        {
                            arrSort[m] = Bucket[r, c];
                            m++;
                        }
                    }
                }
                o++;
            }
            a.Stop();

            time = a.GetElapsedTime();

            numSorted = arrSort.Length;

            return(arrSort);
        }
示例#9
0
        //BucketSort Int
        public int[] BucketSort(int[] arrSort)
        {
            StopWatch a = new StopWatch();

            a.Start();

            if (arrSort.Count() == 0)
            {
                throw new ArgumentNullException();
            }

            else
            {
                int maxValue = arrSort[0];
                int minValue = arrSort[0];
                for (int i = 1; i < arrSort.Length; i++)
                {
                    if (arrSort[i].CompareTo(maxValue) > 0)
                    {
                        maxValue = arrSort[i];
                    }

                    if (arrSort[i].CompareTo(minValue) < 0)
                    {
                        minValue = arrSort[i];
                    }
                }

                List <int>[] bucket = new List <int> [maxValue - minValue + 1];

                for (int i = 0; i < bucket.Length; i++)
                {
                    bucket[i] = new List <int>();
                }

                for (int i = 0; i < arrSort.Length; i++)
                {
                    bucket[arrSort[i] - minValue].Add(arrSort[i]);
                    numSwaps++;
                    numComparisons++;
                }

                int k = 0;
                for (int i = 0; i < bucket.Length; i++)
                {
                    if (bucket[i].Count > 0)
                    {
                        for (int j = 0; j < bucket[i].Count; j++)
                        {
                            arrSort[k] = bucket[i][j];
                            k++;
                        }
                    }
                }
                a.Stop();
                time      = a.GetElapsedTime();
                numSorted = arrSort.Length;

                return(arrSort);
            }
        }