Language: C++

Last login: Sun Jan 28 19:33:13 on console
mac-2:~ aito$ cd c++
mac-2:c++ aito$ cd aoj/
mac-2:aoj aito$ ls
algorithms	intro
mac-2:aoj aito$ cd algorithms/
mac-2:algorithms aito$ ls
a.out			insert_sort.cpp~	sort_stable.cpp
bubble_sort.cpp		quick_sort.cpp		sort_stable.cpp~
insert_sort.cpp		selection_sort.cpp
mac-2:algorithms aito$ emacs sort_stable.cpp

[1]+  Stopped                 emacs sort_stable.cpp
mac-2:algorithms aito$ g++ sort_stable.cpp
sort_stable.cpp:51:1: warning: control may reach end of non-void function
      [-Wreturn-type]
}
^
1 warning generated.
mac-2:algorithms aito$ fg
emacs sort_stable.cpp

[1]+  Stopped                 emacs sort_stable.cpp
mac-2:algorithms aito$ g++ sort_stable.cpp
mac-2:algorithms aito$ ./a.aut
-bash: ./a.aut: No such file or directory
mac-2:algorithms aito$ ./a.out 
5
H4 C9 S4 D2 C3
D2 C3 H4 S4 C9
Stable
mac-2:algorithms aito$ fg
emacs sort_stable.cpp

[1]+  Stopped                 emacs sort_stable.cpp
mac-2:algorithms aito$ g++ sort_stable.cpp
sort_stable.cpp:53:1: warning: control may reach end of non-void function
      [-Wreturn-type]
}
^
1 warning generated.
mac-2:algorithms aito$ fg
emacs sort_stable.cpp

[1]+  Stopped                 emacs sort_stable.cpp
mac-2:algorithms aito$ g++ sort_stable.cpp
mac-2:algorithms aito$ ./a.out 
5
H4 C9 S4 D2 C3
D2 C3 H4 S4 C9
Stable
mac-2:algorithms aito$ fg
emacs sort_stable.cpp

[1]+  Stopped                 emacs sort_stable.cpp
mac-2:algorithms aito$ g++ sort_stable.cpp
mac-2:algorithms aito$ ./a.out 
5
H4 C9 S4 D2 C3
D2 C3 H4 S4 C9
Stable
D2 C3 H4 S4 C9
Stable
mac-2:algorithms aito$ fg
emacs sort_stable.cpp

[1]+  Stopped                 emacs sort_stable.cpp
mac-2:algorithms aito$ g++ sort_stable.cpp
sort_stable.cpp:99:3: error: use of undeclared identifier 'insertion_sort'; did
      you mean 'selection_sort'?
  insertion_sort(sorted, n);
  ^~~~~~~~~~~~~~
  selection_sort
