prithivMLmods/Coder-Stat huggingface dataset:
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

  int maxval = -1000000000;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      maxval = max(maxval, a[j] - a[i]);
    }
  }

  cout << maxval << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

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

  int maxval = -1000000000;
  int minval = a[0];
  for (int i = 1; i < n; i++) {
    maxval = max(maxval, a[i] - minval);
    minval = min(minval, a[i]);
  }

  cout << maxval << endl;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int main() {

  int n, x[1000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  int max = x[1] - x[0];
  for (int k = 0; k < n; k++) {
    for (int m = k + 1; m < n; m++) {
      if (max < (x[m] - x[k]))
        max = x[m] - x[k];
    }
  }
  cout << max << endl;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {

  int n, x[1000000];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  int max = x[1] - x[0];
  int min = x[0];
  for (int k = 1; k < n; k++) {
    if (max < (x[k] - min))
      max = (x[k] - min);
    if (min > x[k])
      min = x[k];
  }
  cout << max << endl;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

long long MaxProfit(int n, long long *Rt) {
  long long max = LLONG_MIN;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      long long tmp = Rt[j] - Rt[i];
      if (max < tmp)
        max = tmp;
    }
  }
  return max;
}

int main() {

  int n;
  cin >> n;
  long long Rt[n];
  long long max;
  for (int i = 0; i < n; i++)
    cin >> Rt[i];
  max = MaxProfit(n, Rt);
  cout << max << endl;
  return 0;
};

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

long long MaxProfit(int n, long long *Rt) {
  long long max = LLONG_MIN;
  long long MinRt = Rt[0];
  for (int j = 1; j < n; j++) {
    long long tmp = Rt[j] - MinRt;
    if (max < tmp)
      max = tmp;
    if (MinRt > Rt[j])
      MinRt = Rt[j];
  }
  return max;
}

int main() {

  int n;
  cin >> n;
  long long Rt[n];
  long long max;
  for (int i = 0; i < n; i++)
    cin >> Rt[i];
  max = MaxProfit(n, Rt);
  cout << max << endl;
  return 0;
};
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
static const int MAX = 20000;

int main() {
  int R[MAX], n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }

  int maxv = -2000000000;
  int minv = R[0];

  for (int j = 1; j < n; j++) {
    maxv = max(maxv, R[j] - minv);
    minv = min(minv, R[j]);
  }

  cout << maxv << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
static const int MAX = 200000;

int main() {
  int R[MAX], n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }

  int maxv = -2000000000;
  int minv = R[0];

  for (int j = 1; j < n; j++) {
    maxv = max(maxv, R[j] - minv);
    minv = min(minv, R[j]);
  }

  cout << maxv << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int R[2000];
int num;
int main() {
  // cout << "????????\???????????¶??´??????" << endl;
  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> R[i];
  }
  int maxv = -2000000000;
  int minv = R[0];
  for (int j = 1; j < num; j++) {
    if (maxv < R[j] - minv) {
      maxv = R[j] - minv;
    }
    if (minv > R[j]) {
      minv = R[j];
    }
    /*	for (int i = 0; i < j; i++)
            {
                    if (maxv < R[j] - R[i])
                    {
                            maxv = R[j] - R[i];
                    }
            }*/
  }
  cout << maxv << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int R[200000];
int num;
int main() {
  // cout << "????????\???????????¶??´??????" << endl;
  cin >> num;
  for (int i = 0; i < num; i++) {
    cin >> R[i];
  }
  int maxv = -2000000000;
  int minv = R[0];
  for (int j = 1; j < num; j++) {
    if (maxv < R[j] - minv) {
      maxv = R[j] - minv;
    }
    if (minv > R[j]) {
      minv = R[j];
    }
    /*	for (int i = 0; i < j; i++)
            {
                    if (maxv < R[j] - R[i])
                    {
                            maxv = R[j] - R[i];
                    }
            }*/
  }
  cout << maxv << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>

using namespace std;

int n;
const int MAX_N = 200000;
int R[MAX_N];

int main() {

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> R[i];
  }
  int maxv = -2000000000;
  for (int j = 1; j <= n - 1; j++) {
    for (int i = 0; i <= j - 1; i++) {
      maxv = max(maxv, R[j] - R[i]);
    }
  }
  cout << maxv << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int n;
const int MAX_N = 200000;
int R[MAX_N];

int main() {

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> R[i];
  }
  int maxv = -2000000000;
  int minv = R[0];
  for (int j = 1; j < n; j++) {

    maxv = max(maxv, R[j] - minv);
    minv = min(minv, R[j]);
  }
  cout << maxv << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 20000;

int main() {
  int R[MAX], n;

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

  int maxv = -2000000000;
  int minv = R[0];

  for (int i = 1; i < n; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }

  cout << maxv << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 200000;

int main() {
  int R[MAX], n;

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

  int maxv = -2000000000;
  int minv = R[0];

  for (int i = 1; i < n; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }

  cout << maxv << endl;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

int main() {
  int n;
  int r[200000];
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> r[i];
  }
  int s = r[1] - r[0];
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int ss = r[j] - r[i];
      if (ss > s) {
        s = ss;
      }
    }
  }
  std::cout << s << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>

int main() {
  int n;
  int r[200000];
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> r[i];
  }
  int s = r[1] - r[0];
  int rr = r[0];
  for (int i = 1; i < n; i++) {
    if (r[i] - rr > s) {
      s = r[i] - rr;
    }
    if (r[i] < rr) {
      rr = r[i];
    }
  }
  std::cout << s << std::endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {

  long long n, MAX = -1000000000;
  cin >> n;
  vector<int> R(n);

  /* 株価の値を入力 */
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }

  /* 株価の最大値を探す */
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (MAX < R[j] - R[i])
        MAX = R[j] - R[i];
    }
  }

  cout << MAX << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {

  long long n, MAX = -1000000000;
  cin >> n;
  vector<int> R(n);

  /* 株価の値を入力 */
  for (int i = 0; i < n; i++) {
    cin >> R[i];
  }

  /* 株価の最大値を探す */
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (MAX < R[j] - R[i])
        MAX = R[j] - R[i];
      if (R[i] >= R[j])
        break;
    }
  }

  cout << MAX << endl;

  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 20000;

