Skip to content

Instantly share code, notes, and snippets.

@logbasex
Last active August 3, 2022 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save logbasex/8b8e40cbf2a9506c145ed4c94ab4a992 to your computer and use it in GitHub Desktop.
Save logbasex/8b8e40cbf2a9506c145ed4c94ab4a992 to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
int sortedArr[n];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
sortedArr[0] = arr[0];
for(int i = 0; i < n; i++) {
if ((i + 1) < n && sortedArr[i] <= arr[i+1]) {
sortedArr[i + 1] = arr[i + 1];
continue;
} else {
for (int j = i; j > -1; j--) {
if ((i + 1) < n && sortedArr[j] > arr[i+1]) {
int temp = sortedArr[j];
sortedArr[j] = arr[i+1];
sortedArr[j + 1] = temp;
}
}
}
}
for (int i = 0; i < n; i++) {
cout << sortedArr[i] << " ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment