What should be returned by the list operation ( mylist.insert(2,x) ).getentry(2) ?

Search, insert and delete in an unsorted array

In this post search, insert, and delete operations in an unsorted array are discussed.

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Search Operation

In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element.


C++




// C++ program to implement linear
// search in unsorted array
#include
using namespace std;
// Function to implement search operation
int findElement[int arr[], int n,
int key]
{
int i;
for [i = 0; i < n; i++]
if [arr[i] == key]
return i;
return -1;
}
// Driver Code
int main[]
{
int arr[] = {12, 34, 10, 6, 40};
int n = sizeof[arr] / sizeof[arr[0]];
// Using a last element as search element
int key = 40;
int position = findElement[arr, n, key];
if [position == - 1]
cout
Javascript




// Javascript program to implement linear
// search in unsorted array
// Function to implement search operation
function findElement[ arr, n, key]
{
let i;
for [i = 0; i < n; i++]
if [arr[i] == key]
return i;
return -1;
}
// Driver program
let arr = [12, 34, 10, 6, 40];
let n = arr.length;
// Using a last element as search element
let key = 40;
let position = findElement[arr, n, key];
if [position == - 1]
document.write["Element not found"];
else
document.write["Element Found at Position: "
+ [position + 1]];

Output:



Element Found at Position: 5

Insert at the end

In an unsorted array, the insert operation is faster as compared to a sorted array because we don’t have to care about the position at which the element is to be placed.


C++




// C++ program to implement insert
// operation in an unsorted array.
#include
using namespace std;
// Inserts a key in arr[] of given capacity.
// n is current size of arr[]. This
// function returns n + 1 if insertion
// is successful, else n.
int insertSorted[int arr[], int n,
int key,
int capacity]
{
// Cannot insert more elements if n is
// already more than or equal to capacity
if [n >= capacity]
return n;
arr[n] = key;
return [n + 1];
}
// Driver Code
int main[]
{
int arr[20] = {12, 16, 20, 40, 50, 70};
int capacity = sizeof[arr] / sizeof[arr[0]];
int n = 6;
int i, key = 26;
cout
Javascript




// Javascript program to implement insert
// operation in an unsorted array.
// Function to insert a given
// key in the array. This
// function returns n + 1
// if insertion is successful,
// else n.
function insertSorted[arr, n, key, capacity]
{
// Cannot insert more elements
// if n is already more than
// or equal to capacity
if [n >= capacity]
return n;
arr[n] = key;
return [n + 1];
}
let arr = new Array[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
let capacity = 20;
let n = 6;
let i, key = 26;
document.write["Before Insertion: "];
for [i = 0; i < n; i++]
document.write[arr[i]+" "];
document.write["
"];
// Inserting key
n = insertSorted[arr, n, key, capacity];
document.write["After Insertion: "];
for [i = 0; i < n; i++]
document.write[arr[i]+" "];

Output:

Before Insertion: 12 16 20 40 50 70 After Insertion: 12 16 20 40 50 70 26

Delete Operation

In the delete operation, the element to be deleted is searched using the linear search, and then delete operation is performed followed by shifting the elements.


C++




// C++ program to implement delete operation in a
// unsorted array
#include
using namespace std;
// To search a key to be deleted
int findElement[int arr[], int n,
int key];
// Function to delete an element
int deleteElement[int arr[], int n,
int key]
{
// Find position of element to be deleted
int pos = findElement[arr, n, key];
if [pos == - 1]
{
cout
Javascript




// Java script program to implement delete
// operation in an unsorted array
// function to search a key to
// be deleted
function findElement[arr,n,key]
{
let i;
for [i = 0; i < n; i++]
if [arr[i] == key]
return i;
return -1;
}
// Function to delete an element
function deleteElement[arr,n,key]
{
// Find position of element to be
// deleted
let pos = findElement[arr, n, key];
if [pos == -1]
{
document.write["Element not found"];
return n;
}
// Deleting element
let i;
for [i = pos; i< n - 1; i++]
arr[i] = arr[i + 1];
return n - 1;
}
// Driver Code
let i;
let arr = [10, 50, 30, 40, 20];
let n = arr.length;
let key = 30;
document.write["Array before deletion
"];
for [i=0; i

Bài mới nhất

Chủ Đề