int main() {
  int R[MAX], N;

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> R[i];

  int maxv = R[1] - R[0];
  int minv = R[0];
  for (int i = 1; i < N; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }
  cout << maxv << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 200000;

int main() {
  int R[MAX], N;

  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> R[i];

  int maxv = R[1] - R[0];
  int minv = R[0];
  for (int i = 1; i < N; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }
  cout << maxv << endl;
  return 0;
}
Problem p02258 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 20000;
int main() {
  int R[MAX], n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> R[i];
  int maxv = -2000000000;
  int minv = R[0];
  for (int i = 1; i < n; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }
  cout << maxv << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 200000;
int main() {
  int R[MAX], n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> R[i];
  int maxv = -2000000000;
  int minv = R[0];
  for (int i = 1; i < n; i++) {
    maxv = max(maxv, R[i] - minv);
    minv = min(minv, R[i]);
  }
  cout << maxv << endl;

  return 0;
}
Problem p02258 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
R = [int(input()) for i in range(N)]

sa = R[N - 1] - R[N - 2]
for i in reversed(range(N)):
    for j in reversed(range(i)):
        tmp = R[i] - R[j]
        if tmp > sa:
            sa = tmp
print(sa)


Fixed submission:
N = int(input())
R = [int(input()) for i in range(N)]

min_v = R[0]
max_v = R[1] - R[0]
for i in range(1, len(R)):
    if min_v > R[i - 1]:
        min_v = R[i - 1]
    if max_v < R[i] - min_v:
        max_v = R[i] - min_v
print(max_v)
Problem p02258 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rloop(i, a, b) for (int i = a; i >= b; i--)
const int inf = 1e8;
const ll INF = 1e16;
#define MOD 1000000007
#define mod 1000000009

int main() {
  int n;
  cin >> n;
  vector<ll> r(n);
  loop(i, 0, n) { cin >> r[i]; }
  ll M = -1 * INF;
  ll m = r[0];
  loop(i, 0, n - 1) {
    if (r[i] == m) {
      vector<ll> r2(n);
      r2 = r;
      sort(r2.begin() + i + 1, r2.end());
      m = r2.back();
    }
    M = max(M, m - r[i]);
  }
  cout << M << "\n";
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rloop(i, a, b) for (int i = a; i >= b; i--)
const int inf = 1e8;
const ll INF = 1e16;
#define MOD 1000000007
#define mod 1000000009

int main() {
  int n;
  cin >> n;
  vector<ll> r(n);
  loop(i, 0, n) { cin >> r[i]; }
  ll M = -1 * INF;
  ll m = r[0];
  loop(i, 1, n) {
    m = min(m, r[i - 1]);
    M = max(M, r[i] - m);
  }
  cout << M << "\n";
}
Problem p02259 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int bubbleSort(int A[], int N) {
  int sw = 0;
  bool flag = 1;
  for (int i = 0; flag; i++) {
    flag = 0;
    for (int j = N - 1; j >= i + 1; j++) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        sw++;
      }
    }
  }
  return sw;
}

int main(void) {
  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sw = bubbleSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << sw << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int bubbleSort(int A[], int N) {
  int sw = 0;
  bool flag = 1;
  for (int i = 0; flag; i++) {
    flag = 0;
    for (int j = N - 1; j >= i + 1; j--) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        sw++;
      }
    }
  }
  return sw;
}

int main(void) {
  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sw = bubbleSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << sw << endl;

  return 0;
}
Problem p02259 (C++)

Buggy submission (status: Time Limit Exceeded):
// bubble sort
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, d, i1, i2, times;
  cin >> n;
  int a[n];

  for (i1 = 0; i1 < n; i1++) {
    cin >> a[i1];
  }
  times = 0;
  i2 = n;
  for (i2 = n; i2 > 0; i2--) {
    i1 = n;
    while (1) {
      i1--;
      if (a[i1] < a[i1 - 1]) {
        d = a[i1];
        a[i1] = a[i1 - 1];
        a[i1 - 1] = d;
        times++;
      }
      if (i1 <= n - i2) {
        break;
      }
    }
  }

  i1 = 0;
  // cout
  while (1) {
    if (i1 == 0) {
      cout << a[i1];
      continue;
    }
    cout << " " << a[i1];
    i1++;
    if (i1 == n) {
      break;
    }
  }
  cout << endl << times << endl;

  return 0;
}

