fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. mt19937_64 rd(time(0));
  5. int rand(int L, int R){
  6. return L + rd() % (R - L + 1);
  7. }
  8.  
  9. void gnome_sort(int* arr, int lenght){
  10. for(int pos = 0; pos < lenght;){
  11. if(pos == 0) pos++;
  12. else if(arr[pos - 1] >= arr[pos]) pos++;
  13. else swap(arr[pos - 1], arr[pos]), pos--;
  14. }
  15. }
  16.  
  17. bool isSorted(int* arr, int length){
  18. for(int i = 1; i < length; i++){
  19. if(arr[i - 1] < arr[i]) return 0;
  20. }
  21.  
  22. return 1;
  23. }
  24.  
  25. int n, arr[10000];
  26.  
  27. int main(){
  28. ios_base::sync_with_stdio(0);
  29. cin.tie(0); cout.tie(0);
  30.  
  31. cin >> n;
  32. for(int i = 0; i < n; i++) cin >> arr[i];
  33.  
  34. while(!isSorted(arr, n)){
  35. gnome_sort(arr, n);
  36. }
  37.  
  38. for(int i = 0; i < n; i++) cout << arr[i] << ' ';
  39.  
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty