0

I would like to know how to determine the efficiency of my array sorting algorithm. After researching big O notation I learned that the time complexity is polynomial of O(n^2) since the algorithm consists of 2 loops. Also, the space complexity is of O(1). However, I don't understand how to determine the efficiency.

public static void sortAnimalsArray(Animal[] animals)
{
    for (int i = 0; i < animals.Length; i++)//move row wise
    {
        for (int j = i + 1; j < animals.Length; j++)//move column wise
        {
            //first sort rowwise and considering each row then sort column wise 
            if (animals[i].Pos.letterX > animals[j].Pos.letterX || (animals[i].Pos.letterX == animals[j].Pos.letterX && animals[i].Pos.letterY > animals[j].Pos.letterY))
            {
                var temp = animals[i];
                animals[i] = animals[j];
                animals[j] = temp;
            }
        }
    }
} 
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
310094933
  • 59
  • 1
  • 5
  • 1
    2 loops does not *necessarily* mean O(n^2). What do you mean by "efficiency"? – Scott Hunter Jul 26 '20 at 18:14
  • Have you already read through the entire wikipedia article? https://en.wikipedia.org/wiki/Bubble_sort "its O(n2) complexity means that its efficiency decreases dramatically on lists of more than a small number of elements. Even among simple O(n2) sorting algorithms, algorithms like insertion sort are usually considerably more efficient." Does that already answer your question? – Fabian Bigler Jul 26 '20 at 18:15
  • @FabianBigler Yes, it does. I was confused because the explanations were using complexity and efficiency interchangeably. – 310094933 Jul 26 '20 at 18:28
  • @FabianBigler How would it affect the efficiency if there were 3 variables being sorted. There are 2 being sorted currently. I would assume the runtime would be slower. – 310094933 Jul 26 '20 at 19:19

0 Answers0