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<bits/stdc++.h>
using namespace std;

int BIT[200];
int sum(int a) {
	int b = 0;
	while (a > 0) {
		b += BIT[a];
		a -= a&-a;
	}
	return b;
}
void add(int a) {
	while (a < 110) {
		BIT[a] ++;
		a += a&-a;
	}
}
int main() {
	int a;
	while (cin >> a, a) {
		memset(BIT, 0, sizeof(BIT));
		int c = 0;
		for (int b = 0; b < a; b++) {
			int d;
			scanf("%d", &d);
			c += b - sum(d);
			add(d);
		}
		cout << c << endl;
	}
}

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

int main(void){
	int n;
	while(cin>>n,n){
		vector<int> sort;
		int inp , cnt = 0;
		for(int i = 0 ; i < n ; i ++){
			cin>>inp;
			sort.push_back(inp);
		}
		for(int i = 0 ; i < n - 1 ; i ++)
			for(int j = i ; j < n ; j ++){
				if(sort[i] > sort[j]){
					int work = sort[i];
					sort[i] = sort[j];
					sort[j] = work;
					cnt ++;
				}
			}
			
		cout<< cnt <<endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include <cassert>

using namespace std;

int N, A[100];
int W[100];

int merge_and_count(int l,int r){
	if (l + 1 >= r)return 0;
	if (l + 2 == r){
		if (A[l] <= A[l + 1])return 0;
		swap(A[l], A[l + 1]);
		return 1;
	}
	int m = (l + r) / 2;
	int cl = merge_and_count(l, m);
	int cr = merge_and_count(m, r);
	int c = 0;
	int i = 1, j = m;
	int k = 1;
	while (i < m && j < r){
		if (A[i] <= A[j])W[k++] = A[i++];
		else{
			W[k++] = A[j++];
			c += j-k-1;
		}
	}
	while (i < m)W[k++] = A[i++];
	while (j < r)W[k++] = A[j++];
	assert(k == r);
	return cl + cr + c;
}

int main()
{
	cin >> N;
	for (int n = 0; n < N; n++){
		cin >> A[n];
	}
	cout << merge_and_count(0, N) << 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] = (10/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<iostream>
using namespace std;

int a[102],n;

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		int ans=0;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		for(int i=0;i<n-1;i++){
			for(int j=i+1;j<n;j++){
				if(a[i]>a[j]){
					ans++;
					swap(a[i],a[j]);
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

C++: #include <stdio.h>
int main (void){
    int a,c,i,j,n;
    for(;;){
        scanf("%d",&n);
        if(n==0)break;
        int b[101]={},ans=0;
        for(i=0;i<n;i++){
            scanf("%d",&a);
            b[i]=a;
        }
        for(i=1;i<n;i++){
            for(j=1;j<n;j++){
                if(b[j-1]>=b[j]){
                    if(b[j-1]>b[j]){
                        ans++;
                    }
                    c=b[j-1];
                    b[j-1]=b[j];
                    b[j]=c;
                }
            }
        }
        printf("%d\n",ans);
    }
return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>

using namespace std;
const int dx[] = {1,0,-1,0} , dy[] = {0,1,0,-1};
#define INF 999999
int main(){
    int n;
    while(scanf("%d",&n),n){
        int so[128]={0};
        for(int i = 0; i < n; i++)scanf("%d",&so[i]);

        int cnt = 0;
        for(int i = 0; i < n; i++){
            for(int j = i; j < n; j++){
                if(so[i] > so[j]){
                    swap(so[i],so[j]);
                    cnt++;
                }
            }
        }
        printf("%d\n",cnt);
    }
	return 0;
}

C++: #include<iostream>
#include<vector>

using namespace std;

void BubbleSort(vector<int>);
void swap(int&, int&);

int main(){
  int i, n, x;
  vector<int> data;

  while(1){
    cin >> n;
    if(n == 0) break;

    for(i=0; i<n; i++){
      cin >> x;
      data.push_back(x);
    }

    BubbleSort(data);
    data.clear();
  }

  return 0;
}

void BubbleSort(vector<int> data){
  int i, j, count=0;

  for(i=0; i<data.size(); ++i){
    for(j=0; j<data.size()-i-1; ++j){
      if(data[j] > data[j+1]){
	swap(data[j], data[j+1]);
	++count;
      }
    }
  }

  cout << count << endl;
}

void swap(int& x, int& y){
  int copy;
  copy = x;
  x = y;
  y = copy;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int main()
{
	int n;
	while(cin >> n && n)
	{
		vector<int> a;
		int temp,count = 0;

		rep(i,n)
		{
			cin >> temp;
			a.push_back(temp);
		}

		rep(i,n)
		{
			REP(j,i+1,n)
			{
				if(a[i] > a[j])
				{
					count++;
					swap(a[i],a[j]);
				}
			}
		}
		cout << count << endl;
	}
	return 0;
}

C++: //16
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    int a[100];
    for(int i=0;i<n;i++){
      cin>>a[i];
    }
    int ans=0;
    bool up;
    do{
      up=false;
      for(int i=0;i<n-1;i++){
	if(a[i]>a[i+1]){
	  ans++;
	  up=true;
	  swap(a[i],a[i+1]);
	}
      }
    }while(up);
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <iostream>
#define MAX 10

using namespace std;

int num[MAX];

int bubble_sort(int n){

  int cnt = 0;

  for( int i = 0 ; i < n - 1 ; i++ ){
    for( int j = n - 1 ; j > i ; j-- ){

      if( num[j - 1] > num[j] ){

	int temp = num[j - 1];
	num[j - 1] = num[j];
	num[j] = temp;

	cnt++;

      }
    }
  }
  return cnt;
}


int main(void){

  int n;
  while( cin >> n , n ){

    for( int i = 0 ; i < MAX ; i++ ) num[i] = 0;

    for( int i = 0 ; i < n ; i++ ){
      cin >> num[i];
    }

    cout << bubble_sort(n) << endl;
  }
  return 0;
}

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

const int N = 100;
int bit[N+10];

int sum(int i){
	int s = 0;
	while(i > 0){
		s += bit[i];
		i -= i & -i;
	}
	return s;
}

void add(int i, int x){
	while(i <= N){
		bit[i] += x;
		i += i & -i;
	}
}

vector<int> compress(vector<int> v){
	map<int, int> mp;
	for(int i=0;i<v.size();i++)mp[v[i]] = 0;

	int id = 1;
	for(map<int,int>::iterator it=mp.begin(); it != mp.end(); it++){
		it->second = id++;
	}

	for(int i=0;i<v.size();i++)v[i] = mp[v[i]];

	return v;
}


int main(){
	int n;
	while(cin >> n && n){
		memset(bit, 0, sizeof(bit));

		vector<int> v(n);
		for(int i=0;i<n;i++)cin >> v[i];

		v = compress(v);

		int ans = 0;
		for(int i=n-1;i>=0;i--){
			ans += sum(v[i] - 1);
			add(v[i], 1);
		}

		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
	int n;
	while (cin >> n){
		if (n == 0)return 0;
		int l[101];
		int sum = 0;
		for (int i = 0; i < n; i++){
			int a; cin >> a;
			l[i] = a;
		}
		for (int i = n; i > 0; i--){
			for (int j = 0; j < n - 1; j++) {
				if (l[j] > l[j + 1]){
					swap(l[j], l[j + 1]);
					sum++;
				}
			}
		}
		cout << sum << endl;
	}
}