Introduction to Computer Science مقدمة في علم الحاسب Chapter 6: Algorithms الوحدة 6: الخوارزميات إعداد: أ. محمد دالي ابراهم 2016-1437 جامعة الكامل \ 1 Outline قائمة املحتوى 1. Introduction 2. Algorithm Representation 3. Algorithm Discovery 4. Efficiency and Correctness مقدمة تمثيل الخوارزميات اكتشاف الخوارزميات الفعالية والصواب.1.2.3.4 2 1
.1 مقدمة Introduction 1. 1. Definition 2. Examples 3. Quick practice tests 4. Brief History تعريف أمثلة تمارين تطبيقية سريعة نبذة تاريخية.1.2.3.4 3 1. Definition: An Algorithm is an ordered set of unambiguous, executable steps that defines a terminating process..1 مقدمة Introduction 1. 2. Examples: Prepare a scrambled egg dish for one person The recipe (Algorithm): Prepare a pan, oil, salt, three eggs, a plate Put a small quantity of oil and salt in the pan Put the pan on the stove and light it at the lowest level Crack the eggs into the pan and stir all together Wait until it becomes solid and begin to turn to brown Empty the pan into the plate Put the plat on the table with some drink The end 1. تعريف: الخوارزمية هي مجموعة خطوات مرتبة وواضحة وقابلة للتنفيذ لعمل محدد له نهاية. 2. أمثلة: تحضير أكلة بيض مقلي لشخص واحد الطريقة )الخوارزمية(: إحضار اللوازم: مقالة زيت ملح 3 حبات بيض صحن وضع كمية مناسبة من الزيت وامللح في املقالة وضع املقالة على الفرن ثم أوقده ونزل الشعلة الى أقل مستوى أكسر البيض في املقالة وخلط الكل مع بعض انتظر حتي يتصلب البيض ويبدأ يتحول الي البني ثم أطفئ الفرن أفرغ املقالة في الصحن ضع الصحن على السفرة مع الخبز واملشروب املناسب نهاية 4 2
2. Examples (continued): Compute the surface of a sphere with radius equal to 5 cm The Algorithm: The rule for the surface is: 4πR 2 Compute: 4 * 3.14 = 12.56 Compute: 12.56 * 5 * 5 = 314 The surface of the sphere is: 314 cm 2 The end.1 مقدمة Introduction 1. أمثلة )تابع(: أحسب مساحة سطح كرة نصف قطرها 5 سم.2 الخوارزمية: القاعدة الحسابية ملساحة كرة: π 4 نق 2 أحسب: * 4 3.14 = 12.56 أحسب: * 12.56 5 * 5 = 314 مساحة الكرة : 314 سم 2 نهاية 5.1 مقدمة Introduction 1..3 تمارين تطبيقية سريعة tests: 3. Quick practice a. Write the algorithm to find the greatest number among five number: n1, n2, n3, n4 and n5 The Algorithm: The rule for the volume is: 4/3πR 3 Compute: 4/3 = 1.33 Compute: 1.33 * 3.14 = 4.18 Compute: 4.18 * 5 * 5 * 5 = 523.33 The volume of the sphere is: 523.33 cm 3 The end أ. أكتب الخوارزمية التي تحسب حجم كرة نصف قطرها 5 سم الخوارزمية: 3 القاعدة الحسابية لحجم كرة: 4\3 * π قن* أحسب: 3\4 = 1.33 أحسب: * 1.33 3.14 = 4.18 أحسب: * 4.18 5 * 5 * 5 = 523.33 حجم الكرة : 523.33 سم 3 نهاية 6 3
ك 4/13/2016 1. Introduction.1 3. Quick practice tests:.3 b. Write the algorithm to find the greatest number ب. أكتب الخوارزمية التي تستخرج العدد ألاكبر من among five number: n1, n2, n3, n4 and n5 ضمن 5 أعداد: ع 1 ع 2 ع 3 ع 4 و ع 5 The Algorithm: Compare between the two first numbers in the list مقدمة تمارين تطبيقية سريعة الخوارزمية: قارن بين العددين ألاولين في القائمة ع 1 وع 2 وجعل أكبرهم هو الناتج ك قارن بين الناتج السابق والعدد املوالي في القائمة ع 3 أجعل أكبرهما هو الناتج ك قارن بين الناتج السابق والعدد املوالي في القائمة ع 4 أجعل أكبرهما هو الناتج ك قارن بين الناتج السابق والعدد ألاخير في القائمة ع 5 أجعل أكبرهما هو الناتج النهائي ك هو العدد ألاكببر نهاية 7 4. Brief History:.1 مقدمة Introduction 1. The words 'algorithm' and 'algorism' come from the name al-khwārizmī. Al-Khwārizmī (.,c خوارزمي 780-850) was a muslim mathematician, astronomer, geographer, and scholar in the House of Wisdom in Baghdad, whose name means 'the native of Khwarezm', a region that is part of Uzbekistan. About 825, he wrote a treatise in the Arabic language, which was translated into Latin in the 12th century under the title Algoritmi de numero Indorum. This title means "Algoritmi on the numbers of the Indians", where "Algoritmi" was the translator's Latinization of Al-Khwarizmi's name. Al-Khwarizmi was the most widely read mathematician in Europe in the late Middle Ages, primarily through his other book, the Algebra. In late medieval Latin, algorismus, English 'algorism', the corruption of his name, simply meant the "decimal number system". In the 15th century, under the influence of the Greek word ἀριθμός'number' (cf. 'arithmetic'), the Latin word was altered to algorithmus, and the corresponding English term 'algorithm' is first attested in the 17th century; the modern sense was introduced in the 19th century. 4. نبذة تاريخية: أبو عبد هللا محمد بن موس ى الخوارزمي عالم مسلم يكنى باسم الخوارزمي و أبو جعفر قيل أنه ولد في حوالي 164 ه 781 م في منطقة خوارزمى التي تقع حاليا في جمهورية أزباكستان وقيل أنه توفي في 232 ه 847 م يعتبر من أوائل علماء الرياضيات املسلمين حيث ساهمت أعماله بدور كبير في تقدم الرياضيات في عصره. اتصل بالخليفة العباس ي املأمون وعمل في بيت الحكمة في بغداد وكسب ثقة الخليفة إذ واله املأمون بيت الحكمة كما عهد إليه برسم خريطة لألرض عمل فيها أكثر من 70 جغرافيا وقبل وفاته كان الخوارزمي قد ترك العديد من املؤلفات في علوم الفلك والجغرافيا من أهمها كتاب الجبر واملقابلة الذي يعد أهم كتبه وقد ترجم الكتاب إلى اللغة الالتينية في سنة 1135 م وقد دخلت على إثر ذلك كلمات مثل الجبر Algebra والصفر Zero إلى اللغات الالتينية. 8 4
1. Plain text 2. Pseudo-code 3. Flowchart 4. Program Code نص عادي الشفرة... مخطط الانسياب شفرة البرامج.1.2.3.4 9 2. Algorithm Representation.2 Algorithm representation is the way in which it is presented to the processing entity which could be a human being or a device such as a computer 1. Human language: The first such representation is natural languages such as English The previous example about the algorithm How to prepare a scrambled egg dish for one person was presented using Normal English Prepare a pan, oil, salt, three eggs, a plate Put a small quantity of oil and salt in the pan Put the pan on the stove and light it at the lowest level This type is used when the algorithm is presented to a human being for execution such as a cook in a restaurant تمثيل الخوارزمية يعني الشكل الذي تعرض من خالله الى الكيان الذي ينفذها والذي يمكن أن يكون إنسانا ويمكن أن يكون آلة مثل الحاسوب اللغة العادية تمثيل الخوارزميات أول شكل لتمثيل الخوارزمية هو اللغات البشرية العادية مثل العربية. املثال السابق عن خورزمية تحضير أكلة بيض مقلي لشخص واحد تم عرضها بطريقة النص العادي في اللغة العربية: إحضار اللوازم: مقالة زيت ملح 3 حبات بيض صحن وضع كمية مناسبة من الزيت وامللح في املقالة... يستخدم هذا النوع من تمثيل الخوازميات عند عرضها على إلانسان للتنفيذ مثل عامل في مطعم 10.1 5
2. Pseudo code: It is a like a normal language such as English which doesn't follow closely the language rules and uses a limited set of statements oriented to computer processing rather than human processing Example: Compute the volume of a sphere with radius equal to R cm The Algorithm in pseudo code: Start Res = 4 / 3 Res = Res * π (3.14) Res = Res * R 3 Sphere volume = Res in cm 3 The end الشفرة املختصرة يشبه اللغة العادية مثل الانجليزية ولكن ال تتبع قواعدها بشكل صارم ويستخدم مجموعة محددة من العبارات املوجهة الى جهاز الحاسوب للتنفيذ مثال: أحسب حجم كرة نصف قطرها قن الخوارزمية بالشكل الشفرة املختصرة بداية ن = 4 \ 3 )3.14( π * ن = ن ن = ن * نق 3 حجم الكرة = ن نهاية.2 11 2. Pseudo code: Remarks: In the computing field pseudo code is used to represent algorithms before the programming phase It is mostly in English It is independent of the programming language to be used. الشفرة املختصرة مالحظات: تستخدم الشفر املختصرة في مجال الحاسب لتمثيل الخوازميات قبل تحولها الى برامج حاسب في إحدى لغات البرمجة مثل جافا س ي. ++ C++,Java( ), اللغة الانجليزية هي السائدة في هذا املجال الشفرة املختصرة مستقلة عن لغات البرمجة.2 12 6
3. Flowchart: It is a Example: Compute the volume of a sphere with radius equal to R cm هو تمثيل نص ي رسومي للخوارزمية بنظرة أن التنفيذ سيتم عن طريق الحاسب. مثال: أحسب حجم كرة نصف قطرها قن ح بداية 3 \ 4 = 3. خرائط الانسياب ح = ح * 3.14 ح = ح * نق 3 الحجم = ح نهاية 13 3. Flowchart: It is 3. خرائط الانسياب توجد أربعة )4( أنواع رئيسية من الرسومات الانسيابية: مخططات العمليات التتابعية (Sequential Flowcharts). مخططات العمليات ذات التفرعات (Branched Flowcharts). 14 7
ن 4/13/2016 3. Flowchart: It is a 3. خرائط الانسياب )تابع( مخططات العمليات ذات الاختيارات (Selection Flowcharts). إذا... نعم ال مخططات العمليات ذات التكرار الدائري (Loop Flowcharts).. ال نهاية التكرار نعم 15 تمثيل الخوارزميات 3. خرائط الانسياب مثال: القاعدة: ن! = ن 2. Algorithm Representation.2 )تابع( حساب مضروب أو عاملي لعدد ن :! 3. Flowchart: * )ن- 1 ( * )ى- 2 ( *... * 2 * 1 تطبيق للقاعدة على مثال:!4 = 4 * 3 * 2 * 1 = 24 مخطط الانسياب: إدخال قيمة ن ن! = 1 ن! = ن! * ن ن = ن- 1 البداية ال هل ن = 1 نعم الناتج هو ن! النهاية 16 8
تمثيل الخوارزميات 3. خرائط الانسياب تمارين: 2. Algorithm Representation.2 )تابع( إنشاء مخطط انسيابي لحساب ناتج ضرب الاعداد الفردية من 1 الى عدد "ن 3. Flowchart: 17 4. Program code: 4. شفرة البرامج: البرنامج هو مجموعة أوامر للحاسب لتنفيذ عمل معين تكتب ألاوامر في لغة من لغات برمجة الحاسب مثل جافا ) Java ( س ي. ++ )++C( شإ. تي. إم. أل...(HTML) 18 9
4. Program code: class sum_even_numbers import javax.swing.joptionpane; public class sum_even الى عدد يدخل public static void main( String args[] ). N int Total, Counter,N; String Value_From_KB; total = 0; Counter = 0; Value_From_KB= JOptionPane.showInputDialog("Enter a positive limit value: " ); N = Integer.parseInt(Value_From_KB); while (Counter <= N ) Total = Total + Counter; Counter = Counter + 2; JOptionPane.showMessageDialog( null, "The Sum off the positives EVEN Numbers less then " +N + "are : " + Total, JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 4. شفرة البرامج: مثال 1 كتابة برنامج في لغة جافا ) Java ( لحساب مجموع ألاعداد الزوجية من 2 عن طريق لوحة املفاتيح ونشير اليه بالحرف 19 Representation 2. Algorithm.2 تمثيل الخوارزميات 4. Program code: 4. شفرة البرامج: class _9_Mul_table public static void main ( string [ ] args ) System.out.println ( " multiplication table " ) ; System.out.println ( " ---------------------------------------------- " ); System.out.print ( " " ) for ( int j=1 ; j <= 9; j ++ ) System.out.print ( " " + j ); System.out.println ( " " ) ; for ( int i = 1 ; i <= 9 ; i ++ ) System.out.print ( i + " " ) ; for ( int j = 1 ; j <= 9 ; j ++ ) if ( i * j < 10 ) System.out.print ( " " + i* j ) ; else System.out.print ( " " + i * j ) ; System.out.println ( " " ) ; مثال 1 كتابة برنامج في لغة الضرب الى العدد 9 جافا ) Java ( لعرض جدول مقدمة في علم الحاسب - Science Introduction to Computer. 6 الخوارزميات - Algorithms 6. 20 10
.3 اكتشاف الخوارزميات Discovery 3. Algorithm 4. Program code: 4. شفرة البرامج: البرنامج هو مجموعة أوامر للحاسب لتنفيذ عمل معين تكتب ألاوامر في لغة من لغات برمجة الحاسب مثل جافا ) Java ( س ي. ++ )++C( شإ. تي. إم. أل...(HTML) 21 Introduction to Computer Science مقدمة في علم الحاسب Chapter 6: Algorithms الوحدة 6: الخوارزميات End of Chapter نهاية الوحدة 24 11