Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void OddEvenSort(int a[], int n) {
- bool is_sorted = false;
- while (!is_sorted) {
- is_sorted = true;
- // even-positions
- for (int i = 0; i + 1 < n; i += 2) {
- if (a[i] > a[i + 1]) {
- swap(a[i], a[i + 1]);
- is_sorted = false;
- }
- }
- // odd positions
- for (int i = 1; i + 1 < n; i += 2) {
- if (a[i] > a[i + 1]) {
- swap(a[i], a[i + 1]);
- is_sorted = false;
- }
- }
- }
- }
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int n;
- cin >> n;
- int a[n];
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- OddEvenSort(a, n);
- for (int i = 0; i < n; i++) {
- cout << a[i] << " ";
- }
- cout << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement