BATCH 12 Read the no of
DATA queries for each
STRUCTURES query, Command
"T" Represents
Abstract: To write a insertion. Insert an
algorithm and a c element such that
program to insert, the array will be in
remove, sort and find sorted order only.
the element in the
b) command "R"
array.
Represents
Introduction: Basic of removal. Remove
ARRAY, SORTING and Least element from
SEARCH ELEMENT the array.
Write an Algorithm by c)Implement this by
implementing following using array and
functionalities linked list and
a)Read the input compare both of
array dynamically them.
Test Cases: For query 2, the
Input: number of operations
using array are 6.
6 10 9 31 4 0 6
For query 2, the
2 number of operations
I 41 using linked list are 2.
R Finally, the resultant
Output: queue is "4 6 9 10 31
41“
For query 1, the
number of operations Description:
using array are 0. Sorted array is
For query 1, the 0 4 6 9 10 31
number of operations After 1st query “I 41"
using linked list are 1. array will be 0 4 69 10
31 41. The number of
operations will be zero
by using array because Removing Least and
it is simply inserting at Highest Elements: If
the end. there are at least two
After 2nd query "R" elements in the array, it
array will be 4 6 9 10 31 removes the least and
41 highest elements by
shifting the remaining
Solution: elements. This is done
Input: It first takes the to prepare the array for
number of elements in searching an element in
the array (‘n’) and the the modified array.
array elements as input Search Element: It
from the user. prompts the user to
Sorting: It sorts the enter an element to
array in ascending search for in the
order using a simple modified array. It then
bubble sort algorithm. searches for this
element in the array
and reports its position elements in the array
if found. and store it in n.
3. Declare an integer
array arr of size n.
4. Prompt the user to
enter n elements into
the array and store
them in arr.
5. Print "Array before
sorting:" followed by
ARRAY calling the print Array
function with arr
Algorithm:
and n as arguments.
1. Declare an integer
6. Call the sort Array
variable n.
function with arr and n
2. Prompt the user to as arguments to sort
enter the number of
the array in ascending 9. Print "Array after
order. removing least and
7. Print "Array in highest elements:"
ascending order:" followed by calling the
followed by calling the print Array function
print Array function with arr and n as
with arr and n as arguments.
arguments. 10. Declare an integer
8. If n is greater than or variable search
equal to 2, then do the Element.
following: 11. Prompt the user to
a. Iterate i from 0 to n enter an element to
- 3: search and store it in
search Element.
i. Set arr[i] to arr[i +
1]. 12. Declare an integer
variable found and
b. Decrement n by 2. initialize it to 0.
13. Declare an integer at position in the
variable position and array."
initialize it to -1. 16. If found is false,
14. Iterate i from 0 to n then print "Element
- 1: search Element not
a. If arr[i] is equal to found in the array."
search Element, then 17. End of the program.
do the following: C Program Code :
i. Set found to 1. #include <stdio.h>
ii. Set position to i + void printArray(int arr[],
1. int size)
iii. Break from the { for (int i = 0; i < size; i+
loop. +)
15. If found is true, {
then print "Element
search Element found printf("%d ",
arr[i]);
} temp = arr[i];
printf("\n"); arr[i] = arr[j]; arr[j] =
temp;
}
}
void sortArray(int arr[],
int size) }
{ }
int temp; for (int i = 0; }
i < size - 1; i++) int main()
{ {
for (int j = i + 1; j < int n; printf("Enter the
size; j++) number of elements in
{ the array: ");
scanf("%d", &n); int
if (arr[i] > arr[j]) arr[n];
{
printf("Enter %d // Removing the least
elements into the and highest elements
array:\n", n); if (n >= 2)
for (int i = 0; i < n; i++) {
{ scanf("%d", for (int i = 0; i < n - 2;
&arr[i]); i++)
} {
printf("Array before arr[i] = arr[i + 1];
sorting: ");
}
printArray(arr, n);
n -= 2;
sortArray(arr, n);
}
printf("Array in printf("Array after
ascending order: "); removing least and
highest elements: ");
printArray(arr, n);
printArray(arr, n);
int searchElement; }
printf("Enter an }
element to search: ");
if (found)
scanf("%d",
&searchElement); {
int found = 0; printf("Element %d
found at position %d in
int position = -1; the array.\n",
for (int i = 0; i < n; i++) searchElement,
{ position);
if (arr[i] == }
searchElement) else
{ {
found = 1; position = i + printf("Element %d not
1; found in the array.\n",
break; searchElement);
}
return 0;
}
Conclusion :
By compiling the above
C program code, we get
output as the result and
get the SORTING and
SEARCHING of an
element
Thank you……..