Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

85
Algorithms Chin-Sung Lin Eleanor Roosevelt High School

Transcript of Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Page 1: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Algorithms

Chin-Sung Lin

Eleanor Roosevelt High School

Page 2: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Algorithms

• Types of Algorithms

– Brute-Force (or Exhaustive Search)

– Divide and Conquer

– The Greedy Method

– Recursion

• Algorithms of Skyscrapers Project

– Review of Pseudo Code

– Decimal-to-Binary Conversion Algorithm

• A Crash Course of C

• Sorting Algorithms

• Searching Algorithms

Page 3: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Brute-Force Algorithms

Page 4: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Brute-Force (or Exhaustive Search)

• Brute-force is a straightforward approach to solving a problem, usually directly based on the problem’s statement and definitions of the concepts involved.

• Try out all possible values or combinations to solve a

problem, or try every possible solution to see which is best.

Page 5: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Brute-Force (or Exhaustive Search) Example

• Example:

Try out all possible combinations to open a lock.

• Example:

Try out all possible password values to log into an account.

Page 6: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

• Example:

Compute an for a given number a and a nonnegative

integer n. By the definition of exponentiation, an = a a a.

• Example:

Build 300-block skyscraper by adding one block each week.

n times

Brute-Force (or Exhaustive Search) Example

Page 7: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Though inefficient in general, the brute-force algorithm should not be

overlooked as an important algorithm design strategy for the following

reasons:

• It is applicable to a very wide variety problems, and seems to be

the only general approach for all problems.

• The expense of designing a more efficient algorithm may be

unjustifiable if only a few instances of a problem need to be solved

and a brute-force algorithm can solve those instances with

acceptable speed.

• It is useful for solving small-size instances of a problem.

• It can serve as an important theoretical or education purpose, e.g.,

as a benchmark to judge more efficient alternatives algorithms.

Brute-Force (or Exhaustive Search)

Page 8: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Divide and Conquer Algorithms

Page 9: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Divide and Conquer

• Repeatedly reduces an instance of a problem to one or more

smaller instances of the same problem (usually recursively)

until the instances are small enough to solve easily. The

solutions to the subproblems are then combined to give a

solution to the original problem.

Page 10: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

The most well known algorithm design strategy:

1. Divide the problem into two or more smaller subproblems.

2. Conquer the subproblems by solving them recursively.

3. Combine the solutions to the subproblems into the

solutions for the original problem.

Divide and Conquer

Page 11: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Example:

Large-integer multiplication (The Grade-School Algorithm)

a1 a2 … an

x b1 b2 … bn

(d10) d11d12 … d1n

(d20) d21d22 … d2n

… … … … … … …

(dn0) dn1dn2 … dnn

This process requires n2 single digit multiplications.

For 1231 2012, this process requires 16 multiplications.

Divide and Conquer Example

Page 12: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Large-integer multiplication (Divide and Conquer Algorithm)1231 2012

= (12*102 + 31) * (20*102 + 12)

= (12*20)*104 + c1*102 + 31*12

where c1 = (12+31)*(20+12) – 12*20 – 31*12 = 764

12*20 = (1*10 + 2) * (2*10 + 0)

= (1*2)*102 + c2*10 + 2*0

where c2 = (1+2)*(2+0) – 1*2 – 2*0 = 4

31*12 = (3*10 + 1) * (1*10 + 2)

= (3*1)*102 + c3*10 + 1*2

where c3 = (3+1)*(1+2) – 3*1 – 1*2 = 7

(12+31)*(20+12) = 43 * 32

= (4*10 + 3) * (3*10 + 2)

= (4*3)*102 + c4*10 + 3*2

where c4 = (4+3)*(3+2) – 4*3 – 3*2 = 17

This process requires 9 digit multiplications as opposed to 16.

Divide and Conquer Example

Page 13: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithms

Page 14: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

14

• An optimization problem is one in which you want to find,

not just a solution, but the best solution.

• It is not exhaustive, and does not give an accurate answer

to many problems. But when it works, it is the fastest

method.

• A greedy algorithm works in phases. At each phase:

– You take the best you can get right now, without regard for

future consequences

– You hope that by choosing a local optimum at each step,

you will end up at a global optimum

Greedy Algorithms

Page 15: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

• Example:

Pay out the exact amount of money using US monetary system.

• Example:

The knapsack problem – Given a set of

items, each with a mass and a value,

determine the number of each item to

include in a collection so that the total

