+ All Categories
Home > Documents > Week11.pptx

Week11.pptx

Date post: 14-Apr-2018
Category:
Upload: minh-tieu
View: 221 times
Download: 0 times
Share this document with a friend

of 42

Transcript
  • 7/27/2019 Week11.pptx

    1/42

    CS1010: Programming Methodologyhttp://www.comp.nus.edu.sg/~cs1010/

    http://www.comp.nus.edu.sg/~cs1101/http://www.comp.nus.edu.sg/~cs1101/
  • 7/27/2019 Week11.pptx

    2/42

    Week 11: Searching and Sorting

    Objectives: Understand the basic searching algorithms and

    sorting algorithms

    Introduce the concept of complexity analysis

    (informally)

    Implement the searching and sorting algorithms

    using arrays.

    Reference: Chapter 6 Arrays

    Lesson 6.7

    CS1010 (AY2011/2 Semester 1) Week11 - 2

  • 7/27/2019 Week11.pptx

    3/42

    Week 11: Outline (1/2)

    0. Week 10 Strings Exercise 4: Counting the

    Exercise 5: Find Elements

    Command-line Arguments

    1. Introduction

    2. Searching

    3. Linear Search (Demo #1)

    4. Exercise #1: Compatible Teammate

    5. Binary Search

    CS1010 (AY2011/2 Semester 1) Week11 - 3

  • 7/27/2019 Week11.pptx

    4/42

    Week 11: Outline (2/2)

    6. Sorting

    7. Selection Sort (Demo #2)

    8. Bubble Sort (Demo #3)

    9. More Sorting Algorithms10. Animated Sorting Algorithms

    11. Exercise #2 (take-home): Module Sorting

    CS1010 (AY2011/2 Semester 1) Week11 - 4

  • 7/27/2019 Week11.pptx

    5/42

    Week 10: 9. Ex4 (take-home): Countingthe (1/2) Write a program Week10_CountThe.c to count the

    number of times the appears in users input.

    The program ends when the user enters an empty

    string.

    The string the may be entered with leading or/and

    trailing spaces, may contain uppercase or lowercase

    letter, or may be part of a longer word.

    You may assume that each string entered by the usercontains at most 30 characters.

    Hint: Use the strstr() function and other appropriate

    function(s).

    Week10 - 5CS1010 (AY2011/2 Semester 1)

  • 7/27/2019 Week11.pptx

    6/42

    Week 10: 9. Ex4 (take-home): Countingthe (1/2) A sample run:

    Enter strings, each with

  • 7/27/2019 Week11.pptx

    7/42

    Week 10: 10. Exercise #5: Find Elements

    Write a program Week10_FindElements.c that breaks acompound into its elemental components, assuming that each

    element name begins with an uppercase letter.

    (If you are interested in the element period table, you may refer

    to: http://www.webelements.com/)

    You may assume that each compound is at most 10 characterslong, and each element is at most 3 characters.

    Hint: Use strcpy and strncpy.

    Sample run:Enter a compound: H2ZrCO4

    The elements are:H2ZrCO4

    Week10 - 7CS1010 (AY2011/2 Semester 1)

    http://www.webelements.com/http://www.webelements.com/
  • 7/27/2019 Week11.pptx

    8/42

    Week 10: 11. Command-line Arguments (1/2)

    So far, our main function header looks like this:int main(void)

    We can pass arguments to a program when we run it:

    a.out water "ice cream" 34+7

    Add two parameters in the main function header:

    int main(int argc, char *argv[])

    Parameterargcstands for argument count

    Parameterargvstands for argument vector. It is an array of

    pointers to strings.

    argv[0] is the name of the executable file (sometimes also

    called the command)

    You can name them anything, but the names argc and argv

    are commonly used.

    Week10 - 8CS1010 (AY2011/2 Semester 1)

  • 7/27/2019 Week11.pptx

    9/42

    #include int main(int argc, char *argv[]){

    int count;printf ("This program was called with \%s\\n, argv[0]);if (argc > 1)

    for (count = 1; count < argc; count++)

    printf("argv[%d] = %s\n", count, argv[count]);else

    printf("The command had no argument.\n");

    return 0;}

    Week10 - 9CS1010 (AY2011/2 Semester 1)

    Week 10: 11. Command-line Arguments (2/2)

    gcc Week10_CommandLineArgs.c

    a.out water "ice cream" 34+7This program was called with "a.out"argv[1] = waterargv[2] = ice creamargv[3] = 34+7

    Week10_CommandLineArgs.c

  • 7/27/2019 Week11.pptx

    10/42

    You have accumulated quite a bit of basic programmingexperience by now.

    Today, we will study some simple yet useful classical

    algorithms which find their place in many CS applications

    Searching for some data amid very large collection of data

    Sorting very large collection of data according to some order

    We will begin with an algorithm (idea), and show how the

    algorithm is transformed into a C program

    (implementation).

    This brings back (reminds you) our very first lecture: the

    importance of beginning with an algorithm.

    CS1010 (AY2011/2 Semester 1) Week11 - 10

    1. Introduction

  • 7/27/2019 Week11.pptx

    11/42

    Searching is a common task that we carry out withoutmuch thought everyday.

    Searching for a location in a map

    Searching for the contact of a particular person

    Searching for a nice picture for your project report Searching for a research paper required in your course.

    In this lecture, you will learn how to search for an item

    (sometimes called a search key) in an array.

    CS1010 (AY2011/2 Semester 1) Week11 - 11

    2. Searching (1/2)

  • 7/27/2019 Week11.pptx

    12/42

    Problem statement:Given a list (collection of data) and a search key X, return the

    position of X in the list if it exists.

    For simplicity, we shall assume there are no duplicate values in

    the list. We will count the number of comparisons the algorithms

    make to analyze their performance.

    The ideal searching algorithm will make the least possible

    number of comparisons to locate the desired data.

    We will introduce worst-case scenario.

    (This topic is called analysis of algorithms, which will be formally

    introduced in CS1020. Here, we will give an informal introduction just for

    an appreciation.)

    CS1010 (AY2011/2 Semester 1) Week11 - 12

    2. Searching (2/2)

  • 7/27/2019 Week11.pptx

    13/42

    Also known as Sequential Search

    Idea: Search the list from one end to the other end in linear

    progression.

    Algorithm

    CS1010 (AY2011/2 Semester 1) Week11 - 13

    // Search for key in list A with n items

    linear_search(A, n, key)

    {

    for i = 0 to n-1

    if Ai is key then report i}

    87 12 51 9 24 63

    Example: Search for 24in this list

    no no no no yes!

    Question: What to report if key is not found?

    Aim for a clean design

    3. Linear Search

  • 7/27/2019 Week11.pptx

    14/42

    If the list is an array, how would youimplement the Linear Search algorithm?

    CS1010 (AY2011/2 Semester 1) Week11 - 14

    // To search for key in arr using linear search// Return index if found; otherwise return -1

    int linearSearch(int arr[], int size, int key){int i;for (i=0; i

  • 7/27/2019 Week11.pptx

    15/42

    We use the number of comparisons here as a roughmeasurement.

    Analysis is done for best case, average case, and worstcase. We will focus on the worst case.

    For an array with n elements, in the worst case,

    What is the number of comparisons in our linear_searchalgorithm?

    Under what circumstances do we encounter the worst case?

    CS1010 (AY2011/2 Semester 1) Week11 - 15

    3. Linear Search: Performance

  • 7/27/2019 Week11.pptx

    16/42

    Problem: Given three arrays: The first array contains theplayers names. The second array stores the players ages

    corresponding to the entries in the first array. The third array

    contains the gender information for each player corresponding

    to the entries in the first array.

    Your task is to accept as input a players name and find acompatible teammate for this player. A compatible teammate

    is one who is of the same age but opposite sex.

    Analysis:

    Inputs: char player_name[STR_LENGTH+1];char names[MAX_PLAYER][STR_LENGTH+1];

    int ages[MAX_PLAYER];

    char genders[MAX_PLAYER];

    CS1010 (AY2011/2 Semester 1) Week11 - 16

    4. Exercise #1: Compatible Teammate (1/3)

  • 7/27/2019 Week11.pptx

    17/42

    CS1010 (AY2011/2 Semester 1) Week11 - 17

    Sample run:

    Given an incomplete program Week11_TeamMate.c,

    complete the program by filling in the search_teammate()

    function.

    Enter 5 players info:Alvin 23 MByron 21 MMay 19 FJune 21 F

    Jupiter 22 MThe list of players are:Name Age GenderAlvin 23 MByron 21 MMay 19 FJune 21 FJupiter 22 M

    Enter a player's name: ByronByrons compatible teammate is June.

    Enter a player's name:AlvinSorry, we cannot find a teammate forAlvin!

    or

    Enter a player's name: John

    No such player John.

    or

    4. Exercise #1: Compatible Teammate (2/3)

  • 7/27/2019 Week11.pptx

    18/42

    CS1010 (AY2011/2 Semester 1) Week11 - 18

    int main(void) {char names[MAX_PLAYER][STR_LENGTH+1];

    int ages[MAX_PLAYER];char genders[MAX_PLAYER];char player_name[STR_LENGTH+1];int i, result;

    printf("Enter %dplayers' info:\n", MAX_PLAYER);for (i=0; i

  • 7/27/2019 Week11.pptx

    19/42

    5. Binary Search (1/6)

    You are going to witness a radically different approach,one that has become the basis of many well-knownalgorithms.

    The idea is simple and fantastic, but applied on the

    searching problem, it has this pre-condition that the listmust be sorted before-hand.

    How the data is organized (in this case, sorted) usuallyaffects how we choose/design an algorithm to accessthem.

    In other words, sometimes (actually, very often) we seekout new way to organize the data so that we can processthem more efficiency.

    CS1010 (AY2011/2 Semester 1) Week11 - 19

  • 7/27/2019 Week11.pptx

    20/42

    5. Binary Search (2/6)

    The algorithm Look for the key in the middle position of the list. Either of the

    following 2 cases happens:

    If the key is smaller than the middle element, then discard the

    right half of the list and repeat the process.

    If the key is greater than the middle element, then discard the

    left half of the list and repeat the process.

    Terminating condition: when the key is found, or when all

    elements have been discarded

    CS1010 (AY2011/2 Semester 1) Week11 - 20

  • 7/27/2019 Week11.pptx

    21/42

    5. Binary Search (3/6)

    Example: Search for key = 23

    CS1010 (AY2011/2 Semester 1) Week11 - 21

    5 12 17 23 38 44 77 84 90

    [0] [1] [2] [3] [4] [5] [6] [7] [8]array

    1. low = 0, high = 8, mid = (0+8)/2 = 4

    2. low = 0, high = 3, mid = (0+3)/2 = 13. low = 2, high = 3, mid = (2+3)/2 = 2

    4. low = 3, high = 3, mid = (3+3)/2 = 3

    Found!

    Return 3

  • 7/27/2019 Week11.pptx

    22/42

    5. Binary Search (4/6)

    In binary search, each step eliminates the problem size(array size) by half.

    The problem size gets reduced to 1 very quickly (see next slide)

    This is a simple yet powerful strategy, of halving the

    solution space in each step Which lab exercise employs similar strategy?

    Such strategy, a special case ofdivide-and-conquerparadigm, can be naturally implemented using recursion

    (a topic we have just covered last week) At the moment, we will stick to repetition (loop)

    You can write the recursion version by yourself after class.

    CS1010 (AY2011/2 Semester 1) Week11 - 22

  • 7/27/2019 Week11.pptx

    23/42

    5. Binary Search (5/6): Performance

    Worst-case analysis

    CS1010 (AY2011/2 Semester 1) Week11 - 23

    Array size

    n

    Linear Search

    (ncomparisons)

    Binary search

    (log2ncomparisons)

    100 100 7

    1,000 1,000 10

    10,000 10,000 14

    100,000 100,000 ?

    1,000,000 1,000,000 ?

    109 109 ?

  • 7/27/2019 Week11.pptx

    24/42

    // To search for key in sorted arr using binary search// Return index if found; otherwise return -1int binarySearch(int arr[], int size, int key){

    int low = 0, high = size 1, mid = (low + high)/2;

    }

    binary_search.c

    5. Binary Search (6/6): Code

    Using iteration. (We will discuss the recursive version in discussionsession.)

    CS1010 (AY2011/2 Semester 1) Week11 - 24

  • 7/27/2019 Week11.pptx

    25/42

    6. Sorting (1/2) Sorting is any process of arranging items in some

    sequence and/or in different sets Wikipedia.

    Sorting is important because once a set of items is sorted,

    many problems (such as searching) become easy. Searching can be speeded up.

    Determining whether the items in a set are all unique. Finding the median item in the set.

    etc.

    CS1010 (AY2011/2 Semester 1) Week11 - 25

  • 7/27/2019 Week11.pptx

    26/42

    6. Sorting (2/2)

    Problem statement:Given a list ofn items, arrange the items into ascending

    order.

    We will implement the list as an integer array.

    We will introduce two basic sort algorithms.

    We will count the number of comparisons the algorithms

    make to analyze their performance.

    The ideal sorting algorithm will make the least possible number of

    comparisons to arrange data in a designated order.

    We will compare the algorithms by analyzing theirworst-

    case performance.

    CS1010 (AY2011/2 Semester 1) Week11 - 26

  • 7/27/2019 Week11.pptx

    27/42

    7. Selection Sort (1/3)

    CS1010 (AY2011/2 Semester 1) Week11 - 27

    The algorithmStep 1: Find the smallest element in the list (find_min)

    Step 2: Swap this smallest element with the element in the first

    position. (Now, the smallest element is in the right place.)

    Step 3: Repeat steps 1 and 2 with the list having one fewerelement (i.e. the smallest element just found and placed is

    discarded from further processing).

  • 7/27/2019 Week11.pptx

    28/42

    7. Selection Sort (2/3)

    CS1010 (AY2011/2 Semester 1) Week11 - 28

    n = 9

    23 17 5 90 12 44 38 84 77

    [0] [1] [2] [3] [4] [5] [6] [7] [8]array

    1st pass:

    first min

    5 17 23 90 12 44 38 84 772nd pass:

    first min

    5 12 23 90 17 44 38 84 773rd pass:

    first min

    5 12 17 90 23 44 38 84 774th pass:

    first min

  • 7/27/2019 Week11.pptx

    29/42

    7. Selection Sort (3/3)

    CS1010 (AY2011/2 Semester 1) Week11 - 29

    n = 9

    5 12 17 23 90 44 38 84 775th pass:

    first min

    5 12 17 23 38 44 90 84 776th pass:

    first min

    5 12 17 23 38 44 90 84 777th pass:

    first min

    5 12 17 23 38 44 77 84 908th pass:

    first min

    5 12 17 23 38 44 77 84 90Final array:

    Q: How many passes for

    an array with n elements?

  • 7/27/2019 Week11.pptx

    30/42

    7. Demo #2: Selection Sort

    CS1010 (AY2011/2 Semester 1) Week11 - 30

    // To sort arr in increasing order

    voidselectionSort(int arr[], int size){

    int i, start_index, min_index, temp;

    for (start_index = 0; start_index < size-1; start_index++){

    // each iteration of the for loop is one pass

    // find the index of minimum elementmin_index = start_index;for (i = start_index+1; i < size; i++)

    if (arr[i] < arr[min_index])min_index = i;

    // swap minimum element with element at start_indextemp = arr[start_index];arr[start_index] = arr[min_index];arr[min_index] = temp;

    }}

    See selection_sort.c for full program

  • 7/27/2019 Week11.pptx

    31/42

    7. Selection Sort: Performance We choose the number of comparisons as our basis of analysis.

    Comparisons of array elements occur in the inner loop, where theminimum element is determined.

    Assuming an array with n elements. Table below shows the numberof comparisons for each pass.

    The total number of comparisons is calculated in the formula below.

    Such an algorithm is call an n2 algorithm, orquadratic algorithm, interms of running time complexity.

    CS1010 (AY2011/2 Semester 1) Week11 - 31

    Pass #comparisons

    1 n 1

    2 n 2

    3 n 3

    n 1 1

  • 7/27/2019 Week11.pptx

    32/42

    8. Bubble Sort

    Selection sort makes one exchange at the end of eachpass.

    What if we make more than one exchange during each

    pass?

    The key idea of the bubble sort is to make pairwise

    comparisons and exchange the positions of the pair if

    they are out of order.

    CS1010 (AY2011/2 Semester 1) Week11 - 32

  • 7/27/2019 Week11.pptx

    33/42

    8. One Pass of Bubble Sort0 1 2 3 4 5 6 7 8

    23 17 5 90 12 44 38 84 77

    17 23 5 90 12 44 38 84 77

    17 5 23 90 12 44 38 84 77

    17 5 23 12 90 44 38 84 77

    17 5 23 12 44 90 38 84 77

    17 5 23 12 44 38 90 84 77

    17 5 23 12 44 38 84 90 77

    17 5 23 12 44 38 84 77 90

    exchange

    ok

    exchange

    exchange

    exchange

    exchange

    exchange

    exchange

    CS1010 (AY2011/2 Semester 1) Week11 - 33

    Done!Q: Is the array sorted?Q: What did we achieve?

  • 7/27/2019 Week11.pptx

    34/42

    8. Demo #3: Bubble Sort

    CS1010 (AY2011/2 Semester 1) Week11 - 34

    // To sort arr in increasing order

    voidbubbleSort(int arr[], int size){

    int i, limit, temp;

    for (limit = size-2; limit >= 0; limit--){

    // limit is where the inner loop variable i should end

    for (i=0; i arr[i+1]) // swap arr[i] with arr[i+1]{

    temp = arr[i];

    arr[i] = arr[i+1];arr[i+1] = temp;}

    }}

    }

    See bubble_sort.c for full program

    Can we improved on this?

    Will discuss on Friday.

  • 7/27/2019 Week11.pptx

    35/42

    8. Bubble Sort Performance Bubble sort, like selection sort, requires n 1 passes for an array

    with n elements.

    The comparisons occur in the inner loop. The number of

    comparisons in each pass is given in the table below.

    The total number of comparisons is calculated in the formulabelow.

    Like selection sort, bubble sort is also an n2 algorithm, orquadratic algorithm, in terms of running time complexity.

    CS1010 (AY2011/2 Semester 1) Week11 - 35

    Pass #comparisons

    1 n 1

    2 n 2

    3 n 3

    n 1 1

  • 7/27/2019 Week11.pptx

    36/42

    8. Bubble Sort: Enhanced version

    It is possible to enhance bubble sort algorithm to reduce

    the number of passes.

    Suppose that in a certain pass, no swap is needed. This

    implies that the array is already sorted, and hence the

    algorithm may terminate without going on to the nextpass.

    You will write this enhanced version for your discussion

    session.

    CS1010 (AY2011/2 Semester 1) Week11 - 36

  • 7/27/2019 Week11.pptx

    37/42

    9. More Sorting Algorithms

    What we have introduced are 2 basic sort algorithms.Together with the Insertion Sort algorithm, these 3

    algorithms are the simplest.

    However, they are very slow, as their running time

    complexity is quadratic.

    Faster sorting algorithms exist and are topics in more

    advanced modules such as CS1020.

    CS1010 (AY2011/2 Semester 1) Week11 - 37

  • 7/27/2019 Week11.pptx

    38/42

    10. Animated Sorting Algorithms

    There are a number of animated sorting algorithms on

    the Internet

    Here are two sites:

    http://www.sorting-algorithms.com/

    http://www.cs.ubc.ca/~harrison/Java/sorting-demo.html

    CS1010 (AY2011/2 Semester 1) Week11 - 38

    http://www.sorting-algorithms.com/http://www.cs.ubc.ca/~harrison/Java/sorting-demo.htmlhttp://www.cs.ubc.ca/~harrison/Java/sorting-demo.htmlhttp://www.cs.ubc.ca/~harrison/Java/sorting-demo.htmlhttp://www.cs.ubc.ca/~harrison/Java/sorting-demo.htmlhttp://www.sorting-algorithms.com/http://www.sorting-algorithms.com/http://www.sorting-algorithms.com/
  • 7/27/2019 Week11.pptx

    39/42

    11. Exercise #2 (take-home): Module Sorting Write a program Week11_ModuleSorting.c: Given two arrays

    with 10 elements: one containing the code of the modules, andthe other containing the number of students enrolled in that

    module. Sort the modules according to the number of students

    enrolled in that module, using Selection Sort, Bubble Sort, or

    Insertion Sort (to be discussed at next lecture).

    Sample run:

    CS1010 (AY2011/2 Semester 1) Week11 - 39

    Enter module codes and students enrolled:CS1010 292CS1234 178CS1010E 358CS2102 260

    IS1103 215IS2104 93IS1112 100GEK1511 83IT2002 51MA1101S 123

    Sorted by student enrolment:IT2002 51GEK1511 83IS2104 93

    IS1112 100MA1101S 123CS1234 178IS1103 215CS2102 260CS1010 292CS1010E 358

  • 7/27/2019 Week11.pptx

    40/42

    Summary for Today

    Searching algorithms Linear (sequential) search

    Binary search

    Basic sorting algorithms Selection sort

    Bubble sort

    CS1010 (AY2011/2 Semester 1) Week11 - 40

  • 7/27/2019 Week11.pptx

    41/42

    Announcements/Things-to-do

    Revise Chapter 6: Numeric Arrays Lesson 6.7

    PE2 this Saturday

    29 October 2011, Saturday

    See module website for details

    Next weeks lecture:

    Structures: Chapter 8

    Lessons 8.1 8.5

    CS1010 (AY2011/2 Semester 1) Week11 - 41

  • 7/27/2019 Week11.pptx

    42/42

    End of File


Recommended