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
// C++ code to find maximum of an array using Multithreading#include <pthread.h>#include <stdio.h>#include <stdlib.h>// Size of array#define max 16// Max number of thread#define Th_max 4// Arrayint a[max] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 300, 64, 110, 220 };// Array to store max of threadsint max_num[Th_max] = { 0 };int thread_no = 0;// Function to find maximumvoid 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;}// Driver codeint main(){ int maxs = 0; int i; pthread_t threads[Th_max]; // creating 4 threads for (i = 0; i < Th_max; i++) pthread_create(&threads[i], NULL, maximum, (void*)NULL); // joining 4 threads i.e. waiting for // all 4 threads to complete for (i = 0; i < Th_max; i++) pthread_join(threads[i], NULL); // Finding max element in an array // by individual threads 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++
// C++ code to find maximum of an array using Multithreading#include <pthread.h>#include <stdio.h>#include <stdlib.h>// Size of array#define max 16// Max number of threads#define Th_max 4// Arrayint a[max] = { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 300, 64, 110, 220 };// Array to store max of threadsint max_num[Th_max] = { 0 };int thread_no = 0;// Function to find maximumvoid* 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;}// Driver codeint main(){ int maxs = 0; int i; pthread_t threads[Th_max]; // creating 4 threads for (i = 0; i < Th_max; i++) pthread_create(&threads[i], NULL, maximum, (void*)NULL); // joining 4 threads i.e. waiting for // all 4 threads to complete for (i = 0; i < Th_max; i++) pthread_join(threads[i], NULL); // Finding max element in an array // by individual threads for (i = 0; i < Th_max; i++) { if (max_num[i] > maxs) maxs = max_num[i]; } printf("Maximum Element is : %d", maxs); return 0;}// This code is contributed by Shivhack999 |
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
# C++ code to find maximum of an array using Multithreadingfrom threading import Thread# Size of arrayMAX = 16# Maximum number of threadsMAX_THREAD = 4# Initial arrayarr = [1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 300, 64, 110, 220]# Max array for storing max of each part computedmax_num = [0 for _ in range(MAX_THREAD)]thread_no = 0# Function to calculate maximumdef maximum(): global thread_no index = thread_no thread_no += 1 # Each thread computes max of 1/4th of array 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__": # Creating list of size MAX_THREAD thread = list(range(MAX_THREAD)) # Creating MAX_THEAD number of threads for i in range(MAX_THREAD): thread[i] = Thread(target=maximum) thread[i].start() # Waiting for all threads to finish for i in range(MAX_THREAD): thread[i].join() # Adding sum of all 4 parts 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{ // Size of array const int MAX = 16; // Maximum number of threads const int MAX_THREAD = 4; // Initial array static int[] arr = new int[] { 1, 5, 7, 10, 12, 14, 15, 18, 20, 22, 25, 27, 300, 64, 110, 220 }; // Max array for storing max of each part computed static int[] max_num = new int[MAX_THREAD]; static int thread_no = 0; // Function to calculate maximum static void Maximum() { int index = Interlocked.Increment(ref thread_no) - 1; // Each thread computes max of 1/4th of array 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() { // Creating array of size MAX_THREAD Thread[] thread = new Thread[MAX_THREAD]; // Creating MAX_THREAD number of threads for (int i = 0; i < MAX_THREAD; i++) { thread[i] = new Thread(Maximum); thread[i].Start(); } // Waiting for all threads to finish for (int i = 0; i < MAX_THREAD; i++) { thread[i].Join(); } // Adding sum of all 4 parts 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); }}// This code is contributed by shivregkec |
Maximum element is : 300
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
