Front and Back Search in unsorted array
Here given code implementation process.
// C Program
// Front and Back Search in unsorted array
#include <stdio.h>
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf(" %d", arr[i]);
}
printf("\n");
}
// Finding the element of using front and back location
void findElement(int arr[], int size, int element)
{
int result = -1;
// Get index location
int front = 0;
int back = size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while (result == -1 && front < back)
{
if (arr[front] == element)
{
// When get the element
// Get element location
result = front;
}
else if (arr[back] == element)
{
// When get the element
// Get element location
result = back;
}
// Change index location
front++;
back--;
}
if (result != -1)
{
printf(" Element %d exist in location of %d \n", element, result);
}
else
{
printf(" Element %d are not exist \n", element);
}
}
int main()
{
// Define collection of array elements
int arr[] = {
6 , 4 , 8 , 2 , 0 , 7 , 3 , 9
};
// Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
printf(" Array Element \n");
printArray(arr, size);
// Find element 4
findElement(arr, size, 4);
// Find element 3
findElement(arr, size, 3);
// Find element 5
findElement(arr, size, 5);
return 0;
}
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
/*
Java Program
Front and Back Search in unsorted array
*/
public class SearchElement
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
// Finding the element of using front and back location
public void findElement(int[] arr, int size, int element)
{
int result = -1;
// Get index location
int front = 0;
int back = size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while (result == -1 && front < back)
{
if (arr[front] == element)
{
// When get the element
// Get element location
result = front;
}
else if (arr[back] == element)
{
// When get the element
// Get element location
result = back;
}
// Change index location
front++;
back--;
}
if (result != -1)
{
System.out.print(" Element " + element + " exist in location of " + result + " \n");
}
else
{
System.out.print(" Element " + element + " are not exist \n");
}
}
public static void main(String[] args)
{
SearchElement task = new SearchElement();
// Define collection of array elements
int[] arr =
{
6 , 4 , 8 , 2 , 0 , 7 , 3 , 9
};
// Get the size of array
int size = arr.length;
System.out.print(" Array Element \n");
task.printArray(arr, size);
// Find element 4
task.findElement(arr, size, 4);
// Find element 3
task.findElement(arr, size, 3);
// Find element 5
task.findElement(arr, size, 5);
}
}
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Front and Back Search in unsorted array
*/
class SearchElement
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
// Finding the element of using front and back location
void findElement(int arr[], int size, int element)
{
int result = -1;
// Get index location
int front = 0;
int back = size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while (result == -1 && front < back)
{
if (arr[front] == element)
{
// When get the element
// Get element location
result = front;
}
else if (arr[back] == element)
{
// When get the element
// Get element location
result = back;
}
// Change index location
front++;
back--;
}
if (result != -1)
{
cout << " Element " << element << " exist in location of " << result << " \n";
}
else
{
cout << " Element " << element << " are not exist \n";
}
}
};
int main()
{
SearchElement task = SearchElement();
// Define collection of array elements
int arr[] = {
6 , 4 , 8 , 2 , 0 , 7 , 3 , 9
};
// Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
cout << " Array Element \n";
task.printArray(arr, size);
// Find element 4
task.findElement(arr, size, 4);
// Find element 3
task.findElement(arr, size, 3);
// Find element 5
task.findElement(arr, size, 5);
return 0;
}
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
// Include namespace system
using System;
/*
C# Program
Front and Back Search in unsorted array
*/
public class SearchElement
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
// Finding the element of using front and back location
public void findElement(int[] arr, int size, int element)
{
int result = -1;
// Get index location
int front = 0;
int back = size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while (result == -1 && front < back)
{
if (arr[front] == element)
{
// When get the element
// Get element location
result = front;
}
else if (arr[back] == element)
{
// When get the element
// Get element location
result = back;
}
// Change index location
front++;
back--;
}
if (result != -1)
{
Console.Write(" Element " + element + " exist in location of " + result + " \n");
}
else
{
Console.Write(" Element " + element + " are not exist \n");
}
}
public static void Main(String[] args)
{
SearchElement task = new SearchElement();
// Define collection of array elements
int[] arr = {
6 , 4 , 8 , 2 , 0 , 7 , 3 , 9
};
// Get the size of array
int size = arr.Length;
Console.Write(" Array Element \n");
task.printArray(arr, size);
// Find element 4
task.findElement(arr, size, 4);
// Find element 3
task.findElement(arr, size, 3);
// Find element 5
task.findElement(arr, size, 5);
}
}
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
<?php
/*
Php Program
Front and Back Search in unsorted array
*/
class SearchElement
{
//Display elements of given array
public function printArray( & $arr, $size)
{
for ($i = 0; $i < $size; ++$i)
{
echo " ". $arr[$i];
}
echo "\n";
}
// Finding the element of using front and back location
public function findElement( & $arr, $size, $element)
{
$result = -1;
// Get index location
$front = 0;
$back = $size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while ($result == -1 && $front < $back)
{
if ($arr[$front] == $element)
{
// When get the element
// Get element location
$result = $front;
}
else if ($arr[$back] == $element)
{
// When get the element
// Get element location
$result = $back;
}
// Change index location
$front++;
$back--;
}
if ($result != -1)
{
echo " Element ". $element ." exist in location of ". $result ." \n";
}
else
{
echo " Element ". $element ." are not exist \n";
}
}
}
function main()
{
$task = new SearchElement();
// Define collection of array elements
$arr = array(6, 4, 8, 2, 0, 7, 3, 9);
// Get the size of array
$size = count($arr);
echo " Array Element \n";
$task->printArray($arr, $size);
// Find element 4
$task->findElement($arr, $size, 4);
// Find element 3
$task->findElement($arr, $size, 3);
// Find element 5
$task->findElement($arr, $size, 5);
}
main();
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
/*
Node Js Program
Front and Back Search in unsorted array
*/
class SearchElement
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
// Finding the element of using front and back location
findElement(arr, size, element)
{
var result = -1;
// Get index location
var front = 0;
var back = size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while (result == -1 && front < back)
{
if (arr[front] == element)
{
// When get the element
// Get element location
result = front;
}
else if (arr[back] == element)
{
// When get the element
// Get element location
result = back;
}
// Change index location
front++;
back--;
}
if (result != -1)
{
process.stdout.write(" Element " + element + " exist in location of " + result + " \n");
}
else
{
process.stdout.write(" Element " + element + " are not exist \n");
}
}
}
function main()
{
var task = new SearchElement();
// Define collection of array elements
var arr = [6, 4, 8, 2, 0, 7, 3, 9];
// Get the size of array
var size = arr.length;
process.stdout.write(" Array Element \n");
task.printArray(arr, size);
// Find element 4
task.findElement(arr, size, 4);
// Find element 3
task.findElement(arr, size, 3);
// Find element 5
task.findElement(arr, size, 5);
}
main();
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
# Python 3 Program
# Front and Back Search in unsorted array
class SearchElement :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1
print(end = "\n")
# Finding the element of using front and back location
def findElement(self, arr, size, element) :
result = -1
# Get index location
front = 0
back = size - 1
# The loop is executed until the location of the given element is found
# And (given front and back variable values are under the limit).
# front value is less than back values
while (result == -1 and front < back) :
if (arr[front] == element) :
# When get the element
# Get element location
result = front
elif(arr[back] == element) :
# When get the element
# Get element location
result = back
# Change index location
front += 1
back -= 1
if (result != -1) :
print(" Element", element ,"exist in location of ", result)
else :
print(" Element", element ,"are not exist ")
def main() :
task = SearchElement()
# Define collection of array elements
arr = [6, 4, 8, 2, 0, 7, 3, 9]
# Get the size of array
size = len(arr)
print(" Array Element ")
task.printArray(arr, size)
# Find element 4
task.findElement(arr, size, 4)
# Find element 3
task.findElement(arr, size, 3)
# Find element 5
task.findElement(arr, size, 5)
if __name__ == "__main__": main()
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
# Ruby Program
# Front and Back Search in unsorted array
class SearchElement
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print(" ", arr[i])
i += 1
end
print("\n")
end
# Finding the element of using front and back location
def findElement(arr, size, element)
result = -1
# Get index location
front = 0
back = size - 1
# The loop is executed until the location of the given element is found
# And (given front and back variable values are under the limit).
# front value is less than back values
while (result == -1 && front < back)
if (arr[front] == element)
# When get the element
# Get element location
result = front
elsif(arr[back] == element)
# When get the element
# Get element location
result = back
end
# Change index location
front += 1
back -= 1
end
if (result != -1)
print(" Element ", element ," exist in location of ", result ," \n")
else
print(" Element ", element ," are not exist \n")
end
end
end
def main()
task = SearchElement.new()
# Define collection of array elements
arr = [6, 4, 8, 2, 0, 7, 3, 9]
# Get the size of array
size = arr.length
print(" Array Element \n")
task.printArray(arr, size)
# Find element 4
task.findElement(arr, size, 4)
# Find element 3
task.findElement(arr, size, 3)
# Find element 5
task.findElement(arr, size, 5)
end
main()
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
/*
Scala Program
Front and Back Search in unsorted array
*/
class SearchElement
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
// Finding the element of using front and back location
def findElement(arr: Array[Int], size: Int, element: Int): Unit = {
var result: Int = -1;
// Get index location
var front: Int = 0;
var back: Int = size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while (result == -1 && front < back)
{
if (arr(front) == element)
{
// When get the element
// Get element location
result = front;
}
else if (arr(back) == element)
{
// When get the element
// Get element location
result = back;
}
// Change index location
front += 1;
back -= 1;
}
if (result != -1)
{
print(" Element " + element + " exist in location of " + result + " \n");
}
else
{
print(" Element " + element + " are not exist \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: SearchElement = new SearchElement();
// Define collection of array elements
var arr: Array[Int] = Array(6, 4, 8, 2, 0, 7, 3, 9);
// Get the size of array
var size: Int = arr.length;
print(" Array Element \n");
task.printArray(arr, size);
// Find element 4
task.findElement(arr, size, 4);
// Find element 3
task.findElement(arr, size, 3);
// Find element 5
task.findElement(arr, size, 5);
}
}
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
/*
Swift 4 Program
Front and Back Search in unsorted array
*/
class SearchElement
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Finding the element of using front and back location
func findElement(_ arr: [Int], _ size: Int, _ element: Int)
{
var result: Int = -1;
// Get index location
var front: Int = 0;
var back: Int = size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while (result == -1 && front < back)
{
if (arr[front] == element)
{
// When get the element
// Get element location
result = front;
}
else if (arr[back] == element)
{
// When get the element
// Get element location
result = back;
}
// Change index location
front += 1;
back -= 1;
}
if (result != -1)
{
print(" Element", element ,"exist in location of ", result ," ");
}
else
{
print(" Element", element ,"are not exist ");
}
}
}
func main()
{
let task: SearchElement = SearchElement();
// Define collection of array elements
let arr: [Int] = [6, 4, 8, 2, 0, 7, 3, 9];
// Get the size of array
let size: Int = arr.count;
print(" Array Element ");
task.printArray(arr, size);
// Find element 4
task.findElement(arr, size, 4);
// Find element 3
task.findElement(arr, size, 3);
// Find element 5
task.findElement(arr, size, 5);
}
main();
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
/*
Kotlin Program
Front and Back Search in unsorted array
*/
class SearchElement
{
//Display elements of given array
fun printArray(arr: Array<Int>, size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
// Finding the element of using front and back location
fun findElement(arr: Array<Int>, size: Int, element: Int): Unit
{
var result: Int = -1;
// Get index location
var front: Int = 0;
var back: Int = size - 1;
// The loop is executed until the location of the given element is found
// And (given front and back variable values are under the limit).
// front value is less than back values
while (result == -1 && front<back)
{
if (arr[front] == element)
{
// When get the element
// Get element location
result = front;
}
else if (arr[back] == element)
{
// When get the element
// Get element location
result = back;
}
// Change index location
front += 1;
back -= 1;
}
if (result != -1)
{
print(" Element " + element + " exist in location of " + result + " \n");
}
else
{
print(" Element " + element + " are not exist \n");
}
}
}
fun main(args: Array<String>): Unit
{
var task: SearchElement = SearchElement();
// Define collection of array elements
var arr: Array<Int> = arrayOf(6, 4, 8, 2, 0, 7, 3, 9);
// Get the size of array
var size: Int = arr.count();
print(" Array Element \n");
task.printArray(arr, size);
// Find element 4
task.findElement(arr, size, 4);
// Find element 3
task.findElement(arr, size, 3);
// Find element 5
task.findElement(arr, size, 5);
}
Output
Array Element
6 4 8 2 0 7 3 9
Element 4 exist in location of 1
Element 3 exist in location of 6
Element 5 are not exist
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment