Last active
March 10, 2021 12:45
-
-
Save lectricas/f3a1b90b39ddf57b2018d02ef2bfc327 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by lectr on 10.03.2021. | |
// | |
#include <iostream> | |
#include <vector> | |
void sort(std::vector<int> &arr, int n) { | |
bool isSorted = true; | |
for (int j = 0; j < n - 1; j++) { | |
if (arr[j] > arr[j + 1]) { | |
isSorted = false; | |
} | |
} | |
if (isSorted) { | |
for (int k = 0; k < n; k++) { | |
std::cout << arr[k] << " "; | |
} | |
std::cout << std::endl; | |
return; | |
} | |
while (true) { | |
int swaps = 0; | |
int last = n - 1; | |
for (int j = 0; j < last; j++) { | |
if (arr[j] > arr[j + 1]) { | |
int temp = arr[j]; | |
arr[j] = arr[j + 1]; | |
arr[j + 1] = temp; | |
swaps++; | |
} | |
} | |
if (swaps == 0) { | |
break; | |
} | |
for (int k = 0; k < n; k++) { | |
std::cout << arr[k] << " "; | |
} | |
std::cout << std::endl; | |
last--; | |
} | |
} | |
int main() { | |
int n; | |
std::cin >> n; | |
std::vector<int> arr(n); | |
for (int i = 0; i < n; i++) { | |
std::cin >> arr[i]; | |
} | |
sort(arr, n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment