-2
    private static void SortArray(List<int> nums)
    {
        int buffer = 0;

        for(int i = 0; i < nums.Count - 1; i++)
        {
            for(int j = 0; j < nums.Count - 1; j++)
            {
                if(nums[j] < nums[j + 1])
                {
                    buffer = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = buffer;
                }
            }
        }
    }

    private static bool IsPalindrome(string word)
    {
        int j = word.Length - 1;
        for (int i = 0; i < word.Length - 1 && j >= 0 && word[i] == word[j]; i++, j--);
        return j == 0;
    }

The two code above are simple algo one used to order numbers in an unordered array (bubble sort) and another to find the palindrome by checking the two edges of the array for matching.

Why does it start from n - 1 elements? i don't really understand the concept behind and sorry if this question might have been asked before.. but feels like i still can't get it.

Some other simple algorithms don't use the n - 1 elements concept in the for loop conditions, why is that so ?

So to make it clear, if the length of a string "HelloWorld" is 10, why substracting it to 10 - 1 since it won't cover all the lenght of the string therefore leaving "d" out.

theo
  • 11
  • 2
  • 3
    Because index of an array starts from 0, not from 1. Does this answer your question? [Why Array index start from '0'](https://stackoverflow.com/questions/16892862/why-array-index-start-from-0) – Karan Dhingra May 06 '22 at 17:43
  • @KaranDhingra And i agree with you, but if you check for example this method for even/odd numbers, it does not use the n - 1 elements in the loop. https://pastebin.com/xYsbRv7w – theo May 06 '22 at 17:45
  • If the loop body uses `+1`, and it does, you better make sure that doesn't reference an index outside of the range. – Damien_The_Unbeliever May 06 '22 at 17:48
  • @Damien_The_Unbeliever yes, in fact i tested tmyself the odd/even method without using the n - 1 as condition of the loop and it is not resulting into an index outside of the range. Basically this is what i don't get why most loops have n - 1 – theo May 06 '22 at 17:51
  • @Theo - whatever you thought you tested, no you didn't. If we have an array of 3 elements, valid indices are 0,1, and 2. But inner inner loop, j became 2 and j+1 was 3. – Damien_The_Unbeliever May 06 '22 at 17:55
  • @Damien_The_Unbeliever right i was talking about the even/odd method as showed in the pastebin a bit above on my second comment, just saying if indices start from 0 in an array, the n - 1 as the condition for the for loop was not introduced and it looks still working – theo May 06 '22 at 18:00
  • 1
    `for(int j = i + 1; j < nums.Count - 1; j++)` is enough, you can start from `i + 1`, not from `0` – Dmitry Bychenko May 06 '22 at 18:23

1 Answers1

2

If you take a look at the code you have lines where you have nums[j+1]. So if you are on the last element of the array those statements would give you a index out of range error. So you loop one less then the size of the array and since you compare against the next element in each iteration, the last element will be sorted correctly.