Fixed submission:
// bubble sort
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int n, d, i1, i2, times;
  cin >> n;
  int a[n];

  for (i1 = 0; i1 < n; i1++) {
    cin >> a[i1];
  }
  times = 0;
  i2 = n;
  for (i2 = n; i2 > 0; i2--) {
    i1 = n;
    while (1) {
      i1--;
      if (a[i1] < a[i1 - 1]) {
        d = a[i1];
        a[i1] = a[i1 - 1];
        a[i1 - 1] = d;
        times++;
      }
      if (i1 <= n - i2) {
        break;
      }
    }
  }

  i1 = 0;
  // cout
  while (1) {
    if (i1 == 0) {
      cout << a[i1];
      if (n == 1) {
        break;
      } else {
        i1++;
        continue;
      }
    }
    cout << " " << a[i1];
    i1++;
    if (i1 == n) {
      break;
    }
  }
  cout << endl << times << endl;

  return 0;
}
Problem p02259 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int bubbleSort(int p[], int n) {
  int flag = 1;
  int count = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!flag)
      break;
    flag = 0;
    for (int j = n - 1; j > i; j++)
      if (p[j] < p[j - 1]) {
        flag = 1;
        int t = p[j];
        p[j] = p[j - 1];
        p[j - 1] = t;
        count++;
      }
  }
  return count;
}
int main() {
  int n;
  cin >> n;
  int *p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];

  int m = bubbleSort(p, n);
  for (int i = 0; i < n - 1; i++)
    cout << p[i] << ' ';
  cout << p[n - 1] << endl;
  cout << m << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int bubbleSort(int p[], int n) {
  int flag = 1;
  int count = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!flag)
      break;
    flag = 0;
    for (int j = n - 1; j > i; j--)
      if (p[j] < p[j - 1]) {
        flag = 1;
        int t = p[j];
        p[j] = p[j - 1];
        p[j - 1] = t;
        count++;
      }
  }
  return count;
}
int main() {
  int n;
  cin >> n;
  int *p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];

  int m = bubbleSort(p, n);
  for (int i = 0; i < n - 1; i++)
    cout << p[i] << ' ';
  cout << p[n - 1] << endl;
  cout << m << endl;
}
Problem p02259 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> List;
  int count = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    int Value;
    cin >> Value;
    List.push_back(Value);
  }

  bool Flag = true;

  while (Flag) {
    Flag = false;
    for (int j = n - 1; j > 0; j++) {
      if (List[j] < List[j - 1]) {
        int Temp;
        Temp = List[j];
        List[j] = List[j - 1];
        List[j - 1] = Temp;
        Flag = true;
        count++;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << List[i];
  }
  cout << endl;

  cout << count << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> List;
  int count = 0;

  cin >> n;

  for (int i = 0; i < n; i++) {
    int Value;
    cin >> Value;
    List.push_back(Value);
  }

  bool Flag = true;

  while (Flag) {
    Flag = false;
    for (int j = n - 1; j > 0; j--) {
      if (List[j] < List[j - 1]) {
        int Temp;
        Temp = List[j];
        List[j] = List[j - 1];
        List[j - 1] = Temp;
        Flag = true;
        count++;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << List[i];
  }
  cout << endl;

  cout << count << endl;
}
Problem p02259 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, a[100], ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i + 1; j++) {
      if (a[i] > a[j])
        swap(a[i], a[j]);
      ans++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == n - 1)
      cout << a[i] << "\n";
    else
      cout << a[i] << " ";
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int n, a[100], ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i + 1; j--) {
      if (a[j] < a[j - 1]) {
        swap(a[j], a[j - 1]);
        ans++;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == n - 1)
      cout << a[i] << "\n";
    else
      cout << a[i] << " ";
  }
  cout << ans << "\n";
  return 0;
}
Problem p02259 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int a[100];
  cin >> n;
  int k = 0, count = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (1) {
    for (int i = n - 1; i > 0 + k; i--) {
      if (a[i - 1] > a[i]) {
        swap(a[i - 1], a[i]);
        count++;
      }
    }
    k++;
    if (k == n - 1)
      break;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0)
      cout << a[i];
    else
      cout << " " << a[i];
  }
  cout << endl;
  cout << count << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int a[100];
  cin >> n;
  int k = 0, count = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  while (1) {
    for (int i = n - 1; i > 0 + k; i--) {
      if (a[i - 1] > a[i]) {
        swap(a[i - 1], a[i]);
        count++;
      }
    }
    k++;
    if (k == n)
      break;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0)
      cout << a[i];
    else
      cout << " " << a[i];
  }
  cout << endl;
  cout << count << endl;
  return 0;
}
Problem p02259 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

void print_vector(vector<int> &v) {
  for (int x : v) {
    std::cout << x << " ";
  }
  std::cout << endl;
}

