# gettting different output in bubble sort in c

Solution for gettting different output in bubble sort in c
is Given Below:

Hey I am a newbie in dsa so I was learning bubble sort but my output is different please see my code below

output should be: `7,12,21,70,83`
my output is: `12,7,70,21,83`
which is incorrect

``````#include <stdio.h>

void bubble_sort(int duparray[], int num) {
int temp, i, j;

for (i = 0; i < num - 1; i++) { // i means number of passes
for (j = 0; j < num - 1 - i; j++) {
if (duparray[i] > duparray[i + 1]) {
temp = duparray[i];
duparray[i] = duparray[i + 1];
duparray[i + 1] = temp;
}
}
}
for (int i = 0; i < num; i++) {
printf("%d ", duparray[i]);
}
}

void display(int duparray[], int num) {
for (int i = 0; i < num; i++) {
printf("%d ", duparray[i]);
}
}

int main() {
int array = { 70, 12, 7, 83, 21 };
int n = 5;
display(array, n);
printf("n");
bubble_sort(array, n);
// display(array, n);

return 0;
}
``````

It seems the for loops should look like

``````for( i=0;i<num-1;i++){// i means number of passes
for( j=0;j<num-1-i;j++){
if(duparray[j]>duparray[j+1]){
temp=duparray[j];
duparray[j]=duparray[j+1];
duparray[j+1]=temp;

}
}
``````

That is use `j` and `j+1` as the indexes for the elements that are being compared and swapped. Note that `i` doesn’t change in the inner loop, so your code was just comparing the same two elements repeatedly.

If you will make the changes then the output will be

``````70 12 7 83 21
7 12 21 70 83
``````