0
What is the function of 'switched' and 'size-=1' in this bubble sort program? Can I just delete it if it's not important?
#include <stdio.h> void bubblesort(int a[], int size); int main(){ int arr[10]={1,2,3,4,5,6,7,8,9,10}; int i=0; printf("before:\n"); for(i=0;i<10;i++) printf("%d", arr[i]); printf("\n"); bubblesort(arr, 10) printf("after:\n"); for(i=0;i<10;i++) printf("%d", arr[i]); printf("\n"); return 0; } void bubblesort(int a[], int size){ int switched=1, hold=0, i=0, j=0; size -=1; for(i=0;i<size && switched; i++){ switched=0; for(j=0; j<size-i; j++) if(a[j]>a[j+1]){ switched=1; hold=a[j]; a[j]=a[j+1]; a[j+1]=hold;} }}
2 Answers
0
Bennett Post , I want to ask again:))
in the middle of for-looping is to show when the loop stops, what is the switched function for? why is it there?
for(i=0;i<size && switched; i++)
0
ooh, i see...
thankyou soooo much