int main() {
#ifdef LOCAL
  // ??\??? cin ?????\????????? 'xxx.txt' ?????????
  std::ifstream in("alds1_2_a.txt");
  std::cin.rdbuf(in.rdbuf());
#else
#endif

  vector<int> vec;
  int n = 0;
  int change_num = 0;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    vec.push_back(tmp);
  }

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

  print_vector(vec);
  cout << change_num << endl;
  return 0;
}

Fixed submission:
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

void print_vector(vector<int> &v) {
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
    std::cout << *it;
    if (it != v.end() - 1) {
      std::cout << " ";
    }
  }
  std::cout << endl;
}

int main() {
#ifdef LOCAL
  // ??\??? cin ?????\????????? 'xxx.txt' ?????????
  std::ifstream in("alds1_2_a.txt");
  std::cin.rdbuf(in.rdbuf());
#else
#endif

  vector<int> vec;
  int n = 0;
  int change_num = 0;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    vec.push_back(tmp);
  }

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

  print_vector(vec);
  cout << change_num << endl;
  return 0;
}
Problem p02259 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int bubbleSort(int A[], int N) {
  int sw = 0;
  bool flag = 1;
  for (int i = 0; flag; i++) {
    for (int j = N - 1; j >= i + 1; j--) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        sw++;
      }
    }
  }
  return sw;
}

int main() {
  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sw = bubbleSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << sw << endl;
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

int bubbleSort(int A[], int N) {
  int sw = 0;
  bool flag = 1;
  for (int i = 0; flag; i++) {
    flag = 0;
    for (int j = N - 1; j >= i + 1; j--) {
      if (A[j] < A[j - 1]) {
        swap(A[j], A[j - 1]);
        flag = 1;
        sw++;
      }
    }
  }
  return sw;
}

int main() {
  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sw = bubbleSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
  cout << sw << endl;
  return 0;
}
Problem p02259 (C++)

Buggy submission (status: Runtime Error):
/***
 *	??????????????????
 *	???????????????????????£???????????£??\????´?????????????????????§????¬???????????????°?????????
 *	?????????????°?????????£??\??????????´?????????????????????????????????§?°???¢?????????????????°???????????????
 ****/

#include <iostream>
#include <vector>
using namespace std;

int main(void) //???????????¢??°
{
  int n;
  cin >> n;

  vector<int> str;
  str.resize(n);

  for (int i = 0; i < n; i++) { //????????\???
    cin >> str[i];
  }
  //	int k = n-1;

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

    for (int j = (n - 1); j <= (i + 1); j--) {
      //		for(int j = 1 ; j< (n-i); j++){
      if (str[j] < str[j - 1]) { //????????????????´???¨??????
        int temp = str[j];
        str[j] = str[j - 1];
        str[j - 1] = temp;
        cnter++;
      }
    }
  }
  //	k=j;

  //----------------------------------	???????????????
  for (int i = 0; i < n; i++) {
    cout << str[i];
    if (i != (n - 1)) {
      cout << " ";
    }
  }
  cout << endl;

  cout
      << cnter
      << endl; //???????????°????????¢??°???????????¢?????°??¨?????°??????????????±?????????????????¨?????°???

  return 0;
}

Fixed submission:
/***
 *	??????????????????
 *	???????????????????????£???????????£??\????´?????????????????????§????¬???????????????°?????????
 *	?????????????°?????????£??\??????????´?????????????????????????????????§?°???¢?????????????????°???????????????
 ****/

#include <iostream>
#include <vector>
using namespace std;

int main(void) //???????????¢??°
{
  int n;
  cin >> n;

  vector<int> str;
  str.resize(n);

  for (int i = 0; i < n; i++) { //????????\???
    cin >> str[i];
  }
  //	int k = n-1;

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

    for (int j = 1; j < (n - i); j++) {
      if (str[j] < str[j - 1]) { //????????????????´???¨??????
        int temp = str[j];
        str[j] = str[j - 1];
        str[j - 1] = temp;
        cnter++;
      }
    }
  }
  //	k=j;

  //----------------------------------	???????????????
  for (int i = 0; i < n; i++) {
    cout << str[i];
    if (i != (n - 1)) {
      cout << " ";
    }
  }
  cout << endl;

  cout
      << cnter
      << endl; //???????????°????????¢??°???????????¢?????°??¨?????°??????????????±?????????????????¨?????°???

  return 0;
}
Problem p02259 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;

void b_sort(int *a, int N);

int main() {
  int a[100];
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }

  b_sort(a, N);
}

void b_sort(int *a, int N) {
  int j = N - 1;
  int t;
  int count = 0;

  while (1) {
    for (int i = 0; i < j; i++) {
      if (a[i] > a[i + 1]) {
        t = a[i];
        a[i] = a[i + 1];
        a[i + 1] = t;
        count++;
      }
    }
    j--;
    if (j == 0) {
      break;
    }
  }
  for (int i = 0; i < N; i++) {
    printf("%d", a[i]);
    if (i != N - 1) {
      printf(" ");
    } else {
      printf("\n");
    }
  }
  printf("%d\n", count);
}


Fixed submission:
#include <cstdio>
using namespace std;

void b_sort(int *a, int N);

int main() {
  int a[100];
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
  }

  b_sort(a, N);
}

void b_sort(int *a, int N) {
  int j = N - 1;
  int t;
  int count = 0;

  while (1) {
    for (int i = 0; i < j; i++) {
      if (a[i] > a[i + 1]) {
        t = a[i];
        a[i] = a[i + 1];
        a[i + 1] = t;
        count++;
      }
    }
    if (j != 0)
      j--;
    if (j == 0) {
      break;
    }
  }
  for (int i = 0; i < N; i++) {
    printf("%d", a[i]);
    if (i != N - 1) {
      printf(" ");
    } else {
      printf("\n");
    }
  }
  printf("%d\n", count);
}
Problem p02260 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int selectionSort(int A[], int N) {
  int i, j, t, sw = 0, minj;
  for (i = 0; i < N - 1; i++) {
    minj = i;
    for (j = i; j < N; j++) {
      if (A[j] < A[minj])
        minj = j;
    }
    t = A[minj];
    A[minj] = A[i];
    A[i] = t;
    if (i != minj)
      sw++;
  }
  return sw;
}

int main() {
  int A[100], N, i, sw;

  scanf("%d", &N);
  for (i = 0; i < N; i++)
    scanf("%d", A[i]);

  sw = selectionSort(A, N);

  for (i = 0; i < N; i++) {
    if (i != 0)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\n");
  printf("%d\n", sw);

  return 0;
}

Fixed submission:
#include <stdio.h>

int selectionSort(int A[], int N) {
  int i, j, t, sw = 0, minj;
  for (i = 0; i < N - 1; i++) {
    minj = i;
    for (j = i; j < N; j++) {
      if (A[j] < A[minj])
        minj = j;
    }
    t = A[minj];
    A[minj] = A[i];
    A[i] = t;
    if (i != minj)
      sw++;
  }
  return sw;
}

int main() {
  int A[100], N, i, sw;

  scanf("%d", &N);
  for (i = 0; i < N; i++)
    scanf("%d", &A[i]);

  sw = selectionSort(A, N);

  for (i = 0; i < N; i++) {
    if (i != 0)
      printf(" ");
    printf("%d", A[i]);
  }
  printf("\n");
  printf("%d\n", sw);

  return 0;
}
Problem p02260 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

// ?????????????????????????????????????????°
int selectionSort(int A[], int N) {
  int sw = 0;
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; i < N; j++) {
      if (A[j] < A[minj])
        minj = j;
    }
    if (i != minj) {
      swap(A[i], A[minj]);
      sw++;
    }
  }
  return sw;
}

int main() {
  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sw = selectionSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl << sw << endl;

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

// ?????????????????????????????????????????°
int selectionSort(int A[], int N) {
  int sw = 0;
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j] < A[minj])
        minj = j;
    }
    if (i != minj) {
      swap(A[i], A[minj]);
      sw++;
    }
  }
  return sw;
}

int main() {
  int A[100], N, sw;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  sw = selectionSort(A, N);

  for (int i = 0; i < N; i++) {
    if (i)
      cout << " ";
    cout << A[i];
  }
  cout << endl << sw << endl;

  return 0;
}
Problem p02260 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main() {
  int m, min, minn, i, j, n, c;
  int a[100];
  c = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 0; i < n - 1; i++) {
    m = a[i];
    min = a[i];
    for (j = i + 1; j < n; j++) {
      if (min > a[j]) {
        min = a[j];
        minn = j;
      }
    }
    if (i != minn) {
      a[i] = a[minn];
      a[minn] = m;
      c++;
    }
  }
  printf("%d", a[0]);
  for (i = 1; i < n; i++) {
    printf(" %d", a[i]);
  }
  printf("\n%d\n", c);
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int m, min, minn, i, j, n, c;
  int a[100];
  c = 0;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 0; i < n - 1; i++) {
    m = a[i];
    min = a[i];
    minn = i;
    for (j = i + 1; j < n; j++) {
      if (min > a[j]) {
        min = a[j];
        minn = j;
      }
    }
    if (i != minn) {
      a[i] = a[minn];
      a[minn] = m;
      c++;
    }
  }
  printf("%d", a[0]);
  for (i = 1; i < n; i++) {
    printf(" %d", a[i]);
  }
  printf("\n%d\n", c);
  return 0;
}
Problem p02261 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

struct Card {
  char suit, value;
};

void bubble(struct Card A[], int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j < i + 1; j--) {
      if (A[j].value < A[j - 1].value) {
        Card t = A[j];
        A[j] = A[j - 1];
        A[j - 1] = t;
      }
    }
  }
}

void selection(struct Card A[], int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j].value < A[minj].value)
        minj = j;
    }
    Card t = A[i];
    A[i] = A[minj];
    A[minj] = t;
  }
}

void print(struct Card A[], int N) {
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    cout << A[i].suit << A[i].value;
  }
  cout << endl;
}

bool isStable(struct Card C1[], struct Card C2[], int N) {
  for (int i = 0; i < N; i++) {
    if (C1[i].suit != C2[i].suit)
      return false;
  }
  return true;
}

int main() {
  Card C1[100], C2[100];
  int N;
  char ch;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> C1[i].suit >> C1[i].value;
  }

  for (int i = 0; i < N; i++)
    C2[i] = C1[i];

  bubble(C1, N);
  selection(C2, N);

  print(C1, N);
  cout << "Stable" << endl;
  print(C2, N);
  if (isStable(C1, C2, N)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

struct Card {
  char suit, value;
};

void bubble(struct Card A[], int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j >= i + 1; j--) {
      if (A[j].value < A[j - 1].value) {
        Card t = A[j];
        A[j] = A[j - 1];
        A[j - 1] = t;
      }
    }
  }
}

