Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			arr[i], _ = strconv.Atoi(scanner.Text())
		}
		cnt := 0
		for i := n-1; i  > 0; i-- {
			for j := 0; j < i; j++ {
				if arr[j] > arr[j+1] {
					// log.Println(arr)
					cnt++
					arr[j], arr[j+1] = arr[j+1], arr[j]
				}
			}
		}
		// log.Println(arr)
		fmt.Println(cnt)
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;

int main(void){
	while(1){
		int n; cin >> n;
		if(n == 0)return 0;

		vector<int> v;
		for (int i = 0; i < n; ++i){
			int t; cin >> t;
			v.push_back(t);
		}

		int cnt = 0;
		for (int i = 0; i < n; ++i){
			for (int j = 0; j < n - i - 1; ++j){
				if(v[j] > v[j + 1]){
					swap(v[j], v[j + 1]);
					cnt++;
				}
			}
		}
		cout << cnt << endl;
	}
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

void solve() {
  int n;
  while (cin >> n, n) {
    vi a(n);
    rep(i, n) cin >> a[i];
    int totalSwapTime = 0;
    for (;;) {
      int st = 0;
      loop(i, 1, n) {
        if (a[i - 1] < a[i]) continue;
        swap(a[i - 1], a[i]);
        st++;
      }
      totalSwapTime += st;
      if (!st) break;
    }
    cout << totalSwapTime << endl;
  }
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define ll long long
//#define scanf scanf_s

int main()
{
	int n, a[1005];
	while (1)
	{
		cin >> n; if (n == 0)return 0;
		REP(i, n)cin >> a[i];
		int cnt = 0;
		REP(i, n-1)REP(j, n-1)
		{
			if (a[j] > a[j + 1])
			{
				swap(a[j], a[j + 1]);
				cnt++;
			}
		}
		cout << cnt << endl;

	}
	return 0;

}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<cstring>
using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(n) rep(i,n)
#define all(n) n.begin(),n.end()

const int MAXN = 101;

int a[MAXN];

int main()
{
    int n;
    while(cin >> n && n)
    {
        REP(n) cin >> a[i];
        int ans = 0;

        for(int j = n - 1; j > 0;j--)REP(j)
        {
            if(a[i] > a[i+1]){ swap(a[i], a[i+1]);ans++; }

        }
        cout << ans << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;

int main(){
  int n,i,j,k,c,a[1000000];

  while(1){
    cin>>n;
    if(n==0)break;
    for(i=0;i<n;i++){
      cin>>a[i];
    }
    k=0;
    for(i=0;i<n-1;i++){
      for(j=i;j<n;j++){
	if(a[i]>a[j]){
	  c=a[i];
	  a[i]=a[j];
	  a[j]=c;
	  k++;
	} 
      }
    }
    cout<<k<<endl;
  }
return 0;
}

C++: #include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {

    while(true){
        int n;
        cin >> n;
        if(n==0){
            break;
        }
        
        int array[100] = {0};
        for(int i=0;i<n;i++){
            cin >> array[i];
        }
        
        
        int count = 0;
        int i = 0;
        int j = n - 1;
        while(j>0){
            
            if(i == j){
                i = 0;
                j--;
            }
            else if(array[i] > array[i+1]){
                int t = array[i];
                array[i] = array[i+1];
                array[i+1] = t;
                i++;
                count++;
            }
            else{
                i++;
            }
            
        }
        
        cout << count << endl;
        
    }
    
    
    return 0;
}

C++: #include<iostream>
using namespace std;

int main(){
  int n,kazu,sort[1111111],cou,tmp;
  while(1){
    cou=0;
    cin >> n;
    if(n == 0) break;
    for(int i=0;i<n;i++){
      cin >> kazu;
      sort[i] = kazu;
    }
    for(int i=0;i<n;i++){
      for(int j=i+1;j<n;j++){
	if(sort[i] > sort[j]){
	  tmp = sort[i];
	  sort[i] = sort[j];
	  sort[j] = tmp;
	  cou++;
	}
      }
    }
    cout << cou << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


#define N (111)
int seg[N+1];

void add(int x,int v){
	for(int i = x ; i < N ; i+=i&-i){
		seg[i] += v;
	}
}

int get(int x){
	int ans = 0;
	for(int i = x ; i > 0 ; i -= i & -i){
		ans += seg[i];
	}
	return ans;
}

int a[101];

int main(){
	int n;
	while(cin >> n && n){
		vector<int> u;
		for(int i = 0 ; i < 101 ; i++) seg[i] = 0;
		
		for(int i = 0 ; i < n ; i++) cin >> a[i] , u.push_back(a[i]);
		sort(u.begin(),u.end());
		u.erase(unique(u.begin(),u.end()),u.end());
		for(int i = 0 ; i < n ; i++)
			a[i] = lower_bound(u.begin(),u.end(),a[i]) - u.begin() + 1;
		int ans = 0;
		for(int i = 0 ; i < n ; i++){
			add(a[i],1);
			ans += (i+1) - get(a[i]);  //a[i]より大きな値
		}
		cout << ans << endl;
	}
}

C++: #include <stdio.h>

int swap(int *x, int *y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;

	return (0);
}

int bsort(int *data, int last)
{
	int count = 0;
	for (int i = 0; i < last; i++){
		if (data[i] > data[i + 1]){
			swap(data + i, data + i + 1);
			count++;
		}
	}
	return (count);
}

int main()
{
	int array[128];
	int n, i;

	while (scanf("%d", &n), n){
		for (i = 0; i < n; i++){
			scanf("%d", array + i);
		}
		int sum = 0;
		for (i = 1; i < n; i++){
			sum += bsort(array, n - i);
		}

		printf("%d\n", sum);
	}

	return (0);
}

C++: #include <iostream>
#include <stdio.h>
using namespace std;
int main() {
 int n,i,j,k,d[100];
 while(cin >> n) {
	 if (n==0) break;
	 for (i=0;i<n;i++) cin >> d[i];
	 k=0;
	 for (i=1;i<n;i++) for (j=0;j<n-i;j++) if (d[j]>d[j+1]) { swap(d[j],d[j+1]); k++;} 
	 cout << k << endl;
 }
 return 0;
 }

C++: #include <iostream>
#include <algorithm>
using namespace std;

int num[1000001];

int BubbleSort(int n)
{	
	int k = 0, ct = 0;
	bool flag = true;
	
	do {
		flag = true;
		
		for (int i = 0; i < n - 1 - k; i++){
			if (num[i] > num[i + 1]){
				flag = false;
				swap(num[i], num[i + 1]);
				ct++;
			}
		}
		k++;
	}while (!flag);
	return (ct);
}

int main()
{
	int n;
	
	while (cin >> n, n){
		for (int i = 0; i < n; i++){
			cin >> num[i];
		}
		int ct = BubbleSort(n);
		cout << ct << endl;
	}
	return (0);
}