SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
Algorithms - 09                                                          CSC1001 Discrete Mathematics             1

 CHAPTER
                                                          อัลกอริทึม
      9                                                  (Algorithms)

  1      Introduction to Algorithms
1. Algorithm Deffinitions
 Definition 1

 An algorithm is a finite sequence of precise instructions or steps for performing a computation or for solving
 a problem (In computer science usually represent the algorithm by using pseudocode).

Example 1 (5 points) Describe an algorithm or write a pseudocode for finding the maximum (largest) value in
a finite sequence of integers.
 procedure maximum({a1, a2, … , an}: integers) {
   max = a1
   for i = 2 to n
     if max < ai then max = ai
   return max
 }


Example 2 (5 points) Describe an algorithm or write a pseudocode for finding the minimum value in a finite se-
quence of real number.




Example 3 (5 points) Describe an algorithm to calculate the average of a finite sequence of integers.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
2       CSC1001 Discrete Mathematics                                                               09 - Algorithms


Example 4 (5 points) Describe an algorithm to find the absolute value of integers.




Example 5 (5 points) Describe an algorithm to find the factorial value of integers.




Example 6 (5 points) Describe an algorithm to find the Fibonacci value of integers (a0 = 0 and a1 = 1).




Example 7 (5 points) Describe an algorithm to find the multiplication of two matrices.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                  เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                        CSC1001 Discrete Mathematics           3
2. Searching Algorithms
 Definition 2

 The Linear Search Algorithm

 procedure linearSearch({a1, a2, … , an}: integers, x: integer) {
   i = 1
   while i ≤ n {
     if ai = x then return i
     else i = i + 1
   }
   return -1
 }


 Definition 3
 The Binary Search Algorithm

 procedure binarySearch({a1, a2, … , an}: integers, x: integer) {
   l = 1 //i is left endpoint of search interval
   r = n //j is right endpoint of search interval
   while l < r {
     m = ⎣ + r) / 2 ⎦
         (l
     if x = am then return m
     else if x > am then l = m + 1
     else r = m - 1
   }
   return -1
 }


Example 8 (20 points) Consider the iteration of linear search and binary search for searching some value from
the input sequence.
1) Search 26 using linear search
        2          3          6         8         11        15        21         26        30          39

2) Search 26 using binary search
        2          3          6         8         11        15        21         26        30          39




3) Search 3 using linear search
        2          3          6         8         11        15        21         26        30          39


มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                             เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
4       CSC1001 Discrete Mathematics                                                      09 - Algorithms


4) Search 3 using binary search
        2          3          6         8         11    15        21        26         30          39




5) Search 2 using linear search
        2          3          6         8         11    15        21        26         30          39

6) Search 2 using binary search
        2          3          6         8         11    15        21        26         30          39




7) Search 17 using linear search
        2          3          6         8         11    15        21        26         30          39

8) Search 17 using binary search
        2          3          6         8         11    15        21        26         30          39




Example 9 (4 points) From an Example 4, can you summarize the different functions or features of linear
search and binary search algorithms?




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                         เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                         CSC1001 Discrete Mathematics            5
3. Sorting Algorithms
 Definition 4

 The Bubble Sort Algorithm

 procedure bubbleSort({a1, a2, … , an}: real number) {
   for i = n to 2 {
     for j = 1 to i - 1 {
       if aj > aj + 1 then {
          temp = aj
          aj = aj + 1
          aj + 1 = temp
       }
     }
   }
 }


 Definition 5
 The Selection Sort Algorithm

 procedure selectionSort({a1, a2, … , an}: real number) {
   for i = n to 2 {
     maxIndex = 1
     for j = 1 to i {
        if aj > amaxIndex then maxIndex = j
     }
     temp = ai
     ai = amaxIndex
     amaxIndex = temp
   }
 }


Example 10 (20 points) Write the steps of bubble sort and selection sort of this sequence.
1) Using bubble sort
       15         30          2         26        21         6         39         3          11           8




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                               เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
6         CSC1001 Discrete Mathematics                                                                   09 - Algorithms


2) Using selection sort
          15          30          2          26         21           6         39           3          11          8




     2      Growth of Functions and Complexity of Algorithms
1. Big-O, Big-Ω and Big-Θ Notation
    Definition 1

    Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We
    say that f (x) is O(g(x)) if there are constants C and k such that
    |f (x)| ≤ C|g(x)| whenever x > k. This is read as “f (x) is big-oh of g(x).”

    Definition 2

    Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We
    say that f (x) is Ω(g(x)) if there are positive constants C and k such that
    |f (x)| ≥ C|g(x)| whenever x > k. This is read as “f (x) is big-Omega of g(x).”

    Definition 3

    Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We
    say that f (x) is Θ(g(x)) if there are real numbers C1 and C2 and a positive real number k such that
    C1|g(x)| ≤ |f (x)| ≤ C2|g(x)| whenever x > k. We say that f (x) is Θ(g(x)) if f (x) is O(g(x)) and f (x) is Ω(g(x)).
    This is read as “f (x) is big-Omega of g(x).”

มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                        เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                           CSC1001 Discrete Mathematics           7
Example 11 (4 points) Show that f(x) = x2 + 2x + 1 is O(x2).




Example 12 (4 points) Show that f(x) = 3x4 + 5x2 + 15 is O(x4).




Example 13 (4 points) Show that f(x) = 7x2 is O(x3) by replace x into f(x).




Example 14 (24 points) Estimate the growth of functions.




Figure: A Display of the Commonly Used in Big-O Estimates

มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
8       CSC1001 Discrete Mathematics                                                      09 - Algorithms


1) (12 points) Ranking the speed rate of functions by descending
      No        Functions            Ranking                     No    Functions           Ranking
      1.             n                                           7.        n2
      2.           0.5n                                          8.      log6 n
      3.         n log n                                         9.        n0.5
      4.             1                                           10.        n!
      5.         n2 log n                                        11.       2n
      6.          log n                                          12.       n3

2) (12 points) Ranking the growth rate of functions by descending
      No        Functions            Ranking                     No    Functions           Ranking
      1.             n                                           7.        n2
      2.           0.5n                                          8.      log6 n
      3.         n log n                                         9.        n0.5
      4.             1                                           10.        n!
      5.         n2 log n                                        11.       2n
      6.          log n                                          12.       n3

Example 15 (4 points) Show that f(x) = 5x3 + 2x2 - 4x + 1 is Ω(x4).




Example 16 (4 points) Show that 3x2 + 8x log x is Θ(x2).




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                         เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                           CSC1001 Discrete Mathematics            9
Example 17 (4 points) Find Big-O of f(x) + g(x) if f(x) = 4x5 + 2x – 10 and g(x) = x3 log x + 10x




2. Time Complexity of Algorithms
    The time complexity of an algorithm can be expressed in terms of the number of operations used by the
algorithm when the input has a particular size. The operations used to measure time complexity can be the
comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any
other basic operation.
Example 18 (5 points) Analyze the time complexity of Finding maximum value algorithm.
 procedure maximum({a1, a2, … , an}: integers) {
   max = a1
   for i = 2 to n
     if max < ai then max = ai
   return max
 }




Example 19 (5 points) Analyze the time complexity of an algorithm in Example 3.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                 เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
10      CSC1001 Discrete Mathematics                                                           09 - Algorithms


Example 20 (5 points) Analyze the time complexity of an algorithm in Example 4.




Example 21 (5 points) Analyze the time complexity of an algorithm in Example 5.




Example 22 (5 points) Analyze the time complexity of an algorithm in Example 6.




Example 23 (5 points) Analyze the time complexity of an algorithm in Example 7.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                              เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                       CSC1001 Discrete Mathematics        11
Example 24 (13 points) Find the Big-O notation of a part of Java program.
 No.                       A Part of Java Program                                     Big-O
        int temp = a[i];
  1.    a[i] = a[a.length - i - 1];
        a[a.length - i - 1] = temp;

        for (int i = a.length - 1; i >= 0; i--) {
          if (a[i] == x) {
  2.        System.out.println(i);
          }
        }

        for (int i = 0; i <= n; i = i + 4) {
  3.      System.out.println(a[i]);
        }

        for (int i = 0; i < a.length; i++) {
          for (int j = 0; j < a[i].length; j++) {
  4.        a[i][j] = 13;
          }
        }

        for (int i = 10000000; i >= 2; i--) {
          System.out.println(a[i]);
  5.      System.out.println(a[i - 1]);
          System.out.println(a[i - 2]);
        }

        for (int i = 0; i < n; i++) {
          for (int j = i; j >= 0; j--) {
            System.out.print(a[i][j] + " ");
  6.      }
          System.out.println();
        }

        for (int i = 0; i < n; i++) {
          for (int j = 100; j >= 0; j--) {
            System.out.print(a[i][j] + " ");
  7.      }
          System.out.print("----------------");
          System.out.println();
        }

        for (int i = 0; i <= n; i = i * 2) {
  8.      System.out.println(a[i]);
        }

        for (int i = 0; i < n; i++) {
          for (int j = n; j >= 0; j = j / 5) {
            System.out.print(a[i][j]);
  9.        System.out.println();
          }
        }

        for (int i = 0; i < n; i += 100) {
          for (int j = 0; j <= 200; j++) {
            System.out.print(a[i][j] + " ");
            sum = sum + a[i][j];
  10.     }
        }
        for (int i = 0; i <= n; i = i * 2) {
          System.out.println(b[i]);
        }



มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                            เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
12      CSC1001 Discrete Mathematics                                09 - Algorithms


  No.                       A Part of Java Program           Big-O
        for (int i = 0; i < mul.length; i++) {
          for (int j = 0; j < mul[i].length; j++) {
            for (int k = 0; k < y.length; k++) {
  11.         mul[i][j] += x[i][k] * y[k][j];
            }
          }
        }

        for (int i = 0; i < n; i++) {
          for (int j = i; j >= 0; j -= 2) {
            for (int k = 0; k < n; k *= 10) {
  12.         mul[i][j] += x[i][k] * y[k][j];
            }
          }
        }

        int left = 0, right = n, index = -1;
        while (left <= right) {
          int mid = (left + right) / 2;
  13.     if (key == a[mid]) index = mid;
          else if (key < a[mid]) right = mid - 1;
          else left = mid + 1;
        }




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)   เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี

Más contenido relacionado

La actualidad más candente

เอกสารความสัมพันธ์เชิงฟังก์ชัน
เอกสารความสัมพันธ์เชิงฟังก์ชันเอกสารความสัมพันธ์เชิงฟังก์ชัน
เอกสารความสัมพันธ์เชิงฟังก์ชันkrurutsamee
 
Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07 (For Print)Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07 (For Print)Wongyos Keardsri
 
Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 01-04 (For Print)Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 01-04 (For Print)Wongyos Keardsri
 
Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Wongyos Keardsri
 
ฟังก์ชันเชิงเส้น
ฟังก์ชันเชิงเส้นฟังก์ชันเชิงเส้น
ฟังก์ชันเชิงเส้นY'Yuyee Raksaya
 
กฎของเลขยกกำลัง
กฎของเลขยกกำลังกฎของเลขยกกำลัง
กฎของเลขยกกำลังNiwat Namisa
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsWongyos Keardsri
 
สมการและอสมการ
สมการและอสมการสมการและอสมการ
สมการและอสมการORAWAN SAKULDEE
 
แนวข้อสอบ
แนวข้อสอบแนวข้อสอบ
แนวข้อสอบprapasun
 

La actualidad más candente (20)

Java-Answer Chapter 12-13
Java-Answer Chapter 12-13Java-Answer Chapter 12-13
Java-Answer Chapter 12-13
 
เอกสารความสัมพันธ์เชิงฟังก์ชัน
เอกสารความสัมพันธ์เชิงฟังก์ชันเอกสารความสัมพันธ์เชิงฟังก์ชัน
เอกสารความสัมพันธ์เชิงฟังก์ชัน
 
Calculus1
Calculus1Calculus1
Calculus1
 
Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07 (For Print)Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07 (For Print)
 
Java-Answer Chapter 07
Java-Answer Chapter 07Java-Answer Chapter 07
Java-Answer Chapter 07
 
ใบงานเลขยกกำลังม.5
ใบงานเลขยกกำลังม.5ใบงานเลขยกกำลังม.5
ใบงานเลขยกกำลังม.5
 
Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 01-04 (For Print)Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 01-04 (For Print)
 
Java-Answer Chapter 08-09
Java-Answer Chapter 08-09Java-Answer Chapter 08-09
Java-Answer Chapter 08-09
 
ฟังก์ชัน
ฟังก์ชันฟังก์ชัน
ฟังก์ชัน
 
Java-Answer Chapter 01-04
Java-Answer Chapter 01-04Java-Answer Chapter 01-04
Java-Answer Chapter 01-04
 
Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)Java-Answer Chapter 12-13 (For Print)
Java-Answer Chapter 12-13 (For Print)
 
Expolog clipvidva
Expolog clipvidvaExpolog clipvidva
Expolog clipvidva
 
ฟังก์ชันเชิงเส้น
ฟังก์ชันเชิงเส้นฟังก์ชันเชิงเส้น
ฟังก์ชันเชิงเส้น
 
กฎของเลขยกกำลัง
กฎของเลขยกกำลังกฎของเลขยกกำลัง
กฎของเลขยกกำลัง
 