weight is less than or equal to a given

limit and the total value is as large as

possible.

Page 16: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

• Example: Dijkstra's algorithm (single-source shortest path problem)

 Works on both directed and undirected graphs. However, all edges must have nonnegative weights.

Input: Weighted graph and source vertex (v) such that all edge weights are nonnegative Output: Lengths of shortest paths (or the shortest paths themselves) from a given source vertex (v) to all other vertices

Greedy Algorithm Example

Page 17: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 18: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 19: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 20: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 21: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 22: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 23: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 24: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 25: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 26: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Greedy Algorithm Example

Page 27: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Recursive Algorithms

Recursive Algorithms

Recursive Algorithms

Recursive Algorithms

Recursive Algorithms

Recursive Algorithms

Recursive Algorithms

Page 28: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Recursion

A recursive function is one which calls itself. To prevent infinite

recursion, you need an if-else statement (of some sort) where

one branch makes a recursive call, and the other branch does

not.

Page 29: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Recursion

Dream (problem)

{

If problems solved

return solution

else

dream(problem)

}

Page 30: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Recursion Example

• Example: Calculate the factorial of N:

N! = N x (N-1) x (N-2) x ……. x 3 x 2 x 1

Traditional algorithm using Loop:

Factorial ()

1. Let index = 1

2. Let product = 1

3. For index = 1 to N  

product = product x index     

4. end-For

5. return product

Page 31: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Recursion Example

•  Example: Calculate the factorial of N:

N! = N x (N-1) x (N-2) x ……. x 3 x 2 x 1

Recursive algorithm:

Factorial(long n)

{     if (n==1)

         return(n);     else          return(n * factorial(n-1));}

Page 32: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Recursion ExampleA traditional algorithm for finding the Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ……)

Fibonacci ()1. Input N 2. Let result0 = 0 3. Let result1 = 1 4. If N = 1 or N = 25. result = N - 16. else7. for loop counter = 2 to N – 1 8. result = result1 + result09. result0 = result110. result1 = result11. end-for loop12. end-if13. output result

Page 33: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Recursion Example

A recursive algorithm for finding the Fibonacci Numbers (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ……)

Fibonacci (N)1. If N = 1 or N = 22. return (N – 1)3. else 4. return Fibonacci (N – 1) + Fibonacci (N – 2) 5. end-if

Page 34: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Algorithms of Skyscrapers Project

Page 35: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Algorithms of Skyscrapers Project

Rules:

• Using prefabricated-modular blocks of 100 meters long, 100 meters

wide, and 5 meters tall.

• The blocks interlock on top and bottom (like Legos), and they cannot be

stacked sideways.

• Using special lifters, putting stacks of blocks at the same height on

ground or on top of another set of equal-height stacks takes one week

regardless of how tall the stacks are or how many stacks are lifted.

• The prefabrication time of the blocks doesn’t count since they are

already in stock.

• No resource/budget limitations (i.e., you can have as many stacks as

possible at the same time),

Page 36: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Algorithms of Skyscrapers Project

Problem Part A:

• If a client wants to build a 100-meter long, 100-meter wide, and

1500-meter high tower as quickly as possible, what is the shortest

amount of time that it will take to build the tower?

• Show your algorithm in both flowchart and pseudocode forms.

Problem Part B:

• Develop a general algorithm for skyscrapers of 100-meter long,

100-meter wide, and N-meter high (where N is a multiple of 5) in

pseudocode format.

Page 37: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Review of Pseudocode

Page 38: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Convention of Pseudocode

Write pseudocode for: • calculating total number of blocks needed for N-meter high

skyscraper.

• calculating the remainder of number of stacks divided by 2.

• creating a while loop to work as long as the number of stacks is

not equal to 1.

• creating a counter to increment the week count in a while loop.

Page 39: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Convention of Pseudocode

Write pseudocode for: • calculating total number of blocks needed for N-meter high

skyscraper.

1. numberBlock = N/5

Page 40: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Convention of Pseudocode

Write pseudocode for: • calculating the remainder of number of stacks divided by 2.

1. Remainder = numberStack % 2

Page 41: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Convention of Pseudocode

Write pseudocode for: • creating a while loop to work as long as the number of stacks is

not equal to 1.

1. While loop numberStack ≠ 1

2. ……

3. end-loop

Page 42: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Convention of Pseudocode

Write pseudocode for: • creating a counter to increment the week count in a while loop.

1. weekCounter = 0

2. While loop numberStack ≠ 1

