示例#1
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
        }
示例#2
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);
        }
示例#3
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);
            }
        }