Java-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and ObjectsJava-Chapter 12 Classes and Objects
Java-Chapter 12 Classes and Objects
 
เมทริกซ์...
เมทริกซ์...เมทริกซ์...
เมทริกซ์...
 
Pat1 expo&log
Pat1 expo&logPat1 expo&log
Pat1 expo&log
 
สมการและอสมการ
สมการและอสมการสมการและอสมการ
สมการและอสมการ
 
แนวข้อสอบ
แนวข้อสอบแนวข้อสอบ
แนวข้อสอบ
 
linear function
linear functionlinear function
linear function
 

Destacado

Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityWongyos Keardsri
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IWongyos Keardsri
 
Introduction to management
Introduction to managementIntroduction to management
Introduction to managementBilal Amjad
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesWongyos Keardsri
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIWongyos Keardsri
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsWongyos Keardsri
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 CountingWongyos Keardsri
 
Tenerife airport disaster klm flight 4805 and pan
Tenerife airport disaster klm flight 4805 and panTenerife airport disaster klm flight 4805 and pan
Tenerife airport disaster klm flight 4805 and panReefear Ajang
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsWongyos Keardsri
 
Chapter 1 Logic of Compound Statements
Chapter 1 Logic of Compound StatementsChapter 1 Logic of Compound Statements
Chapter 1 Logic of Compound Statementsguestd166eb5
 
Discrete Structures. Lecture 1
 Discrete Structures. Lecture 1  Discrete Structures. Lecture 1
Discrete Structures. Lecture 1 Ali Usman
 

Destacado (20)

Discrete-Chapter 07 Probability
Discrete-Chapter 07 ProbabilityDiscrete-Chapter 07 Probability
Discrete-Chapter 07 Probability
 
Chapter 06
Chapter 06 Chapter 06
Chapter 06
 
Chapter 11
Chapter 11 Chapter 11
Chapter 11
 
Chapter 08
Chapter 08 Chapter 08
Chapter 08
 
Discrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part IDiscrete-Chapter 04 Logic Part I
Discrete-Chapter 04 Logic Part I
 
Introduction to management
Introduction to managementIntroduction to management
Introduction to management
 
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and SequencesDiscrete-Chapter 02 Functions and Sequences
Discrete-Chapter 02 Functions and Sequences
 
Discrete-Chapter 01 Sets
Discrete-Chapter 01 SetsDiscrete-Chapter 01 Sets
Discrete-Chapter 01 Sets
 
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part IIDiscrete-Chapter 04 Logic Part II
Discrete-Chapter 04 Logic Part II
 
Discrete-Chapter 08 Relations
Discrete-Chapter 08 RelationsDiscrete-Chapter 08 Relations
Discrete-Chapter 08 Relations
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Chapter 09
Chapter 09 Chapter 09
Chapter 09
 
Discrete-Chapter 06 Counting
Discrete-Chapter 06 CountingDiscrete-Chapter 06 Counting
Discrete-Chapter 06 Counting
 
Tenerife airport disaster klm flight 4805 and pan
Tenerife airport disaster klm flight 4805 and panTenerife airport disaster klm flight 4805 and pan
Tenerife airport disaster klm flight 4805 and pan
 
Discrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and ProofsDiscrete-Chapter 05 Inference and Proofs
Discrete-Chapter 05 Inference and Proofs
 
Set Operations
Set OperationsSet Operations
Set Operations
 
Chapter 1 Logic of Compound Statements
Chapter 1 Logic of Compound StatementsChapter 1 Logic of Compound Statements
Chapter 1 Logic of Compound Statements
 
Set Theory Presentation
Set Theory PresentationSet Theory Presentation
Set Theory Presentation
 
Bab 5
Bab 5Bab 5
Bab 5
 
Discrete Structures. Lecture 1
 Discrete Structures. Lecture 1  Discrete Structures. Lecture 1
Discrete Structures. Lecture 1
 

Similar a Discrete-Chapter 09 Algorithms

บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึมบทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึมภัชรณันติ์ ศรีประเสริฐ
 
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึมบทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึมภัชรณันติ์ ศรีประเสริฐ
 
ใบงานสมการ
ใบงานสมการใบงานสมการ
ใบงานสมการkanjana2536
 
9789740329183
97897403291839789740329183
9789740329183CUPress
 
คณิต มข
คณิต มขคณิต มข
คณิต มขaom08
 