3. weekCounter = weekCounter + 1

4. end-loop

Page 43: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Group Presentation of

Skyscraper Algorithms

Page 44: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Decimal-to-Binary Conversion Algorithm

Page 45: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Decimal-to-Binary Conversion

2 4 0

1 2 0

6 0

3 0

1 5

7

3

1

1

1

1

0

0

0

2

2

2

2

2

2

2

Decimal: 2 4 0

Binary: 1 1 1 1 0 0 0 0

No. of Division 7

No. of One’s: 4

No. of Weeks: 7 + 4 = 11

0

Page 46: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

A Crash Course of C

Page 47: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Sum of 1+ 2+…+N1. // Calculate the Sum of 1 + 2 + 3 + ….. + N

2. #include <stdio.h>

3. int main ()

4. {

5. int n, i;

6. int sum = 0;

7. printf("Enter N: ");

8. scanf("%d", &n);

9. for (i = 1; i <= n; i++)

10. sum = sum + i;

11. printf("Sum = %d\n", sum);

12. return 0;

13. }

Page 48: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Average of Numbers1. // Calculate the average of three numbers

2. // average = (a + b + c)/3

3. #include <stdio.h>

4. int main()

5. {

6. float a, b, c;

7. float average;

8. printf ("Averaging three numbers \n");

9. printf ("Enter three numbers a, b, and c: ");

10. scanf ("%f %f %f", &a, &b, &c);

11. average = (a + b + c)/3.0;

12. printf("Average = %.2f\n", average);

13. return 0;

14. }

Page 49: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Linear Equation1. // Solve Linear Equation

2. // ax + b = c, x = (c - b)/a

3. #include <stdio.h>

4. int main()

5. {

6. float a, b, c;

7. float x;

8. printf("Solve linear equation: ax + b = c\n");

9. printf("Enter coefficients: a, b, c: ");

10. scanf ("%f %f %f", &a, &b, &c);

11. x = (c - b)/a;

12. printf ("x = %.2f\n", x);

13. return 0;

14. }

Page 50: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Skyscraper Algorithm1. // Skyscraper Algorithm

2. #include <stdio.h>

3. int main()

4. {

5. int noWeek, buildingHeight, noStack, remainder;

6. noWeek = 1;

7. printf ("Hello! Please enter the building height: ");

8. scanf ("%d", &buildingHeight);

9. noStack = buildingHeight/5;

10. while (noStack != 1)

11. {

12. remainder = noStack%2;

13. noStack = noStack/2;

14. noWeek = noWeek + remainder + 1;

15. }

16. printf("Number of Week to Build the Skyscraper is: %d\n", noWeek);

17. return 0;

18. }

Page 51: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Square Root1. #include <stdio.h>

2. #include <math.h>

3. int main()

4. {

5. float a;

6. float x;

7. printf("Please enter the number: ");

8. scanf("%f", &a);

9. if (a < 0)

10. printf("No solution.\n");

11. else

12. {

13. x = sqrt(a);

14. printf("sqrt(%.2f) = %.5f\n", a, x);

15. }

16. return 0;

17. }

Page 52: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Integer Array1. // Integer Array

2. #include <stdio.h>

3. #define MAX 5

4. int main()

5. {

6. int array[MAX];

7. float sum = 0.;

8. int i;

9. printf("Enter 5 integers: ");

10. for (i =0; i < MAX; i++)

11. {

12. scanf("%d", &array[i]);

13. sum = sum + array[i];

14. }

15. for (i =0; i < MAX; i++)

16. printf("array[%d] = %d\n", i, array[i]);

17. printf("Average = %.2f\n", sum/MAX);

18. return 0;

19. }

Page 53: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Integer ArrayOutput:

Enter 5 integers: 1 2 3 4 5

array[0] = 1

array[1] = 2

array[2] = 3

array[3] = 4

array[4] = 5

Average = 3.00

Output:

Enter 5 integers: 1 2 3 4 6

array[0] = 1

array[1] = 2

array[2] = 3

array[3] = 4

array[4] = 6

Average = 3.20

Page 54: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Character Strings1. // Strings are Array of Characters.

2. #include <stdio.h>3. #include <string.h>4. #define MAX_STRING_LENGTH 805. int main()6. {7. char str[MAX_STRING_LENGTH];8. unsigned long length;9. int i;10. str[0] = 'H';11. str[1] = 'e';12. str[2] = 'l';13. str[3] = 'l';14. str[4] = 'o';15. str[5] = '!';16. str[6] = '\0';17. length = strlen(str);18. for (i = 0; i < length; i++) 19. printf("str[%d] = %c\n", i, str[i]);20. printf("\nstring =\t%s\n", str);21. printf("length =\t%lu\n", length);22. return 0;23. }

