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<algorithm>
#include<map>

const int MAX_N = 100;
int bit[MAX_N+1], n;

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

int main(){
	int a[MAX_N], b[MAX_N];
	while(std::cin >> n, n){
		for(int i=0;i<MAX_N+1;i++){
			bit[i] = 0;
		}

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

		for(int i=0;i<n;i++){
			b[i] = a[i];
		}
		std::sort(b, b+n);
		std::map<int, int> m;
		for(int i=0;i<n;i++){
			m[b[i]] = i+1;
		}

		int t = 0;
		for(int i=0;i<n;i++){
			t += i - sum(m[a[i]]);
			add(m[a[i]], 1);
		}

		std::cout << t << std::endl;
	}
}

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

using namespace std;

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

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

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

int main() {
  int n, a[100];
  while (1) {
    scanf("%d", &n);
    if (!n) break;
    for (int i=0; i<n; i++) {
      scanf("%d", &a[i]);
    }
    printf("%d\n", bsort(a, n));
  }
  return 0;
}

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

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

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-i-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>

using namespace std;

void print_vector(vector<int> v)
{
    for (auto it = v.begin(); it < v.end() - 1; it++)
    {
        cout << *it << " ";
    }
    cout << *(v.end() - 1) << endl;
}

void bubble_sort(vector<int> vec, int n)
{
    int buf, count = 0;
    bool flag = true;
    while (flag)
    {
        flag = false;
        for (int i = n - 1; i >= 0; i--)
        {
            if (vec[i - 1] > vec[i])
            {
                swap(vec[i - 1], vec[i]);
                count += 1;
                flag = true;
            }
        }
    }
    cout << count << endl;
    return;
}

int main()
{
    int n = 1;
    while (true)
    {
        {
            cin >> n;
            if (n == 0)
            {
                return 0;
            }
            vector<int> v(n);
            for (int i = 0; i < n; i++)
            {
                cin >> v[i];
            }
            bubble_sort(v, n);
            vector<int>().swap(v);
        }
    }
}

C++: #include <iostream>
#include <vector>
using namespace std;
int bubble(vector<int>, vector<int>&);

int main() {
  do {
    int n;
    cin >> n;
    if(n == 0) break;
    vector<int> nums;
    for(int i = 0; i < n; ++i) {
      int buff;
      cin >> buff;
      nums.push_back(buff);
    }
    int count = 0;
    for(int i = 0; i < n - 1; ++i) {
      vector<int> result;
      count += bubble(nums, result);
      nums = result;
    }
    cout << count << endl;
  } while(true);
  return 0;
}

int bubble(vector<int> nums, vector<int> &result) {
  int size = nums.size();
  int buff = nums[0];
  int count = 0;
  for(int i = 0; i < size - 1; ++i) {
    if(buff > nums[i + 1]) ++count;
    result.push_back(min(buff, nums[i + 1]));
    buff = max(buff, nums[i + 1]);
  }
  result.push_back(buff);
  return count;
}

C++: #include<iostream>
#include<cstdio>
#include<time.h>
using namespace std;
#define N 314159
int a[N],t[N];
long long ans=0;
void marge_count(int s,int g){
     if(s+1==g){return;}
  if(s==g){
      //cout<<"aaaaaaaaa"<<endl;
  }
  int m= (s+g)/2;
  marge_count(s,m);
  marge_count(m,g);
  int p=s,q=m,r=s;
  while(p<m&&q<g){
    if(a[p]<a[q]){
      t[r++]=a[p++];
      ans+=q-m;
    }else{
      t[r++]=a[q++];
    }
  }
  if(p<m){
    while(p<m){
      t[r++]=a[p++];ans+=q-m;
    }
  }else{
      while(q<g)
    t[r++]=a[q++];
  }
  for(int i=s;i<g;i++){
    a[i]=t[i];
  }
}
int n;
int main(){
    while(cin>>n&&n){ans=0;
  for(int i=0;i<n;i++)cin>>a[i];
  marge_count(0,n);
  cout<<ans<<endl;
 // for(int i=0;i<n;i++){cout<<" "<<a[i];}
  //cout<<endl;
    }
  return 0;
}

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

int main(void){

		int N, M;
		while(cin >> N, N){	
			int num[N], cnt = 0;
			fill_n(num, N, 0);
			for(int i = 0; i < N; i++){
				cin >> num[i];
			}
		        for(int i = N-1; i > 0; i--){
		            for(int j = 1; j <= i; j++){
		                if(num[j] < num[j-1]){
		                	int tmp;
		                	tmp = num[j];
		                	num[j] = num[j-1];
		                	num[j] = tmp;
		                    cnt++;
		                }
		            }
		        }
		    cout << cnt << endl;
		}
}

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

int main(void){

		int N, M;
		while(cin >> N, N){	
			int num[N], cnt = 0;
			fill_n(num, N, 0);
			for(int i = 0; i < N; i++){
				cin >> num[i];
			}
		        for(int i = N-1; i > 0; i--){
		            for(int j = 1; j <= i; j++){
		                if(num[j] < num[j-1]){
		                	int tmp;
		                	tmp = num[j];
		                	num[j] = num[j-1];
		                	num[j] = tmp;
		                    cnt++;
		                }
		            }
		        }
		    cout << cnt << endl;
		}
}

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