Bubble Sort Code

I am attaching my code below, I am giving input of n elements but the code is taking input of n+1 element and assigning a garbage value

//////////////////////
#include
using namespace std;
void bubble_sort(int a[], int n){
//n-1 iterations to move n-1 elements to the end
for (int itr=1;itr<=n-1; itr++){
//pairwise swapping in the unsorted part of the array
for(int j=0;j<=(n-itr-1);j++){
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
}
}
}
}

int main(){
int n,key;
cout<<"Enter the number of elements : ";
cin>>n;
int a[1000];
cout<<"Enter the elements : “<<endl;
for (int i=0; i<=n;i++){ //Input to an array
cin>>a[i];
}
bubble_sort(a,n);
for (int i=0;i<=n;i++){
cout<<a[i]<<” ";
}
return 0;
}

Please share the link of Code

How to Share Link of Code??

  1. paste your code at https://ide.codingblocks.com/

  2. click->file->save

  3. after a link will be generated at top in place of url something like: https://ide.codingblocks.com/s/12345

  4. share this link

Here is the link

the only mistake you have done is while taking input and it printing the elements

for (int i=0;i<=n;i++){
        cout<<a[i]<<" ";
    }

correct one is

for (int i=0;i<n;i++){
        cout<<a[i]<<" ";
    }

loop should run till i<n; because you are starting i from 0