“Sometimes it is the people no one can imagine anything of who do the things no one can imagine.” ― Sir Alan Turing


ADS

ADS

Saturday, April 8, 2017

UVa 10327 Flip Sort Solution in C / C++


Source Code

#include<stdio.h>

int main() {
  int n, i, j, a[1050], ans, t;
  while (scanf("%d", & n) == 1) {
    ans = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", & a[i]);
    }
    for (i = 1; i < n; i++)
      for (j = n - 1; j >= i; j--)
        if (a[j - 1] > a[j]) {
          t = a[j - 1];
          a[j - 1] = a[j];
          a[j] = t;
          ans = ans + 1;
        }
    printf("Minimum exchange operations : %d\n", ans);
  }
  return 0;
}

No comments:

Post a Comment