Lab11 Searching and Sorting

download Lab11 Searching and Sorting

of 4

Transcript of Lab11 Searching and Sorting

  • 8/10/2019 Lab11 Searching and Sorting

    1/4

    ICS 103 Computer Programming in C

    Lab # 11 Searching & Sorting

    Objective: Learn how to search arrays. Learn how to sort an array.

    Searching:

    A common problem encountered in programming is the need to search an array in order

    to find the location of the desired value. Suppose you are looking for a student with ID

    995!" in a list of ID#s$ then you search for this ID in the list from the beginning till youfind it or reach the end of the array and the ID is not there.

    %he searching algorithms we will use are the linear search and the binary search.

    Linear search:

    %his is a general search strategy. &ere$ we search for the element from the beginning of

    the array. If we find the target element in the array$ we stop searching and return the

    position of the element in the array' otherwise we continue till the end of the array. If wereach the end of the array and did not find the target$ the search returns () as an

    indication that it is not in the array.

    %his is the code of linear search*int linear_search(int a[], int n, int key){

    int i;for(i = 0; i < n; i++){

    if(key == a[i])return i ;

    }return -1;

    }

    Binary search:

    If the array we are searching is sorted and the sorting order is known$ then we can use abetter strategy than linear search to look for the target element. %he binary search makes

    use of the fact that the elements are ordered and does not scan the whole array. %he steps

    of binary search$ for an array that is sorted in increasing order$ are*

    ). if the middle element m is e+ual to the target$ then return its position and we are

    done.. else*

    a. if the target , middle element$ then search the first half of the array and

    repeat step ).

    b. if the target - middle$ then search second half of the array and repeatstep).

    c. if the array is ehausted$ then the target is not in the array and the search

    returns /).

  • 8/10/2019 Lab11 Searching and Sorting

    2/4

    %he following figure is an eample of binary search*

    In the implementation$ we use two variables 0firstand last1 that indicate the start

    and the end of the sub/array being searched. %hrough firstand lastwe can get the

    middle element and specify the boundary of the array for the net pass if we do not find

    the target in the current pass.%his is code for binary search*

    int inary_search(int ![], int first, int last, int key){

    int "i##le;if(last

    }

  • 8/10/2019 Lab11 Searching and Sorting

    3/4

    Sorting:Another interesting problem in programming is to sort the elements of an array in

    increasing or decreasing order. %he use of sorted arrays is very obvious. &aving sortedrecords makes it easier to locate a particular element. Also if you want the output of these

    records 0for eample students ID#s along with their grades1 to be displayed on screen

    sorted$ then using a sort strategy becomes necessary.In this lab we will discuss one sorting algorithm* Selection Sort.

    Selection Sort:

    Selection sort is a simple straightforward algorithm to sort an array of numbers and itworks as follows. 2irst$ we find the minimum element and store it in A3!4. et$ we find

    the minimum of the remaining elements and store it in A3)4. 6e continue this way until

    the second largest element is stored in A3si7e ( )4. %he following figure eplains theprocess of selection sort*

    %his is the code for selection sort*&oi# selection_sort(int ![], int si'e){ int k,,"inos,te"; for (k=0; k < si'e - 1; k++){ "inos = k; $$ initiali'e location of "in &alue $$ *o o&er the ele"ents to fin# location of "ini"u" &alue for( = k+1; < si'e; ++){

    if(![] < !["inos])

    "inos = ; } $$ rin* "ini"u" &alue hich is at "inos at in#e! k

    te" = !["inos]; !["inos] = ![k]; ![k] = te"; }}

  • 8/10/2019 Lab11 Searching and Sorting

    4/4

    Lab Work:

    uestion 1Write a program that will generate an array of 10 integernumbers from 0 to 30 using the function ran#()present in

    st#li.hlibrary. The function ran#()will return a pseudorandom number from 0to /2_34which is the maximum valuein the inttype. In order to limit your number from 0to n,mae the returned &alueto be &alue 5 (n+1). !y usingran#()only, your program will generate the same se"uenceduring each execution. To avoid this problem insert thefollowing statement at the beginning of your main function#sran#(ti"e(677));$s the user to enter a tar*et &alue%between 0 and 30& tochec if it is present in the array by using linear search.If present your program should display its position,

    otherwise print a message saying that the value is not inthe array.

    uestion %The binary search function shown above is recursive' rewritethe same function using a loop instead of recursion. Totest your iterative binary search function repeat 8uestion 1by using binary search instead of linear search.ote you shoul# sort your array efore searchin*

    uestion 9Write a logical function sorte#_inc that receives an arrayof int values and nrepresenting the number of values. Thefunction will return 1if the array values are sorted inincreasing order, 0otherwise.Write a similar function sorte#_#ec to chec if the arrayvalues are sorted in decreasing order or not.Write another version of selection(sort' selection_sort_#ecso that it sorts the array in decreasing order.Write a program that will generate an array x1 of )0 randominteger values from 1 to 100. *se similar procedure as in"uestion 1, but now the lower part is not 0.+rint the array on the screen.

    hec if it is by chance sorted in increasing or decreasingorder by using the functions sorted(inc and sorted(dec. Ifthe array is not sorted %this is what is expected&, sort thearray by using the original and modified selection sortfunctions i.e. increasing and decreasing. +rint your arrayafter each sorting.