Find three smallest element in array
Here given code implementation process.
// C Program
// Find three smallest element in array
#include <stdio.h>
#include <limits.h>
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", arr[i]);
}
printf("\n");
}
//Method which is finding the third smallest element in given array
void three_smallest(int arr[], int size)
{
if (size < 3)
{
return;
}
//initial set the maximum value of variables
int first = INT_MAX;
int second = INT_MAX;
int third = INT_MAX;
for (int i = 0; i < size; ++i)
{
//Check if whether array element i value is less than of variable first
if (arr[i] < first)
{
//modify the value of all three variables
//like a swap operation of two variable
third = second;
second = first;
//Assign a new small value
first = arr[i];
}
else if (arr[i] < second)
{
//When get second smallest element
third = second;
//Assign a new small value
second = arr[i];
}
else if (arr[i] < third)
{
//When get third smallest element
third = arr[i];
}
}
printf("\nResultant smallest\n");
display(arr, size);
printf("first : %d second : %d third : %d\n", first, second, third);
}
int main()
{
//Define array elements
int arr1[] = {
23,
8,
2,
0,
6,
-2,
6,
-1,
9,
4,
3
};
int size = sizeof(arr1) / sizeof(arr1[0]);
three_smallest(arr1, size);
//Define array elements
int arr2[] = {
13,
9,
2,
-7,
4,
1,
3,
7,
22,
8,
-4
};
size = sizeof(arr2) / sizeof(arr2[0]);
three_smallest(arr2, size);
return 0;
}
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
/*
Java Program
Find three smallest element in array
*/
class MyArray
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i] + " ");
}
System.out.print("\n");
}
//Method which is finding the third smallest element in given array
public void three_smallest(int[] arr, int size)
{
if (size < 3)
{
return;
}
//initial set the maximum value of variables
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
int third = Integer.MAX_VALUE;
for (int i = 0; i < size; ++i)
{
//Check if whether array element i value is less than of variable first
if (arr[i] < first)
{
//modify the value of all three variables
//like a swap operation of two variable
third = second;
second = first;
//Assign a new small value
first = arr[i];
}
else if (arr[i] < second)
{
//When get second smallest element
third = second;
//Assign a new small value
second = arr[i];
}
else if (arr[i] < third)
{
//When get third smallest element
third = arr[i];
}
}
System.out.print("\nResultant smallest\n");
display(arr, size);
System.out.print("first : " + first + " second : " + second + " third : " + third + "\n");
}
public static void main(String[] args)
{
MyArray obj = new MyArray();
//Define array elements
int[] arr1 = {
23,
8,
2,
0,
6,
-2,
6,
-1,
9,
4,
3
};
int size = arr1.length;
obj.three_smallest(arr1, size);
//Define array elements
int[] arr2 = {
13,
9,
2,
-7,
4,
1,
3,
7,
22,
8,
-4
};
size = arr2.length;
obj.three_smallest(arr2, size);
}
}
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
/*
C++ Program
Find three smallest element in array
*/
#include<iostream>
#include<limits.h>
using namespace std;
class MyArray
{
public:
//Function which is display array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i] << " ";
}
cout << "\n";
}
//Method which is finding the third smallest element in given array
void three_smallest(int arr[], int size)
{
if (size < 3)
{
return;
}
//initial set the maximum value of variables
int first = INT_MAX;
int second = INT_MAX;
int third = INT_MAX;
for (int i = 0; i < size; ++i)
{
//Check if whether array element i value is less than of variable first
if (arr[i] < first)
{
//modify the value of all three variables
//like a swap operation of two variable
third = second;
second = first;
//Assign a new small value
first = arr[i];
}
else if (arr[i] < second)
{
//When get second smallest element
third = second;
//Assign a new small value
second = arr[i];
}
else if (arr[i] < third)
{
//When get third smallest element
third = arr[i];
}
}
cout << "\nResultant smallest\n";
this->display(arr, size);
cout << "first : " << first << " second : " << second << " third : " << third << "\n";
}
};
int main()
{
MyArray obj ;
int arr1[] = {
23,
8,
2,
0,
6,
-2,
6,
-1,
9,
4,
3
};
int size = sizeof(arr1) / sizeof(arr1[0]);
obj.three_smallest(arr1, size);
int arr2[] = {
13,
9,
2,
-7,
4,
1,
3,
7,
22,
8,
-4
};
size = sizeof(arr2) / sizeof(arr2[0]);
obj.three_smallest(arr2, size);
return 0;
}
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
/*
C# Program
Find three smallest element in array
*/
using System;
class MyArray
{
//Function which is display array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; i++)
{
Console.Write(" " + arr[i] + " ");
}
Console.Write("\n");
}
//Method which is finding the third smallest element in given array
public void three_smallest(int[] arr, int size)
{
if (size < 3)
{
return;
}
//initial set the maximum value of variables
int first = int.MaxValue;
int second = int.MaxValue;
int third = int.MaxValue;
for (int i = 0; i < size; i++)
{
//Check if whether array element i value is less than of variable first
if (arr[i] < first)
{
//modify the value of all three variables
//like a swap operation of two variable
third = second;
second = first;
//Assign a new small value
first = arr[i];
}
else if (arr[i] < second)
{
//When get second smallest element
third = second;
//Assign a new small value
second = arr[i];
}
else if (arr[i] < third)
{
//When get third smallest element
third = arr[i];
}
}
Console.Write("\nResultant smallest\n");
display(arr, size);
Console.Write("first : " + first + " second : " + second + " third : " + third + "\n");
}
public static void Main(String[] args)
{
MyArray obj = new MyArray();
int[] arr1 = {
23,
8,
2,
0,
6,
-2,
6,
-1,
9,
4,
3
};
int size = arr1.Length;
obj.three_smallest(arr1, size);
int[] arr2 = {
13,
9,
2,
-7,
4,
1,
3,
7,
22,
8,
-4
};
size = arr2.Length;
obj.three_smallest(arr2, size);
}
}
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
<?php
/*
Php Program
Find three smallest element in array
*/
class MyArray
{
//Function which is display array elements
function display( & $arr, $size)
{
for ($i = 0; $i < $size; ++$i)
{
echo " ". $arr[$i] ." ";
}
echo "\n";
}
//Method which is finding the third smallest element in given array
function three_smallest( & $arr, $size)
{
if ($size < 3)
{
return;
}
//initial set the maximum value of variables
$first = PHP_INT_MAX;
$second = PHP_INT_MAX;
$third = PHP_INT_MAX;
for ($i = 0; $i < $size; ++$i)
{
//Check if whether array element i value is less than of variable first
if ($arr[$i] < $first)
{
//like a swap operation of two variable
$third = $second;
$second = $first;
//Assign a new small value
$first = $arr[$i];
}
else if ($arr[$i] < $second)
{
//When get second smallest element
$third = $second;
//Assign a new small value
$second = $arr[$i];
}
else if ($arr[$i] < $third)
{
//When get third smallest element
$third = $arr[$i];
}
}
echo "\nResultant smallest\n";
$this->display($arr, $size);
echo "first : ". $first ." second : ". $second ." third : ". $third ."\n";
}
}
function main()
{
$obj = new MyArray();
//Define array elements
$arr1 = array(23, 8, 2, 0, 6, -2, 6, -1, 9, 4, 3);
$size = count($arr1);
$obj->three_smallest($arr1, $size);
//Define array elements
$arr2 = array(13, 9, 2, -7, 4, 1, 3, 7, 22, 8, -4);
$size = count($arr2);
$obj->three_smallest($arr2, $size);
}
main();
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
/*
Node Js Program
Find three smallest element in array
*/
class MyArray
{
//Function which is display array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i] + " ");
}
process.stdout.write("\n");
}
//Method which is finding the third smallest element in given array
three_smallest(arr, size)
{
if (size < 3)
{
return;
}
//initial set the maximum value of variables
var first = Number.MAX_VALUE;
var second = Number.MAX_VALUE;
var third = Number.MAX_VALUE;
for (var i = 0; i < size; ++i)
{
//Check if whether array element i value is less than of variable first
if (arr[i] < first)
{
//modify the value of all three variables
//like a swap operation of two variable
third = second;
second = first;
//Assign a new small value
first = arr[i];
}
else if (arr[i] < second)
{
//When get second smallest element
third = second;
//Assign a new small value
second = arr[i];
}
else if (arr[i] < third)
{
//When get third smallest element
third = arr[i];
}
}
process.stdout.write("\nResultant smallest\n");
this.display(arr, size);
process.stdout.write("first : " + first + " second : " + second + " third : " + third + "\n");
}
}
function main()
{
var obj = new MyArray();
//Define array elements
var arr1 = [23, 8, 2, 0, 6, -2, 6, -1, 9, 4, 3];
var size = arr1.length;
obj.three_smallest(arr1, size);
//Define array elements
var arr2 = [13, 9, 2, -7, 4, 1, 3, 7, 22, 8, -4];
size = arr2.length;
obj.three_smallest(arr2, size);
}
main();
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
import sys
# Python 3 Program
# Find three smallest element in array
class MyArray :
# Function which is display array elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i] ," ", end = "")
i += 1
print("\n", end = "")
# Method which is finding the third smallest element in given array
def three_smallest(self, arr, size) :
if (size < 3) :
return
# initial set the maximum value of variables
first = sys.maxsize
second = sys.maxsize
third = sys.maxsize
i = 0
while (i < size) :
# Check if whether array element i value is less than of variable first
if (arr[i] < first) :
# like a swap operation of two variable
# modify the value of all three variables
third = second
second = first
# Assign a new small value
first = arr[i]
elif(arr[i] < second) :
# When get second smallest element
third = second
# Assign a new small value
second = arr[i]
elif(arr[i] < third) :
# When get third smallest element
third = arr[i]
i += 1
print("\nResultant smallest\n", end = "")
self.display(arr, size)
print("first : ", first ," second : ", second ," third : ", third ,"\n", end = "")
def main() :
obj = MyArray()
# Define array elements
arr1 = [23, 8, 2, 0, 6, -2, 6, -1, 9, 4, 3]
size = len(arr1)
obj.three_smallest(arr1, size)
# Define array elements
arr2 = [13, 9, 2, -7, 4, 1, 3, 7, 22, 8, -4]
size = len(arr2)
obj.three_smallest(arr2, size)
if __name__ == "__main__": main()
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
# Ruby Program
# Find three smallest element in array
class MyArray
# Function which is display array elements
def display(arr, size)
i = 0
while (i < size)
print(" ", arr[i] ," ")
i += 1
end
print("\n")
end
# Method which is finding the third smallest element in given array
def three_smallest(arr, size)
if (size < 3)
return
end
# initial set the maximum value of variables
first = (2 ** (0. size * 8 - 2))
second = (2 ** (0. size * 8 - 2))
third = (2 ** (0. size * 8 - 2))
i = 0
while (i < size)
# Check if whether array element i value is less than of variable first
if (arr[i] < first)
# like a swap operation of two variable
# modify the value of all three variables
third = second
second = first
# Assign a new small value
first = arr[i]
elsif(arr[i] < second)
# When get second smallest element
third = second
# Assign a new small value
second = arr[i]
elsif(arr[i] < third)
# When get third smallest element
third = arr[i]
end
i += 1
end
print("\nResultant smallest\n")
self.display(arr, size)
print("first : ", first ," second : ", second ," third : ", third ,"\n")
end
end
def main()
obj = MyArray.new()
# Define array elements
arr1 = [23, 8, 2, 0, 6, -2, 6, -1, 9, 4, 3]
size = arr1.length
obj.three_smallest(arr1, size)
# Define array elements
arr2 = [13, 9, 2, -7, 4, 1, 3, 7, 22, 8, -4]
size = arr2.length
obj.three_smallest(arr2, size)
end
main()
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
/*
Scala Program
Find three smallest element in array
*/
class MyArray
{
//Function which is display array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i) + " ");
i += 1;
}
print("\n");
}
//Method which is finding the third smallest element in given array
def three_smallest(arr: Array[Int], size: Int): Unit = {
if (size < 3)
{
return;
}
//initial set the maximum value of variables
var first: Int = Int.MaxValue;
var second: Int = Int.MaxValue;
var third: Int = Int.MaxValue;
var i: Int = 0;
while (i < size)
{
//Check if whether array element i value is less than of variable first
if (arr(i) < first)
{
//like a swap operation of two variable
//modify the value of all three variables
third = second;
second = first;
//Assign a new small value
first = arr(i);
}
else if (arr(i) < second)
{
//When get second smallest element
third = second;
//Assign a new small value
second = arr(i);
}
else if (arr(i) < third)
{
//When get third smallest element
third = arr(i);
}
i += 1;
}
print("\nResultant smallest\n");
display(arr, size);
print("first : " + first + " second : " + second + " third : " + third + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyArray = new MyArray();
//Define array elements
var arr1: Array[Int] = Array(23, 8, 2, 0, 6, -2, 6, -1, 9, 4, 3);
var size: Int = arr1.length;
obj.three_smallest(arr1, size);
//Define array elements
var arr2: Array[Int] = Array(13, 9, 2, -7, 4, 1, 3, 7, 22, 8, -4);
size = arr2.length;
obj.three_smallest(arr2, size);
}
}
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
/*
Swift Program
Find three smallest element in array
*/
class MyArray
{
//Function which is display array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i] ," ", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
//Method which is finding the third smallest element in given array
func three_smallest(_ arr: [Int], _ size: Int)
{
if (size < 3)
{
return;
}
//initial set the maximum value of variables
var first: Int = Int.max;
var second: Int = Int.max;
var third: Int = Int.max;
var i: Int = 0;
while (i < size)
{
//Check if whether array element i value is less than of variable first
if (arr[i] < first)
{
//like a swap operation of two variable
//modify the value of all three variables
third = second;
second = first;
//Assign a new small value
first = arr[i];
}
else if (arr[i] < second)
{
//When get second smallest element
third = second;
//Assign a new small value
second = arr[i];
}
else if (arr[i] < third)
{
//When get third smallest element
third = arr[i];
}
i += 1;
}
print("\nResultant smallest\n", terminator: "");
self.display(arr, size);
print("first : ", first ," second : ", second ," third : ", third ,"\n", terminator: "");
}
}
func main()
{
let obj: MyArray = MyArray();
//Define array elements
let arr1: [Int] = [23, 8, 2, 0, 6, -2, 6, -1, 9, 4, 3];
var size: Int = arr1.count;
obj.three_smallest(arr1, size);
//Define array elements
let arr2: [Int] = [13, 9, 2, -7, 4, 1, 3, 7, 22, 8, -4];
size = arr2.count;
obj.three_smallest(arr2, size);
}
main();
Output
Resultant smallest
23 8 2 0 6 -2 6 -1 9 4 3
first : -2 second : -1 third : 0
Resultant smallest
13 9 2 -7 4 1 3 7 22 8 -4
first : -7 second : -4 third : 1
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