alexjercan/bugnet huggingface dataset:
Problem p02259 (C++) — buggy submission, verdict: Runtime Error

//============================================================================
// Name        : AOJCpp.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

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

void trace(int A[], int n) {
  for (int j = 0; j < n - 1; j++) {
    cout << A[j] << " ";
  }

  cout << A[n - 1] << endl;
}

int main() {

  int n;

  cin >> n;
  int A[n];

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

  int count = 0;
  bool flag = 1;
  int j = 0;
  while (flag) {
    flag = 0;
    for (int i = n - 1; i > j; i--) {
      if (A[i] < A[i - 1]) {
        int tmp = A[i - 1];
        A[i - 1] = A[i];
        A[i] = tmp;
        flag = 1;
        count++;
      }
    }
    j++;
  }
  trace(A, n);
  cout << count << endl;
}


Error: -11

Fixed submission:

//============================================================================
// Name        : AOJCpp.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 100;

void trace(int A[], int n) {
  for (int j = 0; j < n - 1; j++) {
    cout << A[j] << " ";
  }

  cout << A[n - 1] << endl;
}

int main() {

  int n;
  int A[MAX];

  cin >> n;

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

  int count = 0;
  bool flag = 1;
  int j = 0;
  while (flag) {
    flag = 0;
    for (int i = n - 1; i > j; i++) {
      if (A[i] < A[i - 1]) {
        int tmp = A[i - 1];
        A[i - 1] = A[i];
        A[i] = tmp;
        flag = 1;
        count++;
      }
    }
    j++;
  }
  trace(A, n);
  cout << count << endl;
}
Problem p02260 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

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

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

int main() {
  int n, num_sec[101] = {}, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> num_sec[i];
  cnt = selectionSort(num_sec, n);
  cout << cnt << endl;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

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

int selectionSort(int A[], int N) {
  int minj, jnum, cnt = 0;
  for (int i = 0; i < N; i++) {
    minj = i;
    for (int j = i; j < N; j++) {
      if (A[j] < A[minj]) {
        jnum = j;
        cnt += 1;
      }
    }
    swap(A[minj], A[jnum]);
  }
  return cnt;
}

int main() {
  int n, num_sec[101] = {}, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> num_sec[i];
  cnt = selectionSort(num_sec, n);
  cout << cnt << endl;
}
Problem p02260 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>

using namespace std;
static const int N = 100;

void inputArray(int a[], int n) {
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
}

void swap(int a, int b) {
  int v;
  v = a;
  a = b;
  b = v;
}

void printArray(int a[], int n) {
  int k = 0;
  while (1) {
    if (k == n - 1)
      break;
    else {
      printf("%d ", a[k]);
    }
    k++;
  }
  printf("%d\\n", a[n - 1]);
}

int selectionSort(int a[], int n) {
  int minj, count = 0, v;
  for (int i = 0; i < n; ++i) {
    minj = i;
    for (int j = i; j < n; ++j) {
      if (a[j] < a[minj]) {
        minj = j;
      }
    }
    if (i != minj) {
      v = a[i];
      a[i] = a[minj];
      a[minj] = v;
      count++;
    }
  }
  return count;
}

int main(int argc, char const *argv[]) {
  int A[N];
  int n, m;

  cin >> n;
  inputArray(A, n);
  m = selectionSort(A, n);
  printArray(A, n);
  printf("%d\\n", m);

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;
static const int N = 100;

void inputArray(int a[], int n) {
  for (int i = 0; i < n; ++i)
    scanf("%d", &a[i]);
}

void swap(int a, int b) {
  int v;
  v = a;
  a = b;
  b = v;
}

void printArray(int a[], int n) {
  int k = 0;
  while (1) {
    if (k == n - 1)
      break;
    else {
      printf("%d ", a[k]);
    }
    k++;
  }
  printf("%d\\n", a[n - 1]);
}

int selectionSort(int a[], int n) {
  int minj, count = 0;
  for (int i = 0; i < n - 1; ++i) {
    minj = i;
    for (int j = i; j < n - 1; ++i) {
      if (a[j] < a[minj]) {
        minj = j;
      }
    }
    if (i != minj) {
      swap(a[i], a[minj]);
      count++;
    }
  }
  return count;
}

int main(int argc, char const *argv[]) {
  int A[N];
  int n, m;

  cin >> n;
  inputArray(A, n);
  m = selectionSort(A, n);
  printArray(A, n);
  cout << m << endl;

  return 0;
}
Problem p02260 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>、
using namespace std;
int main() {
  int N, res;   // 数字个数、交换次数
  int num[150]; // 储存数字
  cin >> N;
  for (int i = 0; i < N; i++) { // 读取数据
    cin >> num[i];
  }
  for (int i = 0; i < N; i++) {
    int minIndex = i;             // 当前位置后最小数的索引
    for (int j = i; j < N; j++) { // 寻找更小的数
      if (num[j] < num[minIndex]) {
        minIndex = j;
      }
    }
    if (minIndex != i) { // 找到更小的数则进行交换
      num[i] ^= num[minIndex];
      num[minIndex] ^= num[i];
      num[i] ^= num[minIndex];
      res++; // 计数
    }
  }
  for (int i = 0; i < N; i++) { // 输出数组
    cout << num[i];
    if (i < N - 1)
      cout << ' ';
  }
  cout << "\\n" << res << endl; // 输出交换次数
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>

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

int main() {
  int N, A[100], 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, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
const int MAX = 100;

int main() {
  int n, mini, temp, count = 0;
  int A[MAX];

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

  for (int i = 0; i < n; i++) {
    mini = i;
    for (int j = i + 1; j < n; j++) {
      if (A[mini] > A[j])
        mini = j;
    }
    if (i != mini) {
      count++;
      temp = A[mini];
      A[mini] = A[i];
      A[i] = temp;
    }
  }

  for (int i = 0; i < n - 1; i++) {
    cout << A[i] << ' ';
  }
  cout << A[n - 1] << endl;
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const int MAX = 100;

int main() {
  int n, mini, temp, count = 0;
  int A[MAX];

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

  for (int i = 0; i < n; i++) {
    mini = i;
    for (int j = i + 1; i < n; j++) {
      if (A[mini] > A[j])
        mini = j;
    }
    if (i != mini) {
      count++;
      temp = mini;
      A[i] = A[mini];
      A[mini] = temp;
    }
  }

  for (int i = 0; i < n - 1; i++) {
    cout << A[i] << ' ';
  }
  cout << A[n - 1] << endl;
  cout << count << endl;
  return 0;
}
Problem p02260 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

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

int main() {
  int N, A[100], 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;
  cout << sw << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

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

int main() {
  int N, A[100], 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;
  cout << sw << endl;
}
Problem p02260 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

class SelectionSort {
public:
  int sw = 0, minj, t, i, j;

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

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

  cin >> N;

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

  SelectionSort ss;

  sw = ss.Sort(A, N);

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

class SelectionSort {
public:
  int A[100], sw = 0;

  int Sort(int A[], int N) {
    int minj;
    for (int i = 0; i < N; i++) {
      minj = i;
      for (int j = i; i < N; j++) {
        if (A[j] < A[minj]) {
          minj = j;
          swap(A[i], A[minj]);
          sw++;
        }
      }
    }
  }

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

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

  cin >> N;

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

  SelectionSort ss;

  ss.Sort(A, N);

  ss.Print(A, N);
}
Problem p02260 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main() {
  int i, n, min, j, temp, c = 0;
  int a[100];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  for (i = 0; i < n; i++) {

    min = i;

    for (j = i; j < n; j++) {

      if (a[j] < a[min]) {
        min = j;
      }
    }
    temp = a[i];
    a[i] = a[min];
    a[min] = temp;
    if (i != min)
      c++;
  }

  for (i = 0; i < n; i++) {
    if (i == n - 1) {
      printf("%d\\n", a[i]);
      break;
    }
    printf("%d ", a[i]);
  }
  printf("%d\\n", c);
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  int i, n, min, j, temp, c = 0;
  int a[100];
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  for (i = 0; i < n; i++) {
    min = i;
    for (j = i; i < n; j++) {
      if (a[j] < a[min]) {
        min = j;
      }
    }
    temp = a[i];
    a[i] = a[min];
    a[min] = temp;
    if (i != min)
      c++;
  }

  for (i = 0; i < n; i++) {
    if (i == n - 1) {
      printf("%d\\n", a[i]);
      break;
    }
    printf("%d ", a[i]);
  }
  printf("%d\\n", c);
}
Problem p02260 (C++) — buggy submission, verdict: 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; 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;
}

Error: TLE

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; 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;
}
Problem p02260 (C++) — buggy submission, verdict: Runtime Error

/***
 *	???????????????
 *	???????¨??????????????????§?????????????°????minj???????????????????????????????????????????????´???????????¢?????´????????????
 ****/

#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 cnter = 0;
  //-----------------------------------??\\???????????????
  for (int i = 0; i <= (n - 1); i++) {
    int minj = i;
    int p = 0;
    for (int j = i; j <= (n - 1); j++) {

      if (str[j] <
          str[minj]) { //???????????????????????§????°??????????????´?minj????±???????
        minj = j;
        p++;
      }
    }
    if (p != 0) {
      int temp = str[i]; // str[j]??¨str[minj]?????????????´??????????
      str[i] = str[minj];
      str[minj] = temp;
      cnter++;
    } else {
      //???????????????????????´???
    }
  }
  //----------------------------------	???????????????
  for (int i = 0; i < n; i++) {
    cout << str[i];
    if (i != (n - 1)) {
      cout << " ";
    }
  }
  cout << endl;

  cout << cnter << endl;

  return 0;
}

Error: 6

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 cnter = 0;
  //-----------------------------------??\\???????????????
  for (int i = 0; i <= (n - 1); i++) {
    int minj = i;
    for (int j = i; j <= (n - 1); j++) {
      if (str[j] <= str[minj]) {
        minj = j;
      }
    }
    int temp = str[i]; // str[j]??¨str[minj]?????????????´??????????
    str[i] = str[minj];
    str[minj] = temp;
    cnter++;
  }
  return cnter;
  //----------------------------------	???????????????
  for (int i = 0; i < n; i++) {
    cout << str[i];
    if (i != (n - 1)) {
      cout << " ";
    }
  }
  cout << endl;

  cout << cnter << endl;

  return 0;
}
//?????????????????¨??????????????£??¢????????????ex.????????°?????????????????????
Problem p02260 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int b = 0, n, i, minj, j, minv, swap;
  int a[100];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n > 1) {
    for (i = 0; i < n - 1; i++) {
      minv = a[i + 1];
      minj = i + 1;
      for (j = i + 1; j < n; j++) {
        if (minv > a[j]) {
          minv = a[j];
          minj = j;
        }
      }
      if (minv < a[i]) {
        swap = a[i];
        a[i] = a[minj];
        a[minj] = swap;
        b++;
      }
      /*for(int k=0;k<n-1;k++){
          cout<<a[k]<<" "<<flush;
      }
      cout<<a[n-1]<<endl;*/
    }
  } else {
    cout << a[0] << endl;
    cout << 0 << endl;
    return 0;
  }
  for (i = 0; i < n - 1; i++) {
    cout << a[i] << " " << flush;
  }
  cout << a[n - 1] << endl;
  cout << b << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int b = 0, n, i, minj, j, minv, swap;
  int a[100];
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    minv = a[i + 1];
    for (j = i + 1; j < n; j++) {
      if (minv > a[j]) {
        minv = a[j];
        minj = j;
      }
    }
    if (minv < a[i]) {
      swap = a[i];
      a[i] = a[minj];
      a[minj] = swap;
      b++;
    }
  }
  for (i = 0; i < n - 1; i++) {
    cout << a[i] << " " << flush;
  }
  cout << a[n - 1] << endl;
  cout << b << endl;
  return 0;
}
Problem p02260 (C++) — buggy submission, verdict: Runtime Error