sort_stable.cpp:26:6: note: 'selection_sort' declared here
void selection_sort(Card cards[], int n) {
     ^
1 error generated.
mac-2:algorithms aito$ fg
emacs sort_stable.cpp

[1]+  Stopped                 emacs sort_stable.cpp
mac-2:algorithms aito$ g++ sort_stable.cpp
mac-2:algorithms aito$ ./a.out 
5
H4 C9 S4 D2 C3
D2 C3 H4 S4 C9
Stable
D2 C3 S4 H4 C9
Not Stable
mac-2:algorithms aito$ less sort_stable.cpp
mac-2:algorithms aito$ less sort_stable.cpp
mac-2:algorithms aito$ less sort_stable.cpp
mac-2:algorithms aito$ less sort_stable.cpp
mac-2:algorithms aito$ fg
emacs sort_stable.cpp

#include <iostream>
#include <cassert>

struct Card {
  char suit;
  int value;
};


void bubble_sort(Card cards[], const int n) {
  bool sorted;

  while(!sorted) {
    sorted = true;
    for (int i = n - 1; i > 0; i--) {
      if (cards[i].value < cards[i - 1].value) {
        Card tmp = cards[i];
        cards[i] = cards[i - 1];
	cards[i - 1] = tmp;
	sorted = false;
      }
    }
  }
}

void selection_sort(Card cards[], int n) {
  for (int i = 0; i < n; i++) {
    int min_index = i;
    for (int j = i + 1; j < n; j++) {
      if (cards[j].value < cards[min_index].value) {
	min_index = j;
      }
    }
    if (cards[min_index].value < cards[i].value) {
      Card tmp = cards[i];
      cards[i] = cards[min_index];
      cards[min_index] = tmp;
    }
  }
}


bool is_stable(Card input[], Card sorted[], int n) {
  for (int i = 0; i < n - 1; i++) {
    if (sorted[i].value == sorted[i+1].value) {
      for (int j = 0; j < n; j++) {
        if (input[j].value == sorted[i].value) {
          if (input[j].suit == sorted[i].suit) {
            return true;
          } else if (input[j].suit == sorted[i+1].suit) {
            return false;
          }
        }
      }
    }
  }
  assert(true);
  return false;
}


int main() {
  const int BUF_SIZE = 36;
  Card input[BUF_SIZE];
  int n;
  Card sorted[BUF_SIZE];
 bool stable = false;

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> input[i].suit >> input[i].value;
  }

  for (int i = 0; i < n; i++) {
    sorted[i] = input[i];
  }

  bubble_sort(sorted, n);

  for (int i = 0; i < n; i++) {
    if (i == n - 1) {
      std::cout << sorted[i].suit << sorted[i].value << std::endl;
    }
    else {
      std::cout << sorted[i].suit << sorted[i].value << " ";
    }
  }

 if (is_stable(input, sorted, n)) {
    std::cout << "Stable" << std::endl;
  } else {
    std::cout << "Not Stable" << std::endl;
  }

  for (int i = 0; i < n; i++) {
    sorted[i] = input[i];
  }

  selection_sort(sorted, n);

  for (int i = 0; i < n; i++) {
    if (i == n - 1) {
      std::cout << sorted[i].suit << sorted[i].value << std::endl;
    }
    else {
      std::cout << sorted[i].suit << sorted[i].value << " ";
    }
  }

  if (is_stable(input, sorted, n)) {
    std::cout << "Stable" << std::endl;
  } else {
    std::cout << "Not stable" << std::endl;
  }

  return 0;
}





Compiler output:
a.cc:69:59: warning: multi-character literal with 14 characters exceeds 'int' size of 4 bytes
   69 | sort_stable.cpp:99:3: error: use of undeclared identifier 'insertion_sort'; did
      |                                                           ^~~~~~~~~~~~~~~~
a.cc:70:16: warning: multi-character literal with 14 characters exceeds 'int' size of 4 bytes
   70 |       you mean 'selection_sort'?
      |                ^~~~~~~~~~~~~~~~
a.cc:74:29: warning: multi-character literal with 14 characters exceeds 'int' size of 4 bytes
   74 | sort_stable.cpp:26:6: note: 'selection_sort' declared here
      |                             ^~~~~~~~~~~~~~~~
a.cc:1:1: error: 'Last' does not name a type
    1 | Last login: Sun Jan 28 19:33:13 on console
      | ^~~~
a.cc:17:1: error: expected declaration before '}' token
   17 | }
      | ^
a.cc:18:1: error: expected unqualified-id before '^' token
   18 | ^
      | ^
a.cc:39:1: error: expected declaration before '}' token
   39 | }
      | ^
a.cc:40:1: error: expected unqualified-id before '^' token
   40 | ^
      | ^
a.cc:69:77: error: 'did' does not name a type
   69 | sort_stable.cpp:99:3: error: use of undeclared identifier 'insertion_sort'; did
      |                                                                             ^~~
a.cc:72:3: error: expected unqualified-id before '^' token
   72 |   ^~~~~~~~~~~~~~
      |   ^