Page 55: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Character StringsOutput:

str[0] = Hstr[1] = estr[2] = lstr[3] = lstr[4] = ostr[5] = !

string = Hello!length = 6

1. // Strings are Array of Characters.2. #include <stdio.h>3. #include <string.h>4. #define MAX_STRING_LENGTH 805. int main()6. {7. char str[MAX_STRING_LENGTH];8. unsigned long length;9. int i;10. str[0] = 'H';11. str[1] = 'e';12. str[2] = 'l';13. str[3] = 'l';14. str[4] = 'o';15. str[5] = '!';16. str[6] = '\0';17. length = strlen(str);18. for (i = 0; i < length; i++) 19. printf("str[%d] = %c\n", i, str[i]);20. printf("\nstring =\t%s\n", str);21. printf("length =\t%lu\n", length);22. return 0;23. }

Page 56: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Strings Operations1. // Strings IO from Command Line2. // String Comparison, Concatenation &

Copy3. #include <stdio.h>4. #include <string.h>5. #define MAX_STRING_LENGTH 806. int main()7. {8. char str1[MAX_STRING_LENGTH];9. char str2[MAX_STRING_LENGTH];10. char str3[MAX_STRING_LENGTH];11. printf("\nString 1: ");12. scanf("%s", str1);13. printf("\nString 2: ");14. scanf("%s", str2);15. printf("\nString1 =\t%s\n", str1);16. printf("\nString2 =\t%s\n", str2);

17. if(strcmp(str1, str2) > 0)18. {19. printf("\nString1 > String2\n");20. strcat(str1, str2);21. printf("\nString1=\t%s\n", str1);22. printf("\nString2=\t%s\n", str2);23. }24. else if (strcmp(str1, str2) < 0)25. {26. printf("\nString1 < String2\n");27. strcat(str2, str1);28. printf("\nString1=\t%s\n", str1);29. printf("\nString2=\t%s\n", str2);30. }31. else32. {33. printf("\nString1 = String2\n");34. strcpy(str3, str1);35. printf("\nString3: %s\n", str3);36. }37. return 0;38. }

Page 57: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

ASCII Code

Page 58: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: Strings Operations

Output:

String 1: Kaela

String 2: Eva

String1 = Kaela

String2 = Eva

String1 > String2

String1= KaelaEva

String2= Eva

Output:

String 1: Dante

String 2: Pawan

String1 = Dante

String2 = Pawan

String1 < String2

String1= Dante

String2= PawanDante

Output:

String 1: ELRO

String 2: ELRO

String1 = ELRO

String2 = ELRO

String1 = String2

String3: ELRO

Page 59: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: File R/W, Strings & Array1. // Read / Write Strings and Integers from / to Files2. #include <stdio.h>3. #define FILESIZE 104. #define STRINGLENGTH 155. int main()6. {7. FILE* fileInput = NULL;8. FILE* fileOutput = NULL;9. char str[FILESIZE][STRINGLENGTH];10. int number[FILESIZE];11. int i;12. fileInput = fopen("input.txt", "r");13. fileOutput = fopen("output.txt", "w"); 14. for (i = 0; i < FILESIZE; i++)15. fscanf (fileInput, "%s %d", str[i], &number[i]);16. for (i = 0; i < FILESIZE; i++)17. fprintf(fileOutput, "string[%d] = %13s number[%d] = %3d\n\n", i, str[i], i, number[i]*2);18. for (i = 0; i < FILESIZE; i++)19. fprintf(fileOutput, "%s ", str[i]);20. fprintf(fileOutput, "\n\n");21. fclose(fileInput);22. fclose(fileOutput);23. }

Page 60: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: File R/W, Strings & ArrayFILE: in.txt

The 10purpose 20of 30this 40project 50is 60to 70have 80fun 90:) 100

FILE: output.txt

string[0] = The number[0] = 20

string[1] = purpose number[1] = 40

string[2] = of number[2] = 60

string[3] = this number[3] = 80

string[4] = project number[4] = 100

string[5] = is number[5] = 120

string[6] = to number[6] = 140

string[7] = have number[7] = 160

string[8] = fun number[8] = 180