#include <iostream>
using namespace std;
int selectionSort(int a[], int N) {
  int sw = 0;
  int temp;

  for (int j = 0; j < N - 1; j++) //??????????¬???°
  {
    int k = j;
    for (int i = j + 1; i < N; i++) {
      if (a[i] < a[k]) {
        k = i;
      }
    }
    temp = a[j];
    a[j] = a[k];
    a[k] = temp;
    if (j != k)
      sw++;
  }
  return sw;
}

int main() {
  int N, sw;
  cin >> N;
  int a[100];
  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;
  cout << sw << endl;
}

Error: 0

Fixed submission:



#include <iostream>
using namespace std;
int selectionSort(int a[], int N) {
  int sw = 0;
  int m;

  for (int j = 0; j < N - 1; j++) {
    int min = a[j];
    for (int i = j + 1; i < N; i++) {

      if (a[i] < min) {
        min = a[i];
        m = i;
      }
    }
    if (a[m] < a[j]) {
      swap(a[m], a[j]);
      sw++;
    }
  }
  return sw;
}

int main() {
  int N, sw;
  cin >> N;
  int a[100];
  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;
  cout << sw << endl;
}
Problem p02260 (C++) — buggy submission, verdict: 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];
    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;
}

Error: WA

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];
    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 p02260 (C++) — buggy submission, verdict: 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;
}

