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;
const int INF = 2000000;

int main() {
  int n;
  while (cin >> n and n != 0) {
    int* arr = new int[n];

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

    int val = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < i; j++)
        if (arr[j] > arr[i]) val++;

    cout << val << endl;
  }
}

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

using namespace std;

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

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

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

int main(){
int n;
int ans;
double count;
while(cin>>n){
if(n==0)return 0;
int a[n];
ans=0;
count=0;
for(int i=0;i<n;i++){
cin >> a[i];
}
for(int j=0;j>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)){
j=-10;
}
}
cout << ans << endl;
}

return 0;
}

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

using namespace std;

int bubble(vector<int> &vec,int to,int changes){
    if(to==0) return changes;
    for(int i=0;i<to;i++){
        if(vec[i] > vec[i+1]){
            int tmp = vec[i];
            vec[i] = vec[i+1];
            vec[i+1] = tmp;
            changes++;
        }
    }
    return bubble(vec,to-1,changes);
}

int main(){
    int n = 0;
    while(cin >> n && n != 0){
        vector<int> vec;
        for(int i=0;i<n;i++){
            int x;
            cin >> x;
            vec.push_back(x);
        }
        cout << bubble(vec,vec.size()-1,0) << endl;
    }
    return 0;
}

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

int main(){

  int n;

  while(cin >> n, n){

    int a[1000],tmp,count = 0;

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

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

	  tmp = a[j];
	  a[j] = a[j-1];
	  a[j-1] = tmp;

	  count++;

	}
      }
    }

    cout << count << endl;

  }
}

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

C++: #include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <cmath>
#include <complex>
#include <map>
using namespace std;

#define rep(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define EPS 1e-8
#define F first
#define S second

static const double PI=6*asin(0.5);
typedef pair<double,double> P;
typedef complex<double> CP;
typedef long long ll;

int a[110];

int main(){
	int n;
	while(cin>>n,n){
		rep(i,110) a[i]=0;
		rep(i,n){
			cin>>a[i];
		}
		ll res=0;
		rep(i,n){
			for(int j=i+1;j<n;j++){
				if(a[i]>a[j]){
					int t;
					t=a[i];
					a[i]=a[j];
					a[j]=t;
					res++;
				}
			}
		}
		cout<<res<<endl;
	}
	return 0;
}

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

int main(){
	int n, dx[101], comp, cnt;
	while(1){
		cin >>n;
		if(n == 0) break;
		for(int i = 0; i < n; i++){
			cin >>dx[i];
		}
		comp = n;
		cnt = 0;
		while(comp != 1){
			for(int i = 0; i < comp; i++){
				if(dx[i] > dx[i + 1]){
					swap(dx[i], dx[i + 1]);
					cnt++;
				}
			}
			comp--;
		}
		cout <<cnt <<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;


int BubbleData[100];

void judge(const int n) {
	int cnt,tmp;

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


int main(void) {
	int n;

	while (cin >> n, n) {
		for (int i = 0; i < n; i++) {
			cin >> BubbleData[i];
		}
		judge(n-1);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
//#define int long long
using namespace std;

int n;
int a[100];

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

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

		cout << ans << endl;
	}
}

C++: //
//  main.cpp
//  Bubble Sort (PC 甲子園 2007)
//
//  Created by 大森 亮 on 2014/04/10.
//  Copyright (c) 2014年 大森 亮. All rights reserved.
//


#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#define rep(i,n) for(int i = 0;i < n;i++)

using namespace std;

int A[105];
int W[105];

int count(int left,int right){
    if(right == left + 1){
        return 0;
    }else if(right == left + 2){
        if(A[left] > A[left+1]){
            swap(A[left], A[left+1]);
            return 1;
        }else{
            return 0;
        }
    }
    int mid = (left+right)/2;
    int cl=count(left,mid),cr=count(mid,right);
    int clr = 0;
    int i = left,j = mid,k=left;
    while(i < mid && j < right){
        if(A[i] <= A[j]){
            W[k++]=A[i++];
        }else{
            clr += mid-i;
            W[k++]=A[j++];

        }
    }
    while(i < mid){
        W[k++]=A[i++];

    }
    while(j < right){
        W[k++]=A[j++];
    }
    assert(k == right);
    copy(W+left,W+right,A+left);
    return cl + cr + clr;
}

int main(){
    int N;
    while(cin >> N&&N!=0){
        rep(i,N){
            cin >> A[i];
        }
        cout << count(0,N) <<endl;
    }
}

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

long long int a[500],n;

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

C++: #include <stdio.h>

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

C++: #include<iostream>
#include<algorithm>
using namespace std;
main() {
  for(int n;cin>>n,n;) {
    int dat[100];
    for(int i=0;i<n;i++)
      cin>>dat[i];
    int ans = 0;
    for(int i=0;i<n-1;i++)
      for(int j=n-1;j>i;j--)
	if(dat[j-1]>dat[j]) swap(dat[j-1],dat[j]), ans++;
    cout<<ans<<endl;
  }
}