Bubble sort doubt

In inner loop,how we can derive n-1-iteration.
Pls explain.

hi @vatsal50, so this is your standard bubble sort code:

void bubbleSort(int arr[], int n)  
{  
    int i, j;  
    for (i = 0; i < n-1; i++)      
      
    // Last i elements are already in place  
    for (j = 0; j < n-i-1; j++)  
        if (arr[j] > arr[j+1])  
            swap(&arr[j], &arr[j+1]);  
}

in bubble sort , what happens is by adjacent comparisons and swap we place the largest element to its correct position. if we have n elements then after 1st iteration we will have the largest element occupy the nth position so we only need to iterate for n-1 element in second iteration ,n-2 in third and so on ,

for the outer loop we do n-1 iterations as if n-1 elements are already in correct positions then nth element will automatically in correct position in a sorted array.