บทที่ 1 การแยกตัวประกอบและการแก้สมการพหุนามดีกรีสอง
บทที่ 1 การแยกตัวประกอบและการแก้สมการพหุนามดีกรีสองบทที่ 1 การแยกตัวประกอบและการแก้สมการพหุนามดีกรีสอง
บทที่ 1 การแยกตัวประกอบและการแก้สมการพหุนามดีกรีสองsawed kodnara
 
1 141202005819-conversion-gate02
1 141202005819-conversion-gate021 141202005819-conversion-gate02
1 141202005819-conversion-gate02Bank Pieamsiri
 
สรุปสูตรและเนื้อหาคณิตศาสตร์ ม.ปลาย
สรุปสูตรและเนื้อหาคณิตศาสตร์ ม.ปลายสรุปสูตรและเนื้อหาคณิตศาสตร์ ม.ปลาย
สรุปสูตรและเนื้อหาคณิตศาสตร์ ม.ปลายCoo Ca Nit Sad
 
สรุปเนื้อหา O- net ม.6
สรุปเนื้อหา O- net ม.6 สรุปเนื้อหา O- net ม.6
สรุปเนื้อหา O- net ม.6 sensehaza
 
สื่อเรื่องกราฟ
สื่อเรื่องกราฟสื่อเรื่องกราฟ
สื่อเรื่องกราฟKanchanid Kanmungmee
 
Exponential and logarithm function
Exponential and logarithm functionExponential and logarithm function
Exponential and logarithm functionThanuphong Ngoapm
 
บทที่ 1 ความรู้เบื้องต้นเกี่ยวกับจำนวนจริงและจำนวนจริงในรูปกรณฑ์
บทที่ 1 ความรู้เบื้องต้นเกี่ยวกับจำนวนจริงและจำนวนจริงในรูปกรณฑ์บทที่ 1 ความรู้เบื้องต้นเกี่ยวกับจำนวนจริงและจำนวนจริงในรูปกรณฑ์
บทที่ 1 ความรู้เบื้องต้นเกี่ยวกับจำนวนจริงและจำนวนจริงในรูปกรณฑ์sawed kodnara
 
บทที่4.pdf
บทที่4.pdfบทที่4.pdf
บทที่4.pdfsewahec743
 
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)Thanuphong Ngoapm
 
ฟังก์ชันเอกโพแนนเชียล
ฟังก์ชันเอกโพแนนเชียลฟังก์ชันเอกโพแนนเชียล
ฟังก์ชันเอกโพแนนเชียลพัน พัน
 

Similar a Discrete-Chapter 09 Algorithms (20)

บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึมบทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
 
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึมบทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
บทที่ 1 เรื่องฟังก์ชันเอกซ์โปเนนเชียลและลอการิทึม
 
ใบงานสมการ
ใบงานสมการใบงานสมการ
ใบงานสมการ
 
9789740329183
97897403291839789740329183
9789740329183
 
4339
43394339
4339
 
คณิต มข
คณิต มขคณิต มข
คณิต มข
 
2
22
2
 
บทที่ 1 การแยกตัวประกอบและการแก้สมการพหุนามดีกรีสอง
บทที่ 1 การแยกตัวประกอบและการแก้สมการพหุนามดีกรีสองบทที่ 1 การแยกตัวประกอบและการแก้สมการพหุนามดีกรีสอง
บทที่ 1 การแยกตัวประกอบและการแก้สมการพหุนามดีกรีสอง
 
1 141202005819-conversion-gate02
1 141202005819-conversion-gate021 141202005819-conversion-gate02
1 141202005819-conversion-gate02
 
สรุปสูตรและเนื้อหาคณิตศาสตร์ ม.ปลาย
สรุปสูตรและเนื้อหาคณิตศาสตร์ ม.ปลายสรุปสูตรและเนื้อหาคณิตศาสตร์ ม.ปลาย
สรุปสูตรและเนื้อหาคณิตศาสตร์ ม.ปลาย
 
สรุปเนื้อหา O- net ม.6
สรุปเนื้อหา O- net ม.6 สรุปเนื้อหา O- net ม.6
สรุปเนื้อหา O- net ม.6
 
51 ตรีโกณมิติ ตอนที่8_ฟังก์ชันตรีโกณมิติผกผัน
51 ตรีโกณมิติ ตอนที่8_ฟังก์ชันตรีโกณมิติผกผัน51 ตรีโกณมิติ ตอนที่8_ฟังก์ชันตรีโกณมิติผกผัน
51 ตรีโกณมิติ ตอนที่8_ฟังก์ชันตรีโกณมิติผกผัน
 
สื่อเรื่องกราฟ
สื่อเรื่องกราฟสื่อเรื่องกราฟ
สื่อเรื่องกราฟ
 
