Find the best answers to your questions at Westonci.ca, where experts and enthusiasts provide accurate, reliable information. Discover comprehensive solutions to your questions from a wide network of experts on our user-friendly platform. Join our platform to connect with experts ready to provide precise answers to your questions in different areas.

A line of students are arranged in odd and even positions. Now the students in the odd positions are to be sorted in the descending order and the students in the even position are to be sorted in ascending order, given a 1 D array. The maximum length of the line is 20. Display – ‘Invalid Size', if the input specified is zero or negative. Write an algorithm to implement the above scenario.

Sagot :

Answer:

The algorithm is as follows:

0. Start

1. MyArray = []

2. Input n

3. if n <1 or n > 20:

3.1print("Invalid Size")

4. else:

4.1 for [tex]i = 0[/tex] to n - 1:

 4.1.1 Input MyArray[i]

 4.2 even = []; odd = []

4.3 enum = 0; onum = 0

4.4 for i = 0 to n - 1:

 4.4.1 if i%2 == 0:

  4.4.1.1 even[enum]=list[i]

  4.4.1.2 enum = enum + 1

 4.4.2 else:

  4.4.2.1 odd[onum]=list[i]

  4.4.2.2 onum= onum + 1  

4.5 MyArray.clear()

4.6 enum=0

4.7 while even:

 4.7.1 minm = even[0]

 4.7.2 for x in even:  

  4.7.2.1 if x < minm:

   4.7.2.1.1 minm = x

 4.7.3 MMyArray[enum] = minm

 4.7.4 even.remove(minm)

 4.7.5 enum = enum + 1  

4.8 while odd:

 4.8.1 maxm = odd[0]

 4.8.2 for x in odd:  

  4.8.2.1 if x > maxm:

   4.8.2.1.1 maxm = x

 4.8.3 MMyArray[enum] = maxm

 4.8.4 odd.remove(maxm)

 4.8.5 enum = enum + 1

4.9 for i = 0 to n - 1:

 4.9.1 print MyArray[i]

5. Stop

Explanation:

This starts the algorithm

0. Start

This creates an empty array

1. MyArray = []

This gets input for n (the length of the array)

2. Input n

If n is is less than 1 or greater than 20, then the input is invalid

3. if n <1 or n > 20:

3.1print("Invalid Size")

For valid values of n, we have:

4. else:

The italicized gets input into the array

4.1 for [tex]i = 0[/tex] to n - 1:

 4.1.1 Input MyArray[i]

This creates empty arrays for even index and for odd index

 4.2 even = []; odd = []

This initializes even index and odd index to 0

4.3 enum = 0; onum = 0

This iterates through the array indices

4.4 for i = 0 to n - 1:

If index is even, add array element to even

 4.4.1 if i%2 == 0:

  4.4.1.1 even[enum]=list[i]

  4.4.1.2 enum = enum + 1

If otherwise, add array element to odd

 4.4.2 else:

  4.4.2.1 odd[onum]=list[i]

  4.4.2.2 onum= onum + 1  

Clear elements of MyArray

4.5 MyArray.clear()

Set index to 0

4.6 enum=0

Iterate through the even array

4.7 while even:

Set minimum to the first index

 4.7.1 minm = even[0]

Sort the array in ascending order

 4.7.2 for x in even:  

  4.7.2.1 if x < minm:

   4.7.2.1.1 minm = x

Add the sorted array into MyArray

 4.7.3 MMyArray[enum] = minm

Delete the elements of even array

 4.7.4 even.remove(minm)

 4.7.5 enum = enum + 1  

Iterate through the odd array

4.8 while odd:

Set maximum to the first index

 4.8.1 maxm = odd[0]

Sort the array in descending order

 4.8.2 for x in odd:  

  4.8.2.1 if x > maxm:

   4.8.2.1.1 maxm = x

Add the sorted array into MyArray

 4.8.3 MMyArray[enum] = maxm

Delete the elements of odd array

 4.8.4 odd.remove(maxm)

 4.8.5 enum = enum + 1

Iterate through the indices of the double sorted MyArray

4.9 for i = 0 to n - 1:

Print each array element

 4.9.1 print MyArray[i]

End algorithm

5. Stop

See attachment for the program implemented in Python

View image MrRoyal