الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

عمل algorithm

بدأه أسماء البريكي في 5 أبريل 2012 · 4 رد · 549 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم أنا طالبة مبتدئه في الجافا أريد مساعدتكم في هذا البرنامج

بداية نعمل خوارزمية ثم نكتب البرنامج...المشكلة الأساسية معي أني أريد أحسب count ف binary search

بس م عارفه..مع العلم أن count هو number of iteration the program do it to find the searched element

فكرة البرنامج : أولا: ان المستخدم يدخل linear array ثم العنصر الي يبحث عنه ويطلع له count

ثانيا: المستخدم يدخل نفس array بس sorted عشان يطبق binary search ثم نفس العنصر الي بحث عنه سايقا ويطلع له count

هدف البرنامج: هو مقارنة بين linear search& binary search أيهم أسرع؟؟ من خلال الكاونت..

هذا هو السؤال:

Write a program that implement both linear search and binary search. You need to do the following :

1- Input an array.

2- Program should analyze both algorithms for above array

3- Output the Count.

تم تعديل هذه المشاركة بواسطة أسماء البريكي في 5 أبريل 2012 في 10:03

#2

انتظــــــــــــــــــر ردودكم...

#3

Main

 public static void main(String[] args) {

  int s, mid, t,n, high, low, h,index1 ,index2;

        Scanner scan = new Scanner(System.in);


        System.out.println(" Enter the size of the search array ");

        n = scan.nextInt();

        int[] a = new int[n];
        class1 ss = new class1();
        index1  = ss.search(a ,n);

        if (index1 == -1)
           System.out.println(" Element is not found ");

        else 
            System.out.println("Element found in Index : "+index1);
         System.out.println("count_Liner  : " + ss.count_liner);
        // End Liner search ^_^

        System.out.println(" Enter the size of the binary search array 2 ");
        h = scan.nextInt();
        int[] r = new int[h];
        index2 = ss.Binarysearch(r,h);
        if (index2 == -1 )
           System.out.println(" Element is not found"); 
        else 
             System.out.println(  "is found at position " + index2);
       System.out.println("count_binary : " + ss.count_binary);
    }

الكلاس

 
import java.util.Scanner;


public class class1 {
    public int count_liner = 0;
    public int count_binary = 0 ;
    public  int  search(int a[], int size ) {
        int x, n=size  ;
        Scanner scan = new Scanner(System.in);

        System.out.println(" Enter " + n + " elements of the array  ");
        for (int i = 0; i < a.length; i++) 
        {
            a = scan.nextInt();
        }
        System.out.println(" Enter the search element ");
        x = scan.nextInt();
        int j = 0;
        while (j < a.length) 
        {
            if (x == a[j]) 
            {
                return j;

            } 
           count_liner ++;
            j++;
        }
        return -1;
    }

    public  int Binarysearch(int r[],int size){
        Scanner scan = new Scanner(System.in);
       int   mid, t,n, high, h =size,low,s;

    System.out.println(" Enter  " + h + " number  in ascending order ");
        for (t = 0; t < r.length; t++) {
            r[t] = scan.nextInt();
        }
        System.out.println(" Enter the search element 2 ");
        s = scan.nextInt();
        low = 0;
        high = h - 1;
        while (low <= high)
        {
            mid = (low + high) / 2;
            if (s == r[mid]) 
            {
                count_binary ++;
                return mid;
            }
            if (s < r[mid]) 
            {
                high = mid - 1;
                 count_binary ++;
            } 
            else 
            {
                low = mid + 1;
                 count_binary ++;
            }

        }
       return -1;
    }
}
2 −2
#4

مشكوووووووور أخي الكريم الله يوفقك ويرزقك كل ما تتمنااااااااااااااااه يااااااارب..

بس اريدك تشوووف الملف المرفق ف الموضوع الي بسويه الحين

#5
The.Eagle كتب:

Main

 public static void main(String[] args) {

  int s, mid, t,n, high, low, h,index1 ,index2;

        Scanner scan = new Scanner(System.in);


        System.out.println(" Enter the size of the search array ");

        n = scan.nextInt();

        int[] a = new int[n];
        class1 ss = new class1();
        index1  = ss.search(a ,n);

        if (index1 == -1)
           System.out.println(" Element is not found ");

        else 
            System.out.println("Element found in Index : "+index1);
         System.out.println("count_Liner  : " + ss.count_liner);
        // End Liner search ^_^

        System.out.println(" Enter the size of the binary search array 2 ");
        h = scan.nextInt();
        int[] r = new int[h];
        index2 = ss.Binarysearch(r,h);
        if (index2 == -1 )
           System.out.println(" Element is not found"); 
        else 
             System.out.println(  "is found at position " + index2);
       System.out.println("count_binary : " + ss.count_binary);
    }

الكلاس

 
import java.util.Scanner;


public class class1 {
    public int count_liner = 0;
    public int count_binary = 0 ;
    public  int  search(int a[], int size ) {
        int x, n=size  ;
        Scanner scan = new Scanner(System.in);

        System.out.println(" Enter " + n + " elements of the array  ");
        for (int i = 0; i < a.length; i++) 
        {
            a = scan.nextInt();
        }
        System.out.println(" Enter the search element ");
        x = scan.nextInt();
        int j = 0;
        while (j < a.length) 
        {
            if (x == a[j]) 
            {
                return j;

            } 
           count_liner ++;
            j++;
        }
        return -1;
    }

    public  int Binarysearch(int r[],int size){
        Scanner scan = new Scanner(System.in);
       int   mid, t,n, high, h =size,low,s;

    System.out.println(" Enter  " + h + " number  in ascending order ");
        for (t = 0; t < r.length; t++) {
            r[t] = scan.nextInt();
        }
        System.out.println(" Enter the search element 2 ");
        s = scan.nextInt();
        low = 0;
        high = h - 1;
        while (low <= high)
        {
            mid = (low + high) / 2;
            if (s == r[mid]) 
            {
                count_binary ++;
                return mid;
            }
            if (s < r[mid]) 
            {
                high = mid - 1;
                 count_binary ++;
            } 
            else 
            {
                low = mid + 1;
                 count_binary ++;
            }

        }
       return -1;
    }
}

ما هذا !!!!!!

يا أخى أظننا والحمد لله نستطيع حل مثل هذه الأسئلة لكن طريقة السؤال غريبة جدا وكأننا نحن الطلبة وليسوا هم :)

لا محاولات ولا حتى محاولة المحاولة

اعلم انك تضر صاحب السؤال بدلا من ان تنفعه

سلام

1


﴿وَمَا كَانَ اللَّهُ لِيُعَذِّبَهُمْ وَأَنْتَ فِيهِمْ ۚ وَمَا كَانَ اللَّهُ مُعَذِّبَهُمْ وَهُمْ يَسْتَغْفِرُونَ﴾

الأنفال ۳۳                                  
 

مواضيع مشابهة