ลอการิทึม..[1]
ลอการิทึม..[1]ลอการิทึม..[1]
ลอการิทึม..[1]
 
Exponential and logarithm function
Exponential and logarithm functionExponential and logarithm function
Exponential and logarithm function
 
บทที่ 1 ความรู้เบื้องต้นเกี่ยวกับจำนวนจริงและจำนวนจริงในรูปกรณฑ์
บทที่ 1 ความรู้เบื้องต้นเกี่ยวกับจำนวนจริงและจำนวนจริงในรูปกรณฑ์บทที่ 1 ความรู้เบื้องต้นเกี่ยวกับจำนวนจริงและจำนวนจริงในรูปกรณฑ์
บทที่ 1 ความรู้เบื้องต้นเกี่ยวกับจำนวนจริงและจำนวนจริงในรูปกรณฑ์
 
บทที่4.pdf
บทที่4.pdfบทที่4.pdf
บทที่4.pdf
 
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)
เมทริกซ์ระดับชั้นมัธยมปลาย(Matrix)
 
ฟังก์ชันเอกโพแนนเชียล
ฟังก์ชันเอกโพแนนเชียลฟังก์ชันเอกโพแนนเชียล
ฟังก์ชันเอกโพแนนเชียล
 
ลิมิต
ลิมิตลิมิต
ลิมิต
 

Más de Wongyos Keardsri

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramWongyos Keardsri
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsWongyos Keardsri
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingWongyos Keardsri
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemWongyos Keardsri
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIWongyos Keardsri
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IWongyos Keardsri
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationWongyos Keardsri
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowWongyos Keardsri
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 RecursionsWongyos Keardsri
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysWongyos Keardsri
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsWongyos Keardsri
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysWongyos Keardsri
 
Java-Chapter 06 File Operations
Java-Chapter 06 File OperationsJava-Chapter 06 File Operations
Java-Chapter 06 File OperationsWongyos Keardsri
 
Java-Chapter 05 String Operations
Java-Chapter 05 String OperationsJava-Chapter 05 String Operations
Java-Chapter 05 String OperationsWongyos Keardsri
 
Java-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration StatementsJava-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration StatementsWongyos Keardsri
 

Más de Wongyos Keardsri (19)

How to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master ProgramHow to Study and Research in Computer-related Master Program
How to Study and Research in Computer-related Master Program
 
The next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applicationsThe next generation intelligent transport systems: standards and applications
The next generation intelligent transport systems: standards and applications
 
IP address anonymization
IP address anonymizationIP address anonymization
IP address anonymization
 
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script ProgrammingSysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 03 Unix Shell Script Programming
 
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating SystemSysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 02 Introduction to Unix Operating System
 
SysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming LanguageSysProg-Tutor 01 Introduction to C Programming Language
SysProg-Tutor 01 Introduction to C Programming Language
 
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part IIIDiscrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part III
 
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part IIDiscrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part II
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
 
Discrete-Chapter 10 Trees
Discrete-Chapter 10 TreesDiscrete-Chapter 10 Trees
Discrete-Chapter 10 Trees
 
Discrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling ComputationDiscrete-Chapter 12 Modeling Computation
Discrete-Chapter 12 Modeling Computation
 
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindowJava-Chapter 14 Creating Graphics with DWindow
Java-Chapter 14 Creating Graphics with DWindow
 
Java-Chapter 11 Recursions
Java-Chapter 11 RecursionsJava-Chapter 11 Recursions
Java-Chapter 11 Recursions
 
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional ArraysJava-Chapter 10 Two Dimensional Arrays
Java-Chapter 10 Two Dimensional Arrays
 
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and ApplicationsJava-Chapter 09 Advanced Statements and Applications
Java-Chapter 09 Advanced Statements and Applications
 
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional ArraysJava-Chapter 07 One Dimensional Arrays
Java-Chapter 07 One Dimensional Arrays
 
Java-Chapter 06 File Operations
Java-Chapter 06 File OperationsJava-Chapter 06 File Operations
Java-Chapter 06 File Operations
 
Java-Chapter 05 String Operations
Java-Chapter 05 String OperationsJava-Chapter 05 String Operations
Java-Chapter 05 String Operations
 
Java-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration StatementsJava-Chapter 04 Iteration Statements
Java-Chapter 04 Iteration Statements
 

