// C++ program to implement linear
// search in unsorted array
#include < bits/stdc++.h>
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 << "Element not found";
else
cout << "Element Found at Position: "
<< position + 1;
return 0;
}
}
// C program to implement insert
// operation in an unsorted array.
#include < stdio.h>
// 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;
printf("\n Before Insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
// Inserting key
n = insertSorted(arr, n, key, capacity);
printf("\n After Insertion: ");
for (i = 0; i < n; i++)
printf("%d ",arr[i]);
return 0;
}
}
// C++ program to implement linear
// search in unsorted array
#include < bits/stdc++.h>
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 << "Element not found";
else
cout << "Element Found at Position: "
<< position + 1;
return 0;
}
}
// Java program to implement insert
// operation in an unsorted array.
class Main
{
// Function to insert a given key in
// the array. This function returns n+1
// if insertion is successful, else n.
static 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
public static void main(String[] args)
{
int[] arr = new int[20];
arr[0] = 12;
arr[1] = 16;
arr[2] = 20;
arr[3] = 40;
arr[4] = 50;
arr[5] = 70;
int capacity = 20;
int n = 6;
int i, key = 26;
System.out.print("Before Insertion:");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
// Inserting key
n=insertSorted(arr, n, key, capacity);
System.out.print("\n After Insertion:");
for (i = 0; i < n; i++)
System.out.print(arr[i]+" ");
}
}
Before Insertion: 12 16 20 40 50 70
After Insertion: 12 16 20 40 50 70 26