-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Assignment.cpp
More file actions
225 lines (196 loc) · 7.29 KB
/
Copy pathArray_Assignment.cpp
File metadata and controls
225 lines (196 loc) · 7.29 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
PROBLEM STATEMENT:
Consider an algorithm that sorts an array of n elements by finding the smallest and largest elements
and then exchanges those elements with the elements in the first and last positions in the array.
Then the size of the array is reduced by two elements after excluding the two elements that are already
in the proper positions, and the process is repeated on the remaining part of the array until the entire array is sorted.
*/
#include<iostream>
#include<array>
#include<vector>
using namespace std;
int ar_Sorted[] ={};
int f_SmallesIndexValue(int arg[],int length)
{
int var_SmallestValue;
for(int k=0; k<length-1; k++)
{
if(arg[k+1]!= arg[length] && arg[k+1]<arg[k])
{
if(var_SmallestValue == 0 || var_SmallestValue>arg[k+1])
var_SmallestValue = arg[k+1];
else
var_SmallestValue = var_SmallestValue;
}
else if(arg[k]!= arg[length] && arg[k+1]<arg[k])
{
if(var_SmallestValue>arg[k+1] || var_SmallestValue == 0)
var_SmallestValue = arg[k+1];
else
var_SmallestValue =- var_SmallestValue;
}
else if(arg[k]!= arg[length] && var_SmallestValue !=0)
{
if ( k == 0 || var_SmallestValue>arg[k])
var_SmallestValue = arg[k];
else if(arg[k+1]<var_SmallestValue)
var_SmallestValue = arg[k+1];
else
var_SmallestValue = var_SmallestValue;
}
else
var_SmallestValue = arg[k];
}
cout <<"The smallest value is: " << var_SmallestValue <<endl;
for(int l=0; l<=length; ++l)
{
if(arg[l] == var_SmallestValue)
{
arg[l]=arg[0];
arg[0] = var_SmallestValue;
}
}
return arg[length];
}
int f_LargestIndexValue(int arg[],int length)
{
int var_LargestValue =0;
arg[length] = 0;
for(int k=0; k<length ; k++)
{
if(arg[k+1]> arg[k] && arg[k+1]>var_LargestValue)
{
if (arg[k+1]== arg[length])
var_LargestValue = var_LargestValue;
else
var_LargestValue = arg[k+1];
}
else if(arg[k+1]<arg[k] && arg[k]>var_LargestValue)
var_LargestValue = arg[k];
}
cout << "The largest value is: " << var_LargestValue <<endl;
for(int l=0; l<length; ++l)
{
if(arg[l] == var_LargestValue)
{
arg[l]=arg[length-1];
arg[length-1] = var_LargestValue;
}
}
return arg[length];
}
int main()
{
int var_N,var_ArrayIndex;
int counter = 0;
cout << "Please Enter The Numbers of Element(n):";
cin >> var_N;
while(var_N <= 1)
{
cout << "The number of indexes should be more than 1.Please Enter again: ";
cin >>var_N;
}
int ar_Input[var_N], ar_Final[var_N];
for(int i = 0; i<var_N; ++i)
{
cout << "Fill Up the array indexes: " << "[" << i << "]";
cin >> var_ArrayIndex;
ar_Input[i] = var_ArrayIndex;
ar_Final[i] = -1;
}
cout << "The ORIGINAL ARRAY is:" ;
for(int j = 0 ; j<var_N; j++)
{
cout << ar_Input[j] << '\0';
}
for(int var_loopcount = 1, var_CutOff = 0; var_loopcount<=var_N - 1; ++var_loopcount, var_CutOff =var_CutOff+2)
{
int var_loopcount1 = 1;
if(var_N - var_CutOff >= 2)
{
if(var_loopcount>1)
{
cout<<'\n';
cout<< " *** The length of the array is " << var_N - var_CutOff << endl;
f_SmallesIndexValue(ar_Sorted, var_N-var_loopcount); //Calling to get the smallest value
f_LargestIndexValue(ar_Sorted,var_N - var_CutOff );// Calling to get the largest value.
if(counter<=var_N)
{
if(ar_Final[var_loopcount -1]==-1 && ar_Final[var_N-var_loopcount]==-1)
{
ar_Final[var_loopcount -1] = ar_Sorted[0];
ar_Final[var_N-var_loopcount] = ar_Sorted[var_N - var_CutOff-1];
counter = counter+2;
}
}
}
else
{
cout<< " *** The length of the array is: " << var_N <<endl;
f_SmallesIndexValue(ar_Input, var_N); //Calling to get the smallest value
ar_Final[0] = ar_Input[0]; //The smallest value after the first iteration
f_LargestIndexValue(ar_Input,var_N);
ar_Final[var_N-1] = ar_Input[var_N-1]; //The largest value after the first iteration
counter = counter+2;
}
cout << "The new array after finding[" << var_loopcount << "]iteration of finding smallest and largest index function : ";
if(var_loopcount>1)
{
for(int m = 0; m<var_N - var_CutOff; m++)
{
cout << ar_Sorted[m] << '\0';
}
}
else
{
for(int m = 0; m<var_N; m++)
{
cout << ar_Input[m] << '\0';
}
}
cout << '\n';
//The new array to be sent to any outer function
if(var_N - var_CutOff > 2)
{
if(var_loopcount<var_N-1)
{
if(var_loopcount>1)
{
for (int x=0; x<var_N-(var_N - var_CutOff) ;++x)
{
ar_Sorted[x] = ar_Sorted[var_loopcount1];
var_loopcount1++;
}
cout<<"The new array to sort: ";
for(int y =0; y<var_N-(var_N - var_CutOff); ++y)
{
cout<<ar_Sorted[y] << '\0';
}
}
else
{
for (int x=0; x<var_N - (var_loopcount+1);++x)
{
ar_Sorted[x] = ar_Input[var_loopcount1];
var_loopcount1++;
}
cout<<"The new array to sort: ";
for(int y =0; y<var_N - (var_loopcount+1); ++y)
{
cout<<ar_Sorted[y] << '\0';
}
}
cout<<'\n';
}
}
else{};
}
}
cout << "The Final Array is: " ;
for(int d = 0 ; d<var_N; d++)
{
cout << ar_Final[d] << '\0';
}
cout << '\n';
return 0;
}