string[9] = :) number[9] = 200

The purpose of this project is to have fun :)

Page 61: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

C Example: File R/W, Strings & ArrayFILE: input.txt

ELRO 1Computational 2Service 3Company 4is 5based 6in 7New 8York 9City. 10

FILE: output.txt

string[0] = ELRO number[0] = 2

string[1] = Computational number[1] = 4

string[2] = Service number[2] = 6

string[3] = Company number[3] = 8

string[4] = is number[4] = 10

string[5] = based number[5] = 12

string[6] = in number[6] = 14

string[7] = New number[7] = 16

string[8] = York number[8] = 18

string[9] = City. number[9] = 20

ELRO Computational Service Company is based in New York City.

Page 62: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Sorting Algorithms

Page 63: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Sorting Algorithms

• Selection Sort I

• Selection Sort 2

• Bubble Sort

• Insertion Sort

Page 64: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Sort Functions1. // This is the main function which can call different sort functions

2.

3. int main(int argc, const char * argv[])

4. {

5. int i, number;

6. int a[20];

7. printf("Number of integers: ");

8. scanf("%d", &number);

9. printf("Enter %d integers: ", number);

10. for (i = 0; i< number; i++)

11. scanf("%d", &a[i]);

12. sort(a, number);13. for (i = 0; i< number; i++)

14. printf("%d ", a[i]);

15. printf("\n");

16. return 0;

17. }

Page 65: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Selection Sort I

• Selection Sort I compares the element in the first position with the

elements in the rest of the array in order.

• Swap them if the first one is greater than any other element in the

array, and result in the smallest element in the first position.

• Find the second smallest element through the same procedure.

• Continue until the array is sorted

Page 66: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Selection Sort I Example

3 5 1 2 4

3 5 1 2 4

1 5 3 2 4

1 5 3 2 4

1 5 3 2 4

1 3 5 2 4

1 2 5 3 4

1 2 5 3 4

1 2 3 5 4

1 2 3 5 4

1 2 3 4 5

1 2 3 4 5

Page 67: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Selection Sort I Code1. // Selection Sort: Compare the elements with the elements in the rest of the

2. // array in order. Swap them until the whole array is sorted.

3.

4. void SelectionSort (int a[], int array_size)

5. {

6. int i, j, temp;

7. for (i = 0; i < array_size-1; i++)

8. for (j = i+1; j < array_size; j++)

9. if (a[i] > a[j])

10. {

11. temp = a[i];

12. a[i] = a[j];

13. a[j] = temp;

14. }

15. }

3 5 1 2 43 5 1 2 41 5 3 2 41 5 3 2 41 5 3 2 41 3 5 2 41 2 5 3 41 2 5 3 41 2 3 5 41 2 3 5 41 2 3 4 51 2 3 4 5

Page 68: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Selection Sort II

• It basically determines the minimum (or maximum) of the list and

swaps it with the element at the index where its supposed to be.

• Find the smallest element in the array.

• Exchange it with the element in the first position.

• Find the second smallest element and exchange it with the element in

the second position.

• Continue until the array is sorted.

Page 69: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Selection Sort II Example

3 5 1 2 4

1 5 3 2 4

1 2 3 5 4

1 2 3 5 4

1 2 3 4 5

1 2 3 4 5

Page 70: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Selection Sort II Code1. // Selection Sort: It basically determines the minimum (or maximum) of the list and

2. // swaps it with the element at the index where its supposed to be.

3.

4. void SelectionSort (int a[], int array_size)

5. {

6. int i;

7. int j, min, temp;

8. for (i = 0; i < array_size - 1; i++)

9. {

10. min = i;

11. for (j = i+1; j < array_size; j++)

12. {

13. if (a[j] < a[min])

14. min = j;

15. }

16. temp = a[i];

17. a[i] = a[min];

18. a[min] = temp;

19. }

20. }

3 5 1 2 4

1 5 3 2 4

1 2 3 5 4

1 2 3 5 4

1 2 3 4 5

1 2 3 4 5

Page 71: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Bubble Sort

• Bubble Sort works by comparing each element of the list with the

element next to it and swapping them if required.

• When we compare pairs of adjacent elements and none are out of

order, the list is sorted.

• If any are out of order, we must have to swap them to get an ordered

list.

• Bubble sort will make passes though the list swapping any adjacent

elements that are out of order.

Page 72: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Bubble Sort

3 5 1 2 4

3 5 1 2 4

3 1 5 2 4

