Exemplo n.º 1
0
 public static void InsertionSort(ISortable4 sortable, int start, int end)
 {
     for (var i = start + 1; i < end; i++)
     {
         for (var j = i; j > start && sortable.Compare(j - 1, j) > 0; j--)
         {
             Swap(sortable, j - 1, j);
         }
     }
 }
Exemplo n.º 2
0
 private static int MiddleValueIndex(ISortable4 sortable, int a, int b, int c)
 {
     if (sortable.Compare(a, b) < 0)
     {
         if (sortable.Compare(b, c) < 0)
         {
             return(b);
         }
         else
         {
             if (sortable.Compare(a, c) < 0)
             {
                 return(c);
             }
             else
             {
                 return(a);
             }
         }
     }
     else
     {
         if (sortable.Compare(b, c) > 0)
         {
             return(b);
         }
         else
         {
             if (sortable.Compare(a, c) > 0)
             {
                 return(c);
             }
             else
             {
                 return(a);
             }
         }
     }
 }
Exemplo n.º 3
0
		public static void Qsort(ISortable4 sortable, int start, int end)
		{
			int length = end - start;
			int middle = start + length / 2;
			if (length > 7)
			{
				int bottom = start;
				int top = end - 1;
				if (length > 40)
				{
					length /= 8;
					bottom = MiddleValueIndex(sortable, bottom, bottom + length, bottom + (2 * length
						));
					middle = MiddleValueIndex(sortable, middle - length, middle, middle + length);
					top = MiddleValueIndex(sortable, top - (2 * length), top - length, top);
				}
				middle = MiddleValueIndex(sortable, bottom, middle, top);
			}
			int a;
			int b;
			int c;
			int d;
			a = b = start;
			c = d = end - 1;
			while (true)
			{
				while (b <= c && sortable.Compare(b, middle) <= 0)
				{
					if (sortable.Compare(b, middle) == 0)
					{
						middle = NewPartionIndex(middle, a, b);
						Swap(sortable, a++, b);
					}
					b++;
				}
				while (c >= b && sortable.Compare(c, middle) >= 0)
				{
					if (sortable.Compare(c, middle) == 0)
					{
						middle = NewPartionIndex(middle, c, d);
						Swap(sortable, c, d--);
					}
					c--;
				}
				if (b > c)
				{
					break;
				}
				middle = NewPartionIndex(middle, b, c);
				Swap(sortable, b++, c--);
			}
			length = Math.Min(a - start, b - a);
			Swap(sortable, start, b - length, length);
			length = Math.Min(d - c, end - 1 - d);
			Swap(sortable, b, end - length, length);
			length = b - a;
			if (length > 0)
			{
				Sort(sortable, start, start + length);
			}
			length = d - c;
			if (length > 0)
			{
				Sort(sortable, end - length, end);
			}
		}
Exemplo n.º 4
0
		private static int MiddleValueIndex(ISortable4 sortable, int a, int b, int c)
		{
			if (sortable.Compare(a, b) < 0)
			{
				if (sortable.Compare(b, c) < 0)
				{
					return b;
				}
				else
				{
					if (sortable.Compare(a, c) < 0)
					{
						return c;
					}
					else
					{
						return a;
					}
				}
			}
			else
			{
				if (sortable.Compare(b, c) > 0)
				{
					return b;
				}
				else
				{
					if (sortable.Compare(a, c) > 0)
					{
						return c;
					}
					else
					{
						return a;
					}
				}
			}
		}
Exemplo n.º 5
0
		public static void InsertionSort(ISortable4 sortable, int start, int end)
		{
			for (int i = start + 1; i < end; i++)
			{
				for (int j = i; j > start && sortable.Compare(j - 1, j) > 0; j--)
				{
					Swap(sortable, j - 1, j);
				}
			}
		}
Exemplo n.º 6
0
        public static void Qsort(ISortable4 sortable, int start, int end)
        {
            var length = end - start;
            var middle = start + length / 2;

            if (length > 7)
            {
                var bottom = start;
                var top    = end - 1;
                if (length > 40)
                {
                    length /= 8;
                    bottom  = MiddleValueIndex(sortable, bottom, bottom + length, bottom + (2 * length
                                                                                            ));
                    middle = MiddleValueIndex(sortable, middle - length, middle, middle + length);
                    top    = MiddleValueIndex(sortable, top - (2 * length), top - length, top);
                }
                middle = MiddleValueIndex(sortable, bottom, middle, top);
            }
            int a;
            int b;
            int c;
            int d;

            a = b = start;
            c = d = end - 1;
            while (true)
            {
                while (b <= c && sortable.Compare(b, middle) <= 0)
                {
                    if (sortable.Compare(b, middle) == 0)
                    {
                        middle = NewPartionIndex(middle, a, b);
                        Swap(sortable, a++, b);
                    }
                    b++;
                }
                while (c >= b && sortable.Compare(c, middle) >= 0)
                {
                    if (sortable.Compare(c, middle) == 0)
                    {
                        middle = NewPartionIndex(middle, c, d);
                        Swap(sortable, c, d--);
                    }
                    c--;
                }
                if (b > c)
                {
                    break;
                }
                middle = NewPartionIndex(middle, b, c);
                Swap(sortable, b++, c--);
            }
            length = Math.Min(a - start, b - a);
            Swap(sortable, start, b - length, length);
            length = Math.Min(d - c, end - 1 - d);
            Swap(sortable, b, end - length, length);
            length = b - a;
            if (length > 0)
            {
                Sort(sortable, start, start + length);
            }
            length = d - c;
            if (length > 0)
            {
                Sort(sortable, end - length, end);
            }
        }