void Sort(ref StudentsList students)
        {
            int s;
            int n = 0;

            while (true)
            {
                s = 0;
                n++;
                for (int i = students.Students.Count; i > 1; i--)
                {
                    if (checkIfSmaller(students.Students[i - 1], students.Students[i - 2]))
                    {
                        s++;
                        Student a, b;
                        a = students.Students[i - 1];
                        b = students.Students[i - 2];

                        students.Students[i - 1] = b;
                        students.Students[i - 2] = a;
                    }
                }
                if (s == 0)
                {
                    break;
                }
            }
        }
        void Sort(StudentsList studentsList, int left, int right)
        {
            var pivot = studentsList.Students[(left + right) / 2];


            StudentsList lesserThanPivot = new StudentsList();

            StudentsList biggerThanPivot = new StudentsList();

            for (int i = 0; i < studentsList.Students.Count; i++)// pętla która wypełnia obie tablice w stosunku do pivota
            {
                if ((left + right) / 2 == i)
                {
                    continue;
                }

                if (studentsList.Students[i].Age < pivot.Age)
                {
                    lesserThanPivot.Students.Add(studentsList.Students[i]);
                }
                else
                {
                    biggerThanPivot.Students.Add(studentsList.Students[i]);
                }
            }


            if (lesserThanPivot.Students.Count == 1)                            //warunek do tablicy elementów mniejszych od pivota
            {
                sorted.Students.Add(lesserThanPivot.Students[0]);               //jeżeli w talbicy jest jeden element to dodaj do posortowanych
                sorted.Students.Add(studentsList.Students[(left + right) / 2]); //a potem dodaj samego pivota
            }
            else if (lesserThanPivot.Students.Count > 1)                        //jeżeli elementów w tablicy mniejszych od pivota jest więcej
            {
                Sort(lesserThanPivot, 0, lesserThanPivot.Students.Count);       //to sortuj je dalej
                biggerThanPivot.Students.Add(pivot);                            // a pivota wrzuć do tablicy większych żeby brał udział w dalszym sortowaniu
            }
            else if (lesserThanPivot.Students.Count == 0)                       //jeżeli nie ma elemntów mniejszych od pivota to dodaj samego pivota
            {
                sorted.Students.Add(studentsList.Students[(left + right) / 2]);
            }


            if (biggerThanPivot.Students.Count > 1)                       //jeżeli elementów większych od pivota jest więcej niż jeden
            {
                Sort(biggerThanPivot, 0, biggerThanPivot.Students.Count); // to je posortuj
            }

            else if (biggerThanPivot.Students.Count == 1)         // jak jest jeden
            {
                sorted.Students.Add(biggerThanPivot.Students[0]); // to dodaj go do listy
            }
        }
예제 #3
0
        static void Main(string[] args)
        {
            StudentsList newClass     = new StudentsList(10);
            StudentsList bubblesorted = new StudentsList();
            StudentsList quicksorted  = new StudentsList();

            foreach (Student s in newClass.Students)
            {
                Console.WriteLine(s.Age);
                bubblesorted.Students.Add(s);
                quicksorted.Students.Add(s);
            }


            Console.WriteLine(".");


            BubbleSorting aaa = new BubbleSorting(ref bubblesorted);

            foreach (Student s in bubblesorted.Students)
            {
                Console.WriteLine(s.Age);
            }

            QuickSort bbb = new QuickSort(ref quicksorted);


            Console.WriteLine(".");

            foreach (Student s in bubblesorted.Students)
            {
                Console.WriteLine(s.Age);
            }



            Console.ReadKey();
        }
 public BubbleSorting(ref StudentsList ts)
 {
     Sort(ref ts);
 }
 public QuickSort(ref StudentsList ts)
 {
     Sort(ts, 0, ts.Students.Count);
 }