File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+ int main ()
4+ {
5+ int n;
6+ cout << " Enter number of elements: " ;
7+ cin >> n;
8+ int *arr = new int [n];
9+ cout<<" \n Enter " << n <<" elements\n " ;
10+ for (int i = 0 ; i < n; i++)
11+ cin >> arr[i];
12+ cout<<" \n Original List\n " ;
13+ for (int i=0 ;i<n;i++)
14+ cout <<arr[i]<<" " ;
15+ for (int i=1 ; i<n; i++)
16+ {
17+ int temp = arr[i];
18+ int j= i-1 ;
19+ while (j>=0 && temp <= arr[j])
20+ {
21+ arr[j+1 ] = arr[j];
22+ j = j-1 ;
23+ }
24+ arr[j+1 ] = temp;
25+ }
26+ cout<<" \n Sorted List in Ascending Order\n " ;
27+ for (int i=0 ;i<n;i++)
28+ cout <<arr[i]<<" " ;
29+ for (int i=1 ; i<n; i++)
30+ {
31+ int temp = arr[i];
32+ int j= i-1 ;
33+ while (j>=0 && temp >= arr[j])
34+ {
35+ arr[j+1 ] = arr[j];
36+ j = j-1 ;
37+ }
38+ arr[j+1 ] = temp;
39+ }
40+ cout<<" \n Sorted List in Descending Order\n " ;
41+ for (int i=0 ;i<n;i++)
42+ cout <<arr[i]<<" " ;
43+ }
You can’t perform that action at this time.
0 commit comments