Professional Documents
Culture Documents
EXPERIMENT No: 1
Operations on arrays
AIM:
THEORY:
The input function prompts the user to enter elements into an array of
a specified size, while the output function displays the array
elements.
The sort function utilizes the bubble sort algorithm to arrange the
array elements in ascending order.
The del function deletes an element from the array based on the user-
specified index, shifting subsequent elements to fill the gap.
Conversely, the insert function inserts a user-provided element at a
specified index, shifting existing elements to accommodate the new
entry.
ALGORITHM:
1. Initialization: Initialize an array arr of size MAX and variables n, size, t, a, b, c, d, e.
2. Input Initial Size and Elements: Ask the user for the initial size of the array and its
elements. Store the size in n and the elements in the first n positions of arr.
3. Menu Display and Operation Selection: Display a menu with options for insertion,
deletion, sorting, searching, and exit. Ask the user to select an operation.
4. Perform Selected Operation: Based on the user’s selection, perform the corresponding
operation:
o Insertion: Ask the user for the number of elements to be inserted and their
values. For each value, call the insert function to insert it at the end of the array
and increment size.
o Deletion: Ask the user for the number to be deleted. Call the del function to
delete the number from the array. If the number is found and deleted,
decrement size.
o Sorting: Call the bubbleSort function to sort the array in ascending order.
o Searching: Ask the user for the number to be searched. Call the search function
to find the index of the number in the array.
5. Display Array: After each operation (except exit), display the current elements of the
array using the display function.
6. Repeat or Exit: If the user selects exit, end the program. Otherwise, go back to step 3.
The functions used in the program are:
• bubbleSort: This function sorts the array in ascending order using the Bubble Sort
algorithm. It has a time complexity of O(n^2).
• insert: This function inserts a given number at the end of the array. It has a time
complexity of O(1).
• del: This function deletes a given number from the array by shifting all elements after it
one position to the left. It has a time complexity of O(n).
• search: This function finds a given number in the array and returns its index. It has a
time complexity of O(n).
• display: This function prints all elements of the array. It has a time complexity of O(n).
CODE:
Output:
EXAMPLE:
3. Displaying elements
13254
In this example, the user first specifies the size of the array (step 1), then
inputs the elements of the array (step 2). The program displays the entered
elements (step 3) and allows the user to search for a specific element (step
4).
After that, the program sorts the array in ascending order (step 5), deletes
an element at a specified index (step 6), and finally inserts a new element
at a specified index (step 7).
CONCLUSION: