Пример #1
0
        public TransationMetrics TransactionTest(int[] arr)
        {
            int i, j;

            int[] tmp = new int[arr.Length];
            for (int shift = 31; shift > -1; --shift)
            {
                j = 0;
                numOfChecks++;
                for (i = 0; i < arr.Length; ++i)
                {
                    numOfSwaps++;
                    bool move = (arr[i] << shift) >= 0;
                    if (shift == 0 ? !move : move)
                    {
                        arr[i - j] = arr[i];
                    }
                    else
                    {
                        tmp[j++] = arr[i];
                    }
                }
                Array.Copy(tmp, 0, arr, arr.Length - j, j);
            }
            Tests.TransationMetrics TMetrics = new TransationMetrics();
            bytesUsed = GC.GetTotalMemory(true);
            TMetrics.NumberOfChecks = numOfChecks;
            TMetrics.NumberOfChecks = numOfSwaps;
            TMetrics.BytesUsed      = bytesUsed;
            return(TMetrics);
        }
Пример #2
0
        public TransationMetrics TransactionTest(int[] array)
        {
            TransationMetrics TM = new TransationMetrics();
            int gap = array.Length / 2;

            while (gap > 0)
            {
                for (int i = 0; i < array.Length - gap; i++) //modified insertion sort
                {
                    NumOfChecks++;
                    int j   = i + gap;
                    int tmp = array[j];
                    while (j >= gap && tmp > array[j - gap])
                    {
                        NumOfSwaps++;
                        array[j] = array[j - gap];
                        j       -= gap;
                    }
                    array[j] = tmp;
                }
                if (gap == 2) //change the gap size
                {
                    gap = 1;
                }
                else
                {
                    gap = (int)(gap / 2.2);
                }
            }
            TM.BytesUsed      = GC.GetTotalMemory(true);
            TM.NumberOfChecks = NumOfChecks;
            TM.NumberOfSwaps  = NumOfSwaps;
            return(TM);
        }
Пример #3
0
        public TransationMetrics TransactionTest(int[] unsortedArray)
        {
            bool hasBeenSwitched = true;
            int  leftToSort      = unsortedArray.Length - 1;

            while (leftToSort > 0 && hasBeenSwitched)
            {
                hasBeenSwitched = false;
                for (int i = 0; i < leftToSort; i++)
                {
                    NumOfChecks++;
                    if (unsortedArray[i].CompareTo(unsortedArray[i + 1]) < 0)
                    {
                        hasBeenSwitched = true;
                        var temp = unsortedArray[i];
                        unsortedArray[i]     = unsortedArray[i + 1];
                        unsortedArray[i + 1] = temp;
                        NumOfSwaps++;
                    }
                }
                leftToSort--;
            }
            TransationMetrics metrics = new TransationMetrics();

            metrics.NumberOfChecks = NumOfChecks;
            metrics.NumberOfSwaps  = NumOfSwaps;
            metrics.BytesUsed      = GC.GetTotalMemory(true);
            return(metrics);
        }
        public TransationMetrics TransactionTest(int[] source, int left, int right, int value)
        {
            TransationMetrics TM = new TransationMetrics();

            while (left <= right)
            {
                int middle = (left + right) / 2;

                NumOfChecks++;
                if (source[middle] == value)
                {
                    TM.BytesUsed      = GC.GetTotalMemory(true);
                    TM.ItemFound      = true;
                    TM.NumberOfChecks = NumOfChecks;
                    return(TM);
                }
                else if (source[middle] > value)
                {
                    right = middle - 1;
                }
                else if (source[middle] < value)
                {
                    left = middle + 1;
                }
            }
            TM.BytesUsed      = GC.GetTotalMemory(true);
            TM.ItemFound      = true;
            TM.NumberOfChecks = NumOfChecks;
            return(TM);
        }
        public TransationMetrics TransactionTest(int[] arr)
        {        //pos_min is short for position of min
            int pos_min, temp;

            for (int i = 0; i < arr.Length - 1; i++)
            {
                pos_min = i;//set pos_min to the current index of array

                for (int j = i + 1; j < arr.Length; j++)
                {
                    NumOfChecks++;
                    if (arr[j] < arr[pos_min])
                    {
                        //pos_min will keep track of the index that min is in, this is needed when a swap happens
                        pos_min = j;
                    }
                }

                //if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
                if (pos_min != i)
                {
                    NumOfSwaps++;
                    temp         = arr[i];
                    arr[i]       = arr[pos_min];
                    arr[pos_min] = temp;
                }
            }
            TransationMetrics transMetrics = new TransationMetrics();

            transMetrics.NumberOfChecks = NumOfChecks;
            transMetrics.NumberOfSwaps  = NumOfSwaps;
            transMetrics.BytesUsed      = GC.GetTotalMemory(true);
            return(transMetrics);
        }
        public TransationMetrics TransactionTest(int[] unsortedArray, int start, int isize, int item)
        {
            TransationMetrics STM = new TransationMetrics();

            int N = unsortedArray.Length;

            if (start < 0)
            {
                STM.BytesUsed      = GC.GetTotalMemory(true);
                STM.ItemFound      = false;
                STM.NumberOfChecks = 0;
                return(STM);
            }

            for (int i = start; i < N; i++)
            {
                if (unsortedArray[i] == item)
                {
                    STM.BytesUsed      = GC.GetTotalMemory(true);
                    STM.ItemFound      = true;
                    STM.NumberOfChecks = i;
                    return(STM);
                }
            }

            STM.BytesUsed      = GC.GetTotalMemory(true);
            STM.ItemFound      = false;
            STM.NumberOfChecks = N;
            return(STM);
        }
        public TransationMetrics TransactionTest(int[] inputArray, int min, int max, int key)
        {
            TransationMetrics TM = new TransationMetrics();

            while (min <= max)
            {
                NumOfChecks++;
                int mid = (min + max) / 2;
                if (key == inputArray[mid])
                {
                    TM.ItemFound      = false;
                    TM.NumberOfChecks = NumOfChecks;
                    TM.BytesUsed      = GC.GetTotalMemory(true);
                    return(TM);
                }
                else if (key < inputArray[mid])
                {
                    max = mid - 1;
                }
                else
                {
                    min = mid + 1;
                }
            }
            TM.ItemFound      = true;
            TM.NumberOfChecks = NumOfChecks;
            TM.BytesUsed      = GC.GetTotalMemory(true);
            return(TM);
        }
        TransationMetrics BottomUpMergeSortTM(int[] A, int n)
        {
            TransationMetrics TM = new TransationMetrics();

            int[] B = new int[A.Length];
            // Each 1-element run in A is already "sorted".
            // Make successively longer sorted runs of length 2, 4, 8, 16... until whole array is sorted.
            for (int width = 1; width < n; width = 2 * width)
            {
                NumOfChecks++;
                // Array A is full of runs of length width.
                for (int i = 0; i < n; i = i + 2 * width)
                {
                    // Merge two runs: A[i:i+width-1] and A[i+width:i+2*width-1] to B[]
                    // or copy A[i:n-1] to B[] ( if(i+width >= n) )
                    BottomUpMerge(A, i, getMin(i + width, n), getMin(i + 2 * width, n), B);
                }
                // Now work array B is full of runs of length 2width.
                // Copy array B to array A for next iteration.
                // A more efficient implementation would swap the roles of A and B.
                CopyArray(B, A, n);
                // Now array A is full of runs of length 2width.
            }
            TM.BytesUsed      = GC.GetTotalMemory(true);
            TM.NumberOfChecks = NumOfChecks;
            TM.NumberOfSwaps  = NumOfSwaps;
            return(TM);
        }
        public TransationMetrics TransactionTest(int[] unsortedArray)
        {
            TransationMetrics TM = new TransationMetrics();

            MergeSortTM(unsortedArray);
            TM.BytesUsed      = GC.GetTotalMemory(true);
            TM.NumberOfChecks = NumOfChecks;
            TM.NumberOfSwaps  = NumOfSwaps;
            return(TM);
        }
Пример #10
0
        public TransationMetrics TransactionTest(int[] unsortedArray)
        {
            TransationMetrics TM = new TransationMetrics();

            var nameList = new List <int>();

            nameList.AddRange(unsortedArray);
            TM.NumberOfChecks--;
            nameList.Sort();
            TM.NumberOfSwaps--;
            TM.BytesUsed = GC.GetTotalMemory(true);
            return(TM);
        }
        static TransationMetrics QuickSortTM(int[] elements, TransationMetrics TM)
        {
            Random rand = new Random();

            if (elements.Count() < 2)
            {
                TM.BytesUsed = GC.GetTotalMemory(true);
                return(TM);
            }
            var pivot   = rand.Next(elements.Count());
            var val     = elements[pivot];
            var lesser  = new List <int>();
            var greater = new List <int>();

            for (int i = 0; i < elements.Count(); i++)
            {
                TM.NumberOfChecks++;
                if (i != pivot)
                {
                    if (elements[i].CompareTo(val) < 0)
                    {
                        TM.NumberOfSwaps++;
                        lesser.Add(elements[i]);
                    }
                    else
                    {
                        TM.NumberOfSwaps++;
                        greater.Add(elements[i]);
                    }
                }
            }

            var merged  = QuickSort(lesser.ToArray());
            var lmerged = merged.ToList();

            lmerged.Add(val);
            lmerged.AddRange(QuickSort(greater.ToArray()));
            TM.BytesUsed = GC.GetTotalMemory(true);
            return(TM);
        }
        public TransationMetrics TransactionTest(int[] array)
        {
            for (int i = 1; i < array.Length; i++)
            {
                int j = i;
                NumOfChecks++;
                while ((j > 0) && (array[j].CompareTo(array[j - 1]) < 0))
                {
                    NumOfSwaps++;
                    int k    = j - 1;
                    var temp = array[k];
                    array[k] = array[j];
                    array[j] = temp;

                    j--;
                }
            }
            TransationMetrics TMetrics = new TransationMetrics();

            TMetrics.NumberOfChecks = NumOfChecks;
            TMetrics.NumberOfSwaps  = NumOfSwaps;
            TMetrics.BytesUsed      = GC.GetTotalMemory(true);
            return(TMetrics);
        }
        public TransationMetrics TransactionTest(int[] unsortedArray)
        {
            TransationMetrics TM = new TransationMetrics();

            return(QuickSortTM(unsortedArray, TM));
        }
        public TransationMetrics TransactionTest(int[] unsortedArray)
        {
            TransationMetrics TM = BottomUpMergeSortTM(unsortedArray, (unsortedArray.Length));

            return(TM);
        }