예제 #1
0
        // Selection sort with in List

        public void selectionSortList()
        {
            List <int> list = new List <int> {
                64, 25, 12, 22, 11
            };
            int length = list.Count;

            Console.WriteLine("Before appling Selection Sort :");
            foreach (var item in list)
            {
                Console.Write(item + " ");
            }
            Console.WriteLine("\n");

            for (int i = 0; i < length - 1; i++)
            {
                int minIndex = i;
                for (int j = i + 1; j < length; j++)
                {
                    if (list[j] < list[minIndex])
                    {
                        minIndex = j;
                    }
                }
                int temp = list[minIndex];
                list[minIndex] = list[i];
                list[i]        = temp;
            }
            PrintMethod.printList("Selection Sort", list);
        }
예제 #2
0
        public void insertionSortWithList()
        {
            List <int> list = new List <int> {
                12, 11, 13, 5, 6
            };
            int lengthCount = list.Count;

            for (int i = 1; i < lengthCount; ++i)
            {
                int key = list[i];
                int j   = i - 1;

                /* Move elements of arr[0..i-1], that are
                 * greater than key, to one position ahead
                 * of their current position */
                while (j >= 0 && list[j] > key)
                {
                    list[j + 1] = list[j];
                    j           = j - 1;
                }
                list[j + 1] = key;
            }
            PrintMethod.printList("Insertion Sort", list);
        }