#inlcude<stdio.h> int main() { int arr[30],num,i,j,k,temp,l=0; printf("Enter the number of elements :\n"); scanf("%d",&num); for(i=1;i<=num;i++) { printf("Enter element %d\n",i); scanf("%d",&arr[i]); } for(k=1;k<=num;k++) {if(arr[k]>arr[k+1]) l++;} if(l!=0) { for(i=2;i<=num;i++) {l=0; for(k=1;k<=num;k++) { if(arr[k]>arrk+1]) l++;} if(l!=0) {for(j=1;j<num;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } printf("\nAfter pass %d elements are:",i-1); for(k=1;k<=num;k++) printf("%d",arr[k]);} else break; } } printf("\nsorted list is:\n"); for(k=1;k<=num;k++) printf("%d ",arr[k]); retrun 0; }
This is the Bubble sort program. My query is I need to stop my process if I find my list is sorted in any intermediate point. I even did that... But still there is some problem with this program. It's not getting accepted in portal, It triggers as the program is "Wrong Answer". I guess there may be mistakes in finding out the intermediate point. Help me out to figure out this..