void selection(struct Card A[], int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j].value < A[minj].value)
        minj = j;
    }
    Card t = A[i];
    A[i] = A[minj];
    A[minj] = t;
  }
}

void print(struct Card A[], int N) {
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    cout << A[i].suit << A[i].value;
  }
  cout << endl;
}

bool isStable(struct Card C1[], struct Card C2[], int N) {
  for (int i = 0; i < N; i++) {
    if (C1[i].suit != C2[i].suit)
      return false;
  }
  return true;
}

int main() {
  Card C1[100], C2[100];
  int N;
  char ch;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> C1[i].suit >> C1[i].value;
  }

  for (int i = 0; i < N; i++)
    C2[i] = C1[i];

  bubble(C1, N);
  selection(C2, N);

  print(C1, N);
  cout << "Stable" << endl;
  print(C2, N);
  if (isStable(C1, C2, N)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  return 0;
}
Problem p02261 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef vector<string> card_list;

bool comp(string a, string b) {
  int x = static_cast<int>(a[1]) - 48;
  int y = static_cast<int>(b[1]) - 48;
  return x < y;
}

int bubbleSort(card_list &A) {
  int N = A.size();
  int c = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = N - 1; j > i; --j) {
      if (comp(A[j], A[j - 1])) {
        swap(A[j], A[j - 1]);
        c++;
      }
    }
  }
  return c;
}

int selectionSort(card_list &A) {
  int N = A.size();
  int c = 0;
  for (int i = 0; i < N; ++i) {
    int min = i;
    for (int j = i; j < N; ++j) {
      if (comp(A[j], A[min])) {
        min = j;
      }
    }
    if (i != min) {
      swap(A[i], A[min]);
      c++;
    }
  }
  return c;
}

void print_card_list(card_list &cards) {
  card_list::iterator it = cards.begin();
  for (; it != cards.end() - 1; ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
}

int main() {
  int n;
  cin >> n;

  int a[n];
  card_list A;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    A.push_back(s);
  }
  card_list B = A;

  bubbleSort(A);
  print_card_list(A);
  cout << "Stable" << endl;

  selectionSort(B);
  print_card_list(B);
  if (equal(A.begin(), A.end(), B.end())) {
    cout << "Stable";
  } else {
    cout << "Not stable";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef vector<string> card_list;

bool comp(string a, string b) {
  int x = static_cast<int>(a[1]) - 48;
  int y = static_cast<int>(b[1]) - 48;
  return x < y;
}

int bubbleSort(card_list &A) {
  int N = A.size();
  int c = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = N - 1; j > i; --j) {
      if (comp(A[j], A[j - 1])) {
        swap(A[j], A[j - 1]);
        c++;
      }
    }
  }
  return c;
}

int selectionSort(card_list &A) {
  int N = A.size();
  int c = 0;
  for (int i = 0; i < N; ++i) {
    int min = i;
    for (int j = i; j < N; ++j) {
      if (comp(A[j], A[min])) {
        min = j;
      }
    }
    if (i != min) {
      swap(A[i], A[min]);
      c++;
    }
  }
  return c;
}

void print_card_list(card_list &cards) {
  card_list::iterator it = cards.begin();
  for (; it != cards.end() - 1; ++it) {
    cout << *it << " ";
  }
  cout << *it << endl;
}

int main() {
  int n;
  cin >> n;

  int a[n];
  card_list A;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    A.push_back(s);
  }
  card_list B = A;

  bubbleSort(A);
  print_card_list(A);
  cout << "Stable" << endl;

  selectionSort(B);
  print_card_list(B);
  if (equal(A.begin(), A.end(), B.begin())) {
    cout << "Stable";
  } else {
    cout << "Not stable";
  }
  cout << endl;

  return 0;
}
Problem p02261 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[36], b[36];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], b[i] = a[i];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j++) {
      if (b[i][1] > b[j][1])
        swap(b[i], b[j]);
    }
  }
  for (int i = 0; i < n; i++)
    cout << (i ? " " : "") << b[i];
  cout << endl;
  cout << "Stable" << endl;
  for (int i = 0; i < n; i++) {
    int r = i;
    for (int j = i + 1; j < n; j++) {
      if (a[j][1] < a[r][1])
        r = j;
    }
    swap(a[i], a[r]);
  }
  for (int i = 0; i < n; i++)
    cout << (i ? " " : "") << a[i];
  cout << endl;
  cout << (equal(a, a + n, b) ? "Stable" : "Not stable") << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[36], b[36];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i], b[i] = a[i];
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (b[j - 1][1] > b[j][1])
        swap(b[j - 1], b[j]);
    }
  }
  for (int i = 0; i < n; i++)
    cout << (i ? " " : "") << b[i];
  cout << endl;
  cout << "Stable" << endl;
  for (int i = 0; i < n; i++) {
    int r = i;
    for (int j = i + 1; j < n; j++) {
      if (a[j][1] < a[r][1])
        r = j;
    }
    swap(a[i], a[r]);
  }
  for (int i = 0; i < n; i++)
    cout << (i ? " " : "") << a[i];
  cout << endl;
  cout << (equal(a, a + n, b) ? "Stable" : "Not stable") << endl;
}
Problem p02261 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

