Maximum element in a very large array using pthreads
Last Updated :
27 Dec, 2023
Given a very large array of integers, find maximum within the array using multithreading.
Examples:
Input : 1, 5, 7, 10, 12, 14, 15, 18, 20,
22, 25, 27, 30, 64, 110, 220
Output :Maximum Element is : 220
Input : 10, 50, 70, 100, 120, 140, 150, 180,
200, 220, 250, 270, 300, 640, 110, 220
Output : Maximum Element is : 640
Prerequisite : Multithreading Note : Useful in large files of size MB/GB.
How to run : It can only be run on linux envirenment. command :
>> gcc -pthread maximum.c
>> ./a.out
C++
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define max 16
#define Th_max 4
int a[max] = { 1, 5, 7, 10, 12, 14, 15, 18, 20,
22, 25, 27, 300, 64, 110, 220 };
int max_num[Th_max] = { 0 };
int thread_no = 0;
void * maximum( void * arg)
{
int i, num = thread_no++;
int maxs = 0;
for (i = num * (max / 4); i < (num + 1) * (max / 4); i++) {
if (a[i] > maxs)
maxs = a[i];
}
max_num[num] = maxs;
}
int main()
{
int maxs = 0;
int i;
pthread_t threads[Th_max];
for (i = 0; i < Th_max; i++)
pthread_create(&threads[i], NULL, maximum, ( void *)NULL);
for (i = 0; i < Th_max; i++)
pthread_join(threads[i], NULL);
for (i = 0; i < Th_max; i++) {
if (max_num[i] > maxs)
maxs = max_num[i];
}
printf ( "Maximum Element is : %d" , maxs);
return 0;
}
|
C
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define max 16
#define Th_max 4
int a[max] = { 1, 5, 7, 10, 12, 14, 15, 18, 20,
22, 25, 27, 300, 64, 110, 220 };
int max_num[Th_max] = { 0 };
int thread_no = 0;
void maximum( void * arg)
{
int i, num = thread_no++;
int maxs = 0;
for (i = num * (max / 4); i < (num + 1) * (max / 4); i++) {
if (a[i] > maxs)
maxs = a[i];
}
max_num[num] = maxs;
}
int main()
{
int maxs = 0;
int i;
pthread_t threads[Th_max];
for (i = 0; i < Th_max; i++)
pthread_create(&threads[i], NULL,
maximum, ( void *)NULL);
for (i = 0; i < Th_max; i++)
pthread_join(threads[i], NULL);
for (i = 0; i < Th_max; i++) {
if (max_num[i] > maxs)
maxs = max_num[i];
}
printf ("Maximum Element is : %d", maxs);
return 0;
}
|
Java
import java.util.Arrays;
public class MaxElementThread extends Thread {
int lo, hi;
int [] arr;
int max;
public MaxElementThread( int [] arr, int lo, int hi) {
this .arr = arr;
this .lo = lo;
this .hi = hi;
this .max = Integer.MIN_VALUE;
}
public void run() {
for ( int i = lo; i < hi; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
}
public static void main(String[] args) {
int [] arr = { 1 , 5 , 7 , 10 , 12 , 14 , 15 , 18 , 20 , 22 , 25 , 27 , 300 , 64 , 110 , 220 };
int n = arr.length;
int numThreads = 4 ;
MaxElementThread[] threads = new MaxElementThread[numThreads];
int blockSize = n / numThreads;
for ( int i = 0 ; i < numThreads; i++) {
int lo = i * blockSize;
int hi = (i == numThreads - 1 ) ? n : (i + 1 ) * blockSize;
threads[i] = new MaxElementThread(arr, lo, hi);
threads[i].start();
}
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < numThreads; i++) {
try {
threads[i].join();
if (threads[i].max > max) {
max = threads[i].max;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println( "Maximum Element is : " + max);
}
}
|
Python3
from threading import Thread
MAX = 16
MAX_THREAD = 4
arr = [ 1 , 5 , 7 , 10 , 12 , 14 , 15 , 18 , 20 ,
22 , 25 , 27 , 300 , 64 , 110 , 220 ]
max_num = [ 0 for _ in range (MAX_THREAD)]
thread_no = 0
def maximum():
global thread_no
index = thread_no
thread_no + = 1
start, end = int (index * ( MAX / 4 )), int ((index + 1 ) * ( MAX / 4 ))
for i in range (start, end, 1 ):
max_num[index] = max (max_num[index], arr[i])
if __name__ = = "__main__" :
thread = list ( range (MAX_THREAD))
for i in range (MAX_THREAD):
thread[i] = Thread(target = maximum)
thread[i].start()
for i in range (MAX_THREAD):
thread[i].join()
actual_max = 0
for x in max_num:
actual_max = max (actual_max, x)
print ( "Maximum element is : %d" % actual_max)
|
C#
using System;
using System.Threading;
public class MaxArrayThread
{
const int MAX = 16;
const int MAX_THREAD = 4;
static int [] arr = new int [] { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 300, 64, 110, 220 };
static int [] max_num = new int [MAX_THREAD];
static int thread_no = 0;
static void Maximum()
{
int index = Interlocked.Increment( ref thread_no) - 1;
int start = ( int )(index * (MAX / 4));
int end = ( int )((index + 1) * (MAX / 4));
for ( int i = start; i < end; i++)
{
max_num[index] = Math.Max(max_num[index], arr[i]);
}
}
public static void Main()
{
Thread[] thread = new Thread[MAX_THREAD];
for ( int i = 0; i < MAX_THREAD; i++)
{
thread[i] = new Thread(Maximum);
thread[i].Start();
}
for ( int i = 0; i < MAX_THREAD; i++)
{
thread[i].Join();
}
int actual_max = 0;
for ( int i = 0; i < MAX_THREAD; i++)
{
actual_max = Math.Max(actual_max, max_num[i]);
}
Console.WriteLine( "Maximum element is : {0}" , actual_max);
}
}
|
Javascript
const MAX = 16;
const MAX_THREAD = 4;
const arr = [1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 300, 64, 110, 220];
const maxNum = new Array(MAX_THREAD).fill(0);
let threadNo = 0;
function maximum() {
const index = threadNo;
threadNo += 1;
const start = Math.floor(index * (MAX / 4));
const end = Math.floor((index + 1) * (MAX / 4));
for (let i = start; i < end; i++) {
maxNum[index] = Math.max(maxNum[index], arr[i]);
}
}
const threads = new Array(MAX_THREAD).fill( null ).map(() => {
return new Promise(resolve => {
maximum();
resolve();
});
});
Promise.all(threads).then(() => {
const actualMax = maxNum.reduce((max, x) => Math.max(max, x), 0);
console.log(`Maximum element is: ${actualMax}`);
});
|
Output
Maximum element is : 300
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...