Example #1
0
        public static void QuickSort(MyDataArray arr, int left, int right)
        {
            int i = left, j = right;
            var pivot = arr[(left + right) / 2];

            /* partition */
            while (i <= j)
            {
                while (arr[i] < pivot)
                {
                    i++;
                }
                while (arr[j] > pivot)
                {
                    j--;
                }
                if (i <= j)
                {
                    arr.Swap(i, j);
                    i++;
                    j--;
                }
            }

            /* recursion */
            if (left < j)
            {
                QuickSort(arr, left, j);
            }
            if (i < right)
            {
                QuickSort(arr, i, right);
            }
        }
Example #2
0
        public static void Test_Array_List(int seed)
        {
            Console.WriteLine("\n Selection sort ");
            int         n       = 12;
            MyDataArray myarray = new MyDataArray(n, seed);

            Console.WriteLine("\n ARRAY \n");
            myarray.Print(n);
            SelectionSort(myarray);
            myarray.Print(n);
            MyDataList mylist = new MyDataList(n, seed);

            Console.WriteLine("\n LIST \n");
            mylist.Print(n);
            mylist.SelectionSort();
            mylist.Print(n);


            Console.WriteLine("\n\n\n Quick sort ");
            myarray = new MyDataArray(n, seed);
            Console.WriteLine("\n ARRAY \n");
            myarray.Print(n);
            QuickSort(myarray, 0, myarray.Length - 1);
            myarray.Print(n);
            mylist = new MyDataList(n, seed);
            Console.WriteLine("\n LIST \n");
            mylist.Print(n);
            mylist.setHeadNode(MyDataList.MyLinkedListNode.QSort(mylist.getHeadNode()));
            mylist.Print(n);
        }
Example #3
0
        public static void SelectionSort(MyDataArray items)
        {
            for (int j = 0; j < items.Length - 1; j++)
            {
                int iMin = j;
                for (int i = j + 1; i < items.Length; i++)
                {
                    if (items[i] < items[iMin])
                    {
                        iMin = i;
                    }
                }

                if (iMin != j)
                {
                    items.Swap(j, iMin);
                }
            }
        }