Discrete-Chapter 09 Algorithms

  • 1. Algorithms - 09 CSC1001 Discrete Mathematics 1 CHAPTER อัลกอริทึม 9 (Algorithms) 1 Introduction to Algorithms 1. Algorithm Deffinitions Definition 1 An algorithm is a finite sequence of precise instructions or steps for performing a computation or for solving a problem (In computer science usually represent the algorithm by using pseudocode). Example 1 (5 points) Describe an algorithm or write a pseudocode for finding the maximum (largest) value in a finite sequence of integers. procedure maximum({a1, a2, … , an}: integers) { max = a1 for i = 2 to n if max < ai then max = ai return max } Example 2 (5 points) Describe an algorithm or write a pseudocode for finding the minimum value in a finite se- quence of real number. Example 3 (5 points) Describe an algorithm to calculate the average of a finite sequence of integers. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 2. 2 CSC1001 Discrete Mathematics 09 - Algorithms Example 4 (5 points) Describe an algorithm to find the absolute value of integers. Example 5 (5 points) Describe an algorithm to find the factorial value of integers. Example 6 (5 points) Describe an algorithm to find the Fibonacci value of integers (a0 = 0 and a1 = 1). Example 7 (5 points) Describe an algorithm to find the multiplication of two matrices. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 3. Algorithms - 09 CSC1001 Discrete Mathematics 3 2. Searching Algorithms Definition 2 The Linear Search Algorithm procedure linearSearch({a1, a2, … , an}: integers, x: integer) { i = 1 while i ≤ n { if ai = x then return i else i = i + 1 } return -1 } Definition 3 The Binary Search Algorithm procedure binarySearch({a1, a2, … , an}: integers, x: integer) { l = 1 //i is left endpoint of search interval r = n //j is right endpoint of search interval while l < r { m = ⎣ + r) / 2 ⎦ (l if x = am then return m else if x > am then l = m + 1 else r = m - 1 } return -1 } Example 8 (20 points) Consider the iteration of linear search and binary search for searching some value from the input sequence. 1) Search 26 using linear search 2 3 6 8 11 15 21 26 30 39 2) Search 26 using binary search 2 3 6 8 11 15 21 26 30 39 3) Search 3 using linear search 2 3 6 8 11 15 21 26 30 39 มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 4. 4 CSC1001 Discrete Mathematics 09 - Algorithms 4) Search 3 using binary search 2 3 6 8 11 15 21 26 30 39 5) Search 2 using linear search 2 3 6 8 11 15 21 26 30 39 6) Search 2 using binary search 2 3 6 8 11 15 21 26 30 39 7) Search 17 using linear search 2 3 6 8 11 15 21 26 30 39 8) Search 17 using binary search 2 3 6 8 11 15 21 26 30 39 Example 9 (4 points) From an Example 4, can you summarize the different functions or features of linear search and binary search algorithms? มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 5. Algorithms - 09 CSC1001 Discrete Mathematics 5 3. Sorting Algorithms Definition 4 The Bubble Sort Algorithm procedure bubbleSort({a1, a2, … , an}: real number) { for i = n to 2 { for j = 1 to i - 1 { if aj > aj + 1 then { temp = aj aj = aj + 1 aj + 1 = temp } } } } Definition 5 The Selection Sort Algorithm procedure selectionSort({a1, a2, … , an}: real number) { for i = n to 2 { maxIndex = 1 for j = 1 to i { if aj > amaxIndex then maxIndex = j } temp = ai ai = amaxIndex amaxIndex = temp } } Example 10 (20 points) Write the steps of bubble sort and selection sort of this sequence. 1) Using bubble sort 15 30 2 26 21 6 39 3 11 8 มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 6. 6 CSC1001 Discrete Mathematics 09 - Algorithms 2) Using selection sort 15 30 2 26 21 6 39 3 11 8 2 Growth of Functions and Complexity of Algorithms 1. Big-O, Big-Ω and Big-Θ Notation Definition 1 Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is O(g(x)) if there are constants C and k such that |f (x)| ≤ C|g(x)| whenever x > k. This is read as “f (x) is big-oh of g(x).” Definition 2 Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Ω(g(x)) if there are positive constants C and k such that |f (x)| ≥ C|g(x)| whenever x > k. This is read as “f (x) is big-Omega of g(x).” Definition 3 Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g(x)) if there are real numbers C1 and C2 and a positive real number k such that C1|g(x)| ≤ |f (x)| ≤ C2|g(x)| whenever x > k. We say that f (x) is Θ(g(x)) if f (x) is O(g(x)) and f (x) is Ω(g(x)). This is read as “f (x) is big-Omega of g(x).” มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 7. Algorithms - 09 CSC1001 Discrete Mathematics 7 Example 11 (4 points) Show that f(x) = x2 + 2x + 1 is O(x2). Example 12 (4 points) Show that f(x) = 3x4 + 5x2 + 15 is O(x4). Example 13 (4 points) Show that f(x) = 7x2 is O(x3) by replace x into f(x). Example 14 (24 points) Estimate the growth of functions. Figure: A Display of the Commonly Used in Big-O Estimates มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 8. 8 CSC1001 Discrete Mathematics 09 - Algorithms 1) (12 points) Ranking the speed rate of functions by descending No Functions Ranking No Functions Ranking 1. n 7. n2 2. 0.5n 8. log6 n 3. n log n 9. n0.5 4. 1 10. n! 5. n2 log n 11. 2n 6. log n 12. n3 2) (12 points) Ranking the growth rate of functions by descending No Functions Ranking No Functions Ranking 1. n 7. n2 2. 0.5n 8. log6 n 3. n log n 9. n0.5 4. 1 10. n! 5. n2 log n 11. 2n 6. log n 12. n3 Example 15 (4 points) Show that f(x) = 5x3 + 2x2 - 4x + 1 is Ω(x4). Example 16 (4 points) Show that 3x2 + 8x log x is Θ(x2). มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 9. Algorithms - 09 CSC1001 Discrete Mathematics 9 Example 17 (4 points) Find Big-O of f(x) + g(x) if f(x) = 4x5 + 2x – 10 and g(x) = x3 log x + 10x 2. Time Complexity of Algorithms The time complexity of an algorithm can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. The operations used to measure time complexity can be the comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any other basic operation. Example 18 (5 points) Analyze the time complexity of Finding maximum value algorithm. procedure maximum({a1, a2, … , an}: integers) { max = a1 for i = 2 to n if max < ai then max = ai return max } Example 19 (5 points) Analyze the time complexity of an algorithm in Example 3. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 10. 10 CSC1001 Discrete Mathematics 09 - Algorithms Example 20 (5 points) Analyze the time complexity of an algorithm in Example 4. Example 21 (5 points) Analyze the time complexity of an algorithm in Example 5. Example 22 (5 points) Analyze the time complexity of an algorithm in Example 6. Example 23 (5 points) Analyze the time complexity of an algorithm in Example 7. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 11. Algorithms - 09 CSC1001 Discrete Mathematics 11 Example 24 (13 points) Find the Big-O notation of a part of Java program. No. A Part of Java Program Big-O int temp = a[i]; 1. a[i] = a[a.length - i - 1]; a[a.length - i - 1] = temp; for (int i = a.length - 1; i >= 0; i--) { if (a[i] == x) { 2. System.out.println(i); } } for (int i = 0; i <= n; i = i + 4) { 3. System.out.println(a[i]); } for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { 4. a[i][j] = 13; } } for (int i = 10000000; i >= 2; i--) { System.out.println(a[i]); 5. System.out.println(a[i - 1]); System.out.println(a[i - 2]); } for (int i = 0; i < n; i++) { for (int j = i; j >= 0; j--) { System.out.print(a[i][j] + " "); 6. } System.out.println(); } for (int i = 0; i < n; i++) { for (int j = 100; j >= 0; j--) { System.out.print(a[i][j] + " "); 7. } System.out.print("----------------"); System.out.println(); } for (int i = 0; i <= n; i = i * 2) { 8. System.out.println(a[i]); } for (int i = 0; i < n; i++) { for (int j = n; j >= 0; j = j / 5) { System.out.print(a[i][j]); 9. System.out.println(); } } for (int i = 0; i < n; i += 100) { for (int j = 0; j <= 200; j++) { System.out.print(a[i][j] + " "); sum = sum + a[i][j]; 10. } } for (int i = 0; i <= n; i = i * 2) { System.out.println(b[i]); } มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 12. 12 CSC1001 Discrete Mathematics 09 - Algorithms No. A Part of Java Program Big-O for (int i = 0; i < mul.length; i++) { for (int j = 0; j < mul[i].length; j++) { for (int k = 0; k < y.length; k++) { 11. mul[i][j] += x[i][k] * y[k][j]; } } } for (int i = 0; i < n; i++) { for (int j = i; j >= 0; j -= 2) { for (int k = 0; k < n; k *= 10) { 12. mul[i][j] += x[i][k] * y[k][j]; } } } int left = 0, right = n, index = -1; while (left <= right) { int mid = (left + right) / 2; 13. if (key == a[mid]) index = mid; else if (key < a[mid]) right = mid - 1; else left = mid + 1; } มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี