-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathollie23.cpp
45 lines (41 loc) · 830 Bytes
/
ollie23.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: " << endl;
for(int i = 0 ; i < n ; ++i)
{
cin >> arr[i];
}
for(int i = 1 ; i < n ; ++i)
{
int curr = arr[i];
for(int j = i-1 ; j >= 0 ; --j)
{
if(arr[j] > curr)
{
arr[j+1] = arr[j];
if(j == 0)
{
arr[j] = curr;
}
}
else
{
arr[j+1] = curr;
break;
}
}
}
cout << "Sorted array in ascending order: " << endl;
for(int i = 0 ; i < n ; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}