struct Card {
  char suit, value;
};

void bubble(struct Card a[], int n) {
  Card hoge;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j++) {
      if (a[j].value < a[j - 1].value) {
        hoge = a[j];
        a[j] = a[j - 1];
        a[j - 1] = hoge;
      }
    }
  }
}

void selection(struct Card a[], int n) {
  int minj;
  Card hoge;
  for (int i = 0; i < n; i++) {
    minj = i;
    for (int j = i; j < n; j++) {
      if (a[j].value < a[minj].value)
        minj = j;
    }
    hoge = a[i];
    a[i] = a[minj];
    a[minj] = hoge;
  }
}
void print(struct Card a[], int n) {
  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << a[i].suit << a[i].value;
  }
  cout << endl;
}
bool stable(struct Card c1[], struct Card c2[], int n) {
  for (int i = 0; i < n; i++) {
    if (c1[i].suit != c2[i].suit)
      return false;
  }
  return true;
}

int main() {
  Card c1[40], c2[40];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c1[i].suit >> c1[i].value;
    c2[i] = c1[i];
  }
  bubble(c1, n);
  selection(c2, n);
  print(c1, n);
  cout << "Stable" << endl;
  print(c2, n);
  if (stable(c1, c2, n))
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;

struct Card {
  char suit, value;
};

void bubble(struct Card a[], int n) {
  Card hoge;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (a[j].value < a[j - 1].value) {
        hoge = a[j];
        a[j] = a[j - 1];
        a[j - 1] = hoge;
      }
    }
  }
}

void selection(struct Card a[], int n) {
  int minj;
  Card hoge;
  for (int i = 0; i < n; i++) {
    minj = i;
    for (int j = i; j < n; j++) {
      if (a[j].value < a[minj].value)
        minj = j;
    }
    hoge = a[i];
    a[i] = a[minj];
    a[minj] = hoge;
  }
}
void print(struct Card a[], int n) {
  for (int i = 0; i < n; i++) {
    if (i > 0)
      cout << " ";
    cout << a[i].suit << a[i].value;
  }
  cout << endl;
}
bool stable(struct Card c1[], struct Card c2[], int n) {
  for (int i = 0; i < n; i++) {
    if (c1[i].suit != c2[i].suit)
      return false;
  }
  return true;
}

int main() {
  Card c1[40], c2[40];
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c1[i].suit >> c1[i].value;
    c2[i] = c1[i];
  }
  bubble(c1, n);
  selection(c2, n);
  print(c1, n);
  cout << "Stable" << endl;
  print(c2, n);
  if (stable(c1, c2, n))
    cout << "Stable" << endl;
  else
    cout << "Not stable" << endl;

  return 0;
}
Problem p02261 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Card {
  char suit, value;
};

void BubbleSort(struct Card C[], int n) {
  Card tmp;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i; j--) {
      if (C[j].value > C[j - 1].value) {
        tmp = C[j];
        C[j] = C[j - 1];
        C[j - 1] = tmp;
      }
    }
  }
}

void SelectionSort(struct Card C[], int n) {
  Card tmp;
  for (int i = 0; i < n; i++) {
    int min = i;
    for (int j = i; j < n; j++) {
      if (C[j].value < C[min].value) {
        min = j;
      }
    }
    tmp = C[i];
    C[i] = C[min];
    C[min] = tmp;
  }
}

void print(struct Card C[], int n) {
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << C[i].suit << C[i].value;
  }
  cout << endl;
}

bool isStable(struct Card C1[], struct Card C2[], int n) {
  for (int i = 0; i < n; i++) {
    if (C1[i].suit != C2[i].suit) {
      return false;
    }
  }
  return true;
}

int main() {
  Card C1[100], C2[100];
  int n;
  char ch;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> C1[i].suit >> C1[i].value;
  }
  for (int i = 0; i < n; i++)
    C2[i] = C1[i];
  BubbleSort(C1, n);
  SelectionSort(C2, n);
  print(C1, n);
  cout << "Stable" << endl;
  print(C2, n);
  if (isStable(C1, C2, n)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Card {
  char suit, value;
};

void BubbleSort(struct Card C[], int n) {
  Card tmp;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j >= i; j--) {
      if (C[j].value < C[j - 1].value) {
        tmp = C[j];
        C[j] = C[j - 1];
        C[j - 1] = tmp;
      }
    }
  }
}

void SelectionSort(struct Card C[], int n) {
  Card tmp;
  for (int i = 0; i < n; i++) {
    int min = i;
    for (int j = i; j < n; j++) {
      if (C[j].value < C[min].value) {
        min = j;
      }
    }
    tmp = C[i];
    C[i] = C[min];
    C[min] = tmp;
  }
}

void print(struct Card C[], int n) {
  for (int i = 0; i < n; i++) {
    if (i != 0)
      cout << " ";
    cout << C[i].suit << C[i].value;
  }
  cout << endl;
}

