Maƙasudi da yawa Neman (Multiple Targets Search) Algorithm a ciki Java

Algorithm na Maƙasudi da yawa hanya ce a cikin Java shirye-shirye da ake amfani da su don nemo ƙididdiga masu yawa lokaci guda a cikin tsararru ko jeri. Wannan hanyar tana inganta tsarin bincike kuma tana adana lokaci ta hanyar neman ƙima da yawa a lokaci ɗaya.

Yadda Algorithm Binciken Manufa da yawa ke Aiki

Algorithm na Maƙasudin Maƙasudi da yawa yana aiki ta hanyar ƙididdigewa ta kowane nau'in tsararru ko jeri da kwatanta su da jerin ƙimar ƙima da za a bincika. Idan wani abu a cikin tsararrun ya yi daidai da ƙimar manufa, ana ƙara shi zuwa lissafin sakamako.

Abũbuwan amfãni da rashin amfani Algorithm Binciken Manufofi da yawa

Amfani:

  • Kyakkyawar Aiki: Wannan algorithm yana bincika ƙididdiga masu yawa a cikin tafi ɗaya, yana adana lokaci idan aka kwatanta da yin bincike daban-daban.
  • M: Ana iya amfani da shi a yanayi daban-daban waɗanda ke buƙatar neman manufa da yawa.

Rashin hasara:

  • Amfanin Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwaƙwalwar Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwararren Ƙwaƙwalwa na Ƙwaƙwalwa na Ƙwaƙwalwa) na iya cinyewa fiye da bincike mai sauƙi.

Misali da Bayani

Yi la'akari da misali na yin amfani da Algorithm na Maƙasudi da yawa don nemo takamaiman lambobi masu yawa a cikin tsararrun lamba a cikin Java.

import java.util.ArrayList;  
import java.util.List;  
  
public class MultipleTargetsSearchExample {  
    public static List<Integer> multipleTargetsSearch(int[] array, int[] targets) {  
        List<Integer> results = new ArrayList<>();  
  
        for(int target: targets) {  
            for(int i = 0; i < array.length; i++) {  
                if(array[i] == target) {  
                    results.add(i); // Add position to results if found  
                }  
            }  
        }  
  
        return results;  
    }  
  
    public static void main(String[] args) {  
        int[] numbers = { 4, 2, 7, 2, 9, 5, 7 };  
        int[] targets = { 2, 7 };  
  
        List<Integer> positions = multipleTargetsSearch(numbers, targets);  
  
        if(!positions.isEmpty()) {  
            System.out.println("Targets found at positions: " + positions);  
        } else {  
            System.out.println("Targets not found in the array");  
        }  
    }  
}  

A cikin wannan misalin, muna amfani da Algorithm na Neman Maƙasudi da yawa don nemo lambobi 2 da 7 a cikin tsararrun lamba. Algorithm ɗin yana ƙididdigewa ta hanyar tsararru kuma yana kwatanta kowane kashi tare da lissafin ƙimar manufa. A wannan yanayin, ana samun lamba 2 a matsayi na 1 da 3, kuma ana samun lamba 7 a matsayi na 2 da 6 a cikin tsararru.

Duk da yake wannan misalin yana nuna yadda Algorithm ɗin Bincike na Maƙasudi da yawa zai iya nemo ƙididdiga masu yawa a lokaci ɗaya, kuma ana iya amfani da shi zuwa yanayin bincike daban-daban a cikin Java shirye-shirye.