3 1 2 5 4

3 1 2 4 5

1 3 2 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

Page 73: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Bubble Sort Code1. // Bubble Sort: comparing each element of the list with the element

2. // next to it and swapping them if required.

3.

4. void BubbleSort (int a[], int array_size)

5. {

6. int i, j, temp;

7. for (i = 0; i < (array_size - 1); ++i)

8. for (j = 0; j < array_size - 1 - i; ++j )

9. if (a[j] > a[j+1])

10. {

11. temp = a[j+1];

12. a[j+1] = a[j];

13. a[j] = temp;

14. }

15. }

3 5 1 2 4

3 5 1 2 4

3 1 5 2 4

3 1 2 5 4

3 1 2 4 5

1 3 2 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

Page 74: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Insertion Sort

• Insertion Sort starts from the beginning, traverses through the list and

as you find elements misplaced by precedence you remove them and

insert them back into the right position. Eventually what you have is a

sorted list of elements.

• Adding a new element to a sorted list will keep the list sorted if the

element is inserted in the correct place.

• A single element list is sorted.

• Inserting a second element in the proper place keeps the list sorted.

• This is repeated until all the elements have been inserted into the

sorted part of the list.

Page 75: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Insertion Sort

3 5 1 2 4

3 5 1 2 4

1 3 5 2 4

1 2 3 5 4

1 2 3 4 5

Page 76: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Insertion Sort Code1. // Insertion Sort: You start from the beginning, traverse through the list

2. // and as you find elements misplaced by precedence you remove them

3. // and insert them back into the right position.

4.

5. void insertionSort (int a[], int array_size)

6. {

7. int i, j, key;

8. for (i = 1; i < array_size; i++)

9. {

10. key = a[i];

11. for (j = i; j > 0 && a[j-1] > key; j--)

12. a[j] = a[j-1];

13. a[j] = key;

14. }

15. }

3 5 1 2 4

3 5 1 2 4

1 3 5 2 4

1 2 3 5 4

1 2 3 4 5

Page 77: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Searching Algorithms

Page 78: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Searching Algorithms

• Linear Search

• Binary Search

Page 79: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Search Functions1. // This is the main function which can call different search functions

2. #include <stdio.h>

3. int main()

4. {

5. int i, number, key;

6. int a[20];

7. printf("Number of integers: ");

8. scanf("%d", &number);

9. printf("Enter %d integers: ", number);

10. for (i = 0; i< number; i++)

11. scanf("%d", &a[i]);

12. printf("The array of integers:\n");

13. for (i = 0; i< number; i++)

14. printf("%d ", a[i]);

15. printf("\n");

16. printf("Enter your search key: ");

17. scanf("%d", &key);

18. search (key, a, number);19. return 0;

20. }

Page 80: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Linear Search

• Linear Search is the simplest searching algorithm.

• It uses a loop to sequentially step through an array, starting with the

first element.

• It compares each element with the value being searched for and stops

when that value is found or the end of the array is reached.

Page 81: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Linear Search Code1. // Linear Search

2. int LinearSearch (int key, int a[], int array_size)

3. {

4. int i, found = 0;

5. for(i = 0; i < array_size; i++)

6. {

7. if (a[i] == key)

8. {

9. found = 1;

10. printf ("%2d] %2d\n\n", i, a[i]);

11. }

12. }

13. if (found == 1)

14. return 1;

15. else

16. {

17. printf ("Can not find %d\n", key);

18. return -1;

19. }

20. }

Page 82: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Binary Search

• Binary search is much more efficient than the linear search.

• It requires the list to be in order.

• The algorithm starts searching with the middle element.

• If the item is less than the middle element, it starts over searching the

first half of the list.

• If the item is greater than the middle element, the search starts over

starting with the middle element in the second half of the list.

• It then continues halving the list until the item is found.

Page 83: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Binary Search

Page 84: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Binary Search Code1. // Binary Search:

2.

3. int binsearch(int x, int v[], int n)

4. {

5. int low,high,mid;

6. low = 0;

7. high = n - 1;

8. while(low<=high)

9. {

10. mid = (low + high) / 2;

11. if(x < v[mid])

12. {

13. high = mid + 1;

14. }

15. else if(x > v[mid])

16. {

17. low = mid + 1;

18. }

19. else

20. {

21. return mid;

22. }

23. return -1;

24. }

25. }

Page 85: Algorithms Chin-Sung Lin Eleanor Roosevelt High School.

Q & A