bool isStable(struct Card C1[], struct Card C2[], int n) {
  for (int i = 0; i < n; i++) {
    if (C1[i].suit != C2[i].suit) {
      return false;
    }
  }
  return true;
}

int main() {
  Card C1[100], C2[100];
  int n;
  char ch;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> C1[i].suit >> C1[i].value;
  }
  for (int i = 0; i < n; i++)
    C2[i] = C1[i];
  BubbleSort(C1, n);
  SelectionSort(C2, n);
  print(C1, n);
  cout << "Stable" << endl;
  print(C2, n);
  if (isStable(C1, C2, n)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }
  return 0;
}
Problem p02261 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

typedef struct {
  char suit;
  char value;
} Card;

void bubbleSort(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j >= i; j--) {
      if (A[j].value >= A[j - 1].value) {
        Card t = A[j];
        A[j] = A[j - 1];
        A[j - 1] = t;
      }
    }
  }
}

void selectionSort(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j].value < A[minj].value)
        minj = j;
    }
    Card t = A[i];
    A[i] = A[minj];
    A[minj] = t;
  }
}

void print(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    cout << A[i].suit << A[i].value;
  }
  cout << endl;
}

bool isStable(Card C1[], Card C2[], int N) {
  for (int i = 0; i < N; i++) {
    if (C1[i].suit != C2[i].suit)
      return false;
  }
  return true;
}

int main() {
  Card C1[100], C2[100];
  int N;
  char ch;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> C1[i].suit >> C1[i].value;
  }

  for (int i = 0; i < N; i++)
    C2[i] = C1[i];

  bubbleSort(C1, N);
  selectionSort(C2, N);

  print(C1, N);
  cout << "Stable" << endl;
  print(C2, N);
  if (isStable(C1, C2, N)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

typedef struct {
  char suit;
  char value;
} Card;

void bubbleSort(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    for (int j = N - 1; j >= i; j--) {
      if (A[j].value < A[j - 1].value) {
        Card t = A[j];
        A[j] = A[j - 1];
        A[j - 1] = t;
      }
    }
  }
}

void selectionSort(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    int minj = i;
    for (int j = i; j < N; j++) {
      if (A[j].value < A[minj].value)
        minj = j;
    }
    Card t = A[i];
    A[i] = A[minj];
    A[minj] = t;
  }
}

void print(Card A[], int N) {
  for (int i = 0; i < N; i++) {
    if (i > 0)
      cout << " ";
    cout << A[i].suit << A[i].value;
  }
  cout << endl;
}

bool isStable(Card C1[], Card C2[], int N) {
  for (int i = 0; i < N; i++) {
    if (C1[i].suit != C2[i].suit)
      return false;
  }
  return true;
}

int main() {
  Card C1[100], C2[100];
  int N;
  char ch;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> C1[i].suit >> C1[i].value;
  }

  for (int i = 0; i < N; i++)
    C2[i] = C1[i];

  bubbleSort(C1, N);
  selectionSort(C2, N);

  print(C1, N);
  cout << "Stable" << endl;
  print(C2, N);
  if (isStable(C1, C2, N)) {
    cout << "Stable" << endl;
  } else {
    cout << "Not stable" << endl;
  }

  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
#include <vector>

#define ArrayNum 100

vector<int> g;

int insertionSort(int a[], int n, int g) {
  int i, j, cnt = 0, v;

  for (i = g; i < n; i++) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j = j - g;
      cnt++;
    }
    a[j + g] = v;
  }
  return cnt;
}

void shellSort(int a[], int n) {
  int i, cnt = 0, m;

  for (int h = 1;;) {
    if (h > n)
      break;
    g.push_back(h);
    h = 3 * h + 1;
  }
  m = g.size();
  for (i = m - 1; i >= 0; i--)
    cnt = cnt + insertionSort(a, n, g[i]);

  cout << m << "\n";
  for (i = m - 1; i > 0; i--)
    cout << g[i] << " ";
  cout << g[0] << "\n";
  cout << cnt << "\n";
  for (i = 0; i < n; i++)
    cout << a[i] << "\n";
}

int main(void) {
  int i, n, a[ArrayNum];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  shellSort(a, n);
}


Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
#include <vector>

#define ArrayNum 1000000

vector<int> g;

int insertionSort(int a[], int n, int g) {
  int i, j, cnt = 0, v;

  for (i = g; i < n; i++) {
    v = a[i];
    j = i - g;
    while (j >= 0 && a[j] > v) {
      a[j + g] = a[j];
      j = j - g;
      cnt++;
    }
    a[j + g] = v;
  }
  return cnt;
}

void shellSort(int a[], int n) {
  int i, cnt = 0, m;

  for (int h = 1;;) {
    if (h > n)
      break;
    g.push_back(h);
    h = 3 * h + 1;
  }
  m = g.size();
  for (i = m - 1; i >= 0; i--)
    cnt = cnt + insertionSort(a, n, g[i]);

  cout << m << "\n";
  for (i = m - 1; i > 0; i--)
    cout << g[i] << " ";
  cout << g[0] << "\n";
  cout << cnt << "\n";
  for (i = 0; i < n; i++)
    cout << a[i] << "\n";
}

int main(void) {
  int i, n, a[ArrayNum];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> a[i];
  shellSort(a, n);
}