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>

using namespace std;

int main(){

  int n;

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

    int data[n];
    int sum = 0;

    for(int i = 0; i < n; i++){
      cin >> data[i];
    }
    int tmp;
    for(int j = 0; j < n; j++){
      for(int i = 0; i < n-1-j; i++){
	if(data[i] > data[i+1]){
	  tmp = data[i];
	  data[i] = data[i+1];
	  data[i+1] = tmp;
	  sum++;
	}
      }
    }

    cout << sum << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <algorithm>
int A[999999], W[999999];
long long merge_and_count(int l, int r) { // range [l,r)
	if (l+1 >= r) return 0; // empty
	if (l+2 == r) { // [l,r) == [l,l+1] 要素2つだけ
		if (A[l] <= A[l+1]) return 0; // 逆転はなし
		std::swap(A[l], A[l+1]);
		return 1; // 逆転一つ
	}
	int m = (l+r)/2; // [l,r) == [l,m) + [m,r)
	long long cl = merge_and_count(l, m); // 左半分を再帰的に数える
	long long cr = merge_and_count(m, r); // 右半分を再帰的に数える
	long long c = 0; // 左と右を混ぜるときの逆点数
	int i=l, j=m; // iは[l,m)を動き, jは[m,r)を動いて、
	int k=l; // 小さいものから W[k] に書き込む
	while (i<m && j<r) { // A[i]とA[j]を比べながら進む
		if (A[i] <= A[j]) W[k++] = A[i++]; // 左半分の方が小さく逆転なし
		else {
			W[k++] = A[j++];
			c += m-i; // 左半分の方が大きい、左半分で未処理の要素だけ飛び越える
		}
	}
	while (i<m) W[k++] = A[i++]; // 左半分が余った場合
	while (j<r) W[k++] = A[j++]; // 右半分が余った場合
	//assert(k == r);
	std::copy(W+l, W+r, A+l);
	return cl + cr + c;
}
main(){
	int N,i;
	for(;scanf("%d",&N),N;printf("%lld\n",merge_and_count(0,N)))for(i=0;i<N;i++)scanf("%d",A+i);
}

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

int kaijyou(int x){
int b;
for(int i=1;i<x;i++){
b*=i;
}
return b;
}

int main(){
int n,t;
int ans;
double count;
while(cin>>n){
if(n==0)return 0;
int a[n];
t=0;
ans=0;
count=0;
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int j=0;t==0;j++){
if(j+1==n){
j=0;
count+=1;
}
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
ans+=1;
}
if(count>kaijyou(n)){
cout << ans << endl;
t=1;
}
}
}

return 0;
}

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

int bubbleSort( int* a, int n )
{
    int cnt = 0;
    for( int i = 0; i < n; i++ )
        for( int j = 0; j < n - 1; j++ )
        {
            if( a[j] > a[j+1] )
            {
                int temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
                cnt++;
            }
        }
    return cnt;
}

int main()
{
    int n, a[100];

    while( cin >> n )
    {
        if( n == 0 )
            break;
        for( int i = 0; i < 100; i++ )
            a[i] = 0;
        for( int i = 0; i < n; i++ )
            cin >> a[i];
        cout << bubbleSort( a, n ) << endl;
    }
    return 0;
}

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 + 1 ; 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<utility>
#include<cmath>
using namespace std;

int kaijyou(int x){
int b;
for(int i=1;i<x;i++){
b*=i;
}
return b;
}

int main(){
int n,t;
int ans;
double count;
while(cin>>n){
if(n==0)return 0;
int a[n];
t=0;
ans=0;
count=0;
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int j=0;t==0;j++){
if(j+1==n){
j=0;
count+=1;
}
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
ans+=1;
}
if(count>kaijyou(n)){
cout << ans << endl;
t=1;
}
}
}

return 0;
}

C++: #include<complex>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cmath>
#include<vector>
#include<map>
#include<sstream>
#include<ctime>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<deque>
#include<queue>
#include<climits>
#include<list>
#include<stack>
#include<ctime>
#define all(n) n.begin(),n.end()
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define insert(a,b,c,d) PP(P(a,b),P(c,d))
#define F first
#define S second
#define Iter(c) __typeof__((c).begin())
#define foreach(i,c) for(Iter(c) i = (c).begin();i != (c).end();++i)
#define pb push_back
#define INF (1<<27)
#define EPS 1e-10
#define equals(a,b) (fabs((a)-(b)) < EPS)

using namespace std;
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef unsigned long long ull;
typedef long long ll;
const int dx[4] = {0,1,0,-1};
const int dy[4] = {1,0,-1,0};

int n,a[10000000];

int bit[10000001];

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;
    }
}

void solve()
{
  rep(i,n+1)
    {
      bit[i] = 0;
    }
  ll ans = 0;
  rep(j,n)
    {
      ans += j-sum(a[j]);
      add(a[j],1);
    }
  cout << ans << endl;
}

int main(){
  
  while(cin >> n && n)
    {
      rep(i,n)
	{
	  cin >> a[i];
	}
      solve();
    }
  return 0;
}

C++: #include <iostream>

int bubblesort(int* a, int n)
{
	int res = 0;
	for (int i=n-1; i>=0; i--) {
		for (int j=0; j<i; j++) {
			if (a[j] > a[j+1]) {
				std::swap(a[j], a[j+1]); res++;
			}
		}
	}
	return res;
}

int main()
{
	int n;
	while (true) {
		std::cin >> n;
		if (n==0) break;
		int* a = new int[n];
		for (int i=0; i<n; i++) std::cin >> a[i];
		std::cout << bubblesort(a, n) << std::endl;
		delete[] a;
	}
	return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
    int n;
    while(cin>>n,n){
        int num[10000]={0},cnt=0;
        num[n]=11000000;
        for(int i=0;i<n;i++)cin>>num[i];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(num[j]>num[j+1]){
                    swap(num[j],num[j+1]);
                    cnt++;
                }
            }
        }
        cout<<cnt<<endl;
    }
    return 0;
}

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

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

C++: #include<stdio.h>
int table[100];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<a;i++)scanf("%d",table+i);
		int ret=0;
		for(int i=0;i<a;i++){
			for(int j=0;j<a-1;j++){
				if(table[j]>table[j+1]){
					ret++;
					int val=table[j];
					table[j]=table[j+1];
					table[j+1]=val;
				}
			}
		}
		printf("%d\n",ret);
	}
}

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

int main(void){
	int n;
	while(cin>>n,n){
		int t[n];
		for(int i=0;i<n;i++){
			cin>>t[i];
		}
		if(n == 1){
			cout<<0<<endl;
			continue;
		}
		int ans = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n-1;j++){
				if(t[j] > t[j+1]){
					ans++;
					swap(t[j],t[j+1]);
				}
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}