Error: -11

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, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
using namespace std;

int main() {
  int A[100];
  int n;
  int minj;
  int ch;

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  ch = 0;
  for (int i = 0; i < n; i++) {
    minj = i;
    for (int j = i; j < n; j++) {
      if (A[j] < A[minj]) {
        minj = j;
      }
    }
    if (i != minj)
      ch++;
    int sub = A[i];
    A[i] = A[minj];
    A[minj] = sub;
  }

  for (int i = 0; i < n; i++) {
    printf("%d", A[i]);
    if (i + 1 < n)
      printf(" ");
    else
      printf("\\n");
  }
  printf("%d\\n", ch);

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstdlib>
using namespace std;

int main() {
  int *A;
  int n;
  int minj;
  int ch;

  scanf("%d", &n);
  A = (int *)malloc(sizeof(int));
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  ch = 0;
  for (int i = 0; i < n; i++) {
    minj = i;
    for (int j = i; j < n; j++) {
      if (A[j] < A[minj]) {
        minj = j;
      }
    }
    if (i != minj)
      ch++;
    int sub = A[i];
    A[i] = A[minj];
    A[minj] = sub;
  }

  for (int i = 0; i < n; i++) {
    printf("%d", A[i]);
    if (i + 1 < n)
      printf(" ");
    else
      printf("\\n");
  }
  printf("%d\\n", ch);

  return 0;
}
Problem p02260 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int selectionSort(int A[], int N) {
  int sw = 0;
  for (int i = 0; i <= N - 1; i++) {
    int minj = i;
    for (int j = i; j <= N - 1; j++) {
      if (A[j] < A[minj]) {
        minj = j;
      }
    }
    //????´????????????????
    if (A[i] != A[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;
  cout << sw << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int selectionSort(int A[], int N) {
  int sw = 0;

  for (int i = 0; N - 1; i++) {
    int minj = i;
    for (int j = i; j < N - 1; j++) {
      if (A[j] < A[minj]) {
        minj = j;
        //????´????????????????
        swap(A[i], A[j]);
        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;
  cout << sw << endl;

  return 0;
}