Esempio n. 1
0
        private void Heapify(int[] arr, int idx, int heapSize)
        {
            if (idx >= heapSize / 2)
            {
                return;
            }

            var left  = Left(idx);
            var right = Right(idx);

            var largest = idx;

            if (left < heapSize && arr[left] > arr[idx])
            {
                largest = left;
            }

            if (right < heapSize && arr[right] > arr[largest])
            {
                largest = right;
            }

            if (largest != idx)
            {
                BotterUtils.Swap(ref arr[largest], ref arr[idx]);
                Heapify(arr, largest, heapSize);
            }
        }
Esempio n. 2
0
        public void Sort(int[] arr)
        {
            int k     = 0;
            int left  = 0;
            int right = arr.Length - 1;

            while (left < right)
            {
                for (int i = right; i > left; i--)
                {
                    if (arr[i] < arr[i - 1])
                    {
                        BotterUtils.Swap(ref arr[i], ref arr[i - 1]);
                        k = i;
                    }
                }
                left = k;

                for (int i = left; i < right; i++)
                {
                    if (arr[i] > arr[i + 1])
                    {
                        BotterUtils.Swap(ref arr[i], ref arr[i + 1]);
                        k = i;
                    }
                }
                right = k;
            }
        }
Esempio n. 3
0
 public void Sort(int[] arr)
 {
     for (int i = 0; i < arr.Length - 1; i++)
     {
         for (int j = arr.Length - 1; j > i; j--)
         {
             if (arr[j] < arr[j - 1])
             {
                 BotterUtils.Swap(ref arr[j], ref arr[j - 1]);
             }
         }
     }
 }
Esempio n. 4
0
 public void Sort(int[] arr)
 {
     for (int i = 0; i < arr.Length - 1; i++)
     {
         for (int j = i + 1; j < arr.Length; j++)
         {
             if (arr[j] < arr[i])
             {
                 BotterUtils.Swap(ref arr[j], ref arr[i]);
             }
         }
     }
 }
Esempio n. 5
0
        public void Sort(int[] arr)
        {
            // build heap
            for (int i = arr.Length / 2 - 1; i >= 0; i--)
            {
                Heapify(arr, i, arr.Length);
            }

            // sort
            var heapSize = arr.Length;

            for (int i = arr.Length - 1; i > 0; i--)
            {
                BotterUtils.Swap(ref arr[i], ref arr[0]);
                heapSize--;
                Heapify(arr, 0, heapSize);
            }
        }
Esempio n. 6
0
        public void Sort(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                // find index of minimum element in unordered list
                var minIdx = i;
                for (int j = i + 1; j < arr.Length; j++)
                {
                    if (arr[j] < arr[minIdx])
                    {
                        minIdx = j;
                    }
                }

                // put minimum element to ordered list
                if (minIdx != i)
                {
                    BotterUtils.Swap(ref arr[minIdx], ref arr[i]);
                }
            }
        }
Esempio n. 7
0
        private int Partition(int[] m, int leftIdx, int rightIdx)
        {
            // get pivot as the right most element of the array
            var pivot = m[rightIdx];
            var i     = leftIdx;

            for (var j = leftIdx; j < rightIdx; j++)
            {
                if (m[j] <= pivot)
                {
                    BotterUtils.Swap(ref m[i], ref m[j]);

                    i++;
                }
            }

            var tm = m[rightIdx];

            m[rightIdx] = m[i];
            m[i]        = tm;

            return(i);
        }