Last active
August 3, 2022 16:57
-
-
Save logbasex/8b8e40cbf2a9506c145ed4c94ab4a992 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
#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