You are viewing a single comment's thread. Return to all comments →
int main() { int n = parse_int(ltrim(rtrim(readline())));
char** a_temp = split_string(rtrim(readline())); int* a = malloc(n * sizeof(int)); for (int i = 0; i < n; i++) { int a_item = parse_int(*(a_temp + i)); *(a + i) = a_item; } // Write your code here int c=0,temp,s=0; for(int i=0;i<n-1;i++){ for(int j=0;j<n-1;j++){ if(a[j]>a[j+1]){ temp=a[j+1]; a[j+1]=a[j]; a[j]=temp; s++; } else { c++; } } if(c==1){ break; } } printf("Array is sorted in %d swaps.\n",s); printf("First Element: %d\n", a[0]); printf("Last Element: %d",a[n-1]); return 0;
}
Seems like cookies are disabled on this browser, please enable them to open this website
Day 20: Sorting
You are viewing a single comment's thread. Return to all comments →
int main() { int n = parse_int(ltrim(rtrim(readline())));
}