Top Banner

of 26

Serching + Sorting

Apr 08, 2018

Download

Documents

Vicky Butt
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/7/2019 Serching + Sorting

    1/26

    Data Structures & Algorithms

    Lecture # 6

  • 8/7/2019 Serching + Sorting

    2/26

    Searching Algorithms

    Sequential Search

    Binary Search

    Sorting Algorithms

    Bubble Sort

    Selection Sort

    Insertion Sort Merge Sort

    Today Topics

  • 8/7/2019 Serching + Sorting

    3/26

    What is Searching?

    The process of finding a specific data item or record froma list is called searching.

    Main task is efficient storage of data to facilitate fast

    searching.

    Other operations like inserting and deletion aredependent on this operation. For example, to delete a data item from a list, its position is first

    located in the list and then the deletion operation is performed.

  • 8/7/2019 Serching + Sorting

    4/26

    What is Searching?

    If specified data is found, Search is successful

    Search operation is terminates when it is successful

    If specified data is not found, search is unsuccessful

    Commonly used searching methods are:

    Sequential Search Binary Search

  • 8/7/2019 Serching + Sorting

    5/26

    Sequential Search It is simple and straightforward technique to

    search a specified item in an unordered list

    Specified value is searched in the listsequentially i.e. starting from the first elementto the last element in the list in a sequence.

    When the required value is found, searchoperation stops.

  • 8/7/2019 Serching + Sorting

    6/26

    Sequential Search It is slow process

    It is used for small amounts of data

    It is not recommended for large

    amount of data

  • 8/7/2019 Serching + Sorting

    7/26

    Algorithm Sequential SearchSearchItem (Item, ABC, LOC)

    Find a value ITEM from an array ABC consisting of Nelements

    1. start

    2. Set Loc = -1

    3. Repeat step-4 FOR I = 1 to N

    4. If Item = ABC[I] then

    Loc = IPrint Item found at location , Loc

    End if

  • 8/7/2019 Serching + Sorting

    8/26

    Algorithm Sequential Search5. If Loc = -1 then

    Print Item not Found

    End if

    6. Exit

  • 8/7/2019 Serching + Sorting

    9/26

    void main(){int arr[25];int n,item;coutn;for(int i=0;I

  • 8/7/2019 Serching + Sorting

    10/26

    int main(void){

    int array[10]cout> key;

    int flag = 0; // set flag to off

    for(int i=0; i

  • 8/7/2019 Serching + Sorting

    11/26

    Algorithm Sequential SearchReplaceSearchItem (Value1, Value2)

    Find a Value1 from an array ABC consisting of Nelements and replace with Value2

    1. start

    2. Set Loc = -1

    3. Repeat step-4 FOR I = 1 to N

    4. If Item = ABC[I] then

    ABC [I] = Value2Loc = I

    Print Item modified at location , Loc

    End if

  • 8/7/2019 Serching + Sorting

    12/26

    Algorithm Sequential Search5. If Loc = -1 then

    Print Item not Found

    End if

    6. Exit

  • 8/7/2019 Serching + Sorting

    13/26

    Algorithm Sequential SearchFindLargeValue ()

    Find and print the largest value in an array ABCconsisting of N elements

    1. start

    2. Set Max = ABC[0]

    3. Repeat step-4 FOR I = 1 to N

    4. If Max < ABC[I] then

    Max = ABC [I]End if

    5. Print Max

    6. Exit

  • 8/7/2019 Serching + Sorting

    14/26

    (Largest Element in Array) A nonempty arrayDATA with N numerical values is given. Thisalgorithm finds the location LOC and the value

    MAX of the largest element ofDATA. Thevariable K is used as a counter.

    Step 1. [Initialize] Set K:= 1, LOC := 1 and

    MAX:= DATA[K].

    Step 2. [increment counter]. Set K:= K + 1. Step 3. [Test counter] if K > N , than

    write: LOC, MAX, and exit.

    Step 4. [compare and update] if MAX < DATA[K],

    than,

    Set LOC:=K and MAX:= DATA[K]. Step 5. [Repeat loop]. Go to step 2.

  • 8/7/2019 Serching + Sorting

    15/26

    Binary Search It is a more efficient technique to

    search a specific item from list of

    items.

    It is mostly used for relatively largelists or table of records that aresorted in ascending or descendingorder

  • 8/7/2019 Serching + Sorting

    16/26

    Binary Search It begins by searching the required value from the

    middle of the list.

    If the required value is in the middle of the list thensearch process terminates at that point.

    If the list is sorted in ascending order and the requiredvalue is greater than the value at middle, the controlgoes to the higher value to search.

    If the list is sorted in ascending order and the requiredvalue is lesser than the value at middle, the control goesto the lesser value to search.

  • 8/7/2019 Serching + Sorting

    17/26

    Algorithm Binary SearchBinarySearch(Item)

    To find a value in an array ABC consisting of N elementssorted in ascending order

    1. Start, LOC:=NULL

    2. Set BEG:= LB, END:=UB and MID:= INT((BEG+END)/2)

    3. Repeat step 3 and 4 while ABC[MID] != ITEM

  • 8/7/2019 Serching + Sorting

    18/26

    Algorithm Binary Search5. If Item < ABC [Mid] then

    SET END:= MID-1.

    else:

    SET BEG: MID +1.

    [end of if structure]

    6. Set MID:= INT ((BEG+END)/2).

    [end of step 2 loop]

    7. If ABC[MID]=ITEM, then

    SET LOC:=MID.

    else:

    SET LOC: NULL

    8. [end of if structure]

    9.exit

  • 8/7/2019 Serching + Sorting

    19/26

    performance The advantage of a binary search over a linear search is

    outstanding for large numbers.

    For an array of a million elements, binary search, O(logN), will find the target element with a worst case of only20 comparisons.

    Linear search, O(N), on average will take 500,000comparisons to find the element.

  • 8/7/2019 Serching + Sorting

    20/26

    // binary search of an integer array, this search isefficient for large arrays

    int main()

    {

    int a[20] = {0};

    int n, i, j, temp;

    int *beg, *end, *mid, target;

    printf(" enter the total integers you want to enter (make it lessthen 20):\n");

    scanf("%d", &n);

    if (n >= 20) return 0;

    printf(" enter the integer array elements:\n" );

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

    {

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

    }

  • 8/7/2019 Serching + Sorting

    21/26

    // sort the loaded array, a must for binary search!

    // you can apply qsort or other algorithms here

    for(i = 0; i < n-1; i++)

    {

    for(j = 0; j < n-i-1; j++)

    {

    if (a[j+1] < a[j])

    {

    temp = a[j];

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

    a[j+1] = temp;

    }

    }

    } printf(" the sorted numbers are:");

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

    {

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

    }

  • 8/7/2019 Serching + Sorting

    22/26

    point to beginning and end of the array

    beg = &a[0]; end = &a[n]; // use n = one element past the loaded

    array!

    printf("\n beg points to address %d and end to %d",beg,end); // test

    mid should point somewhere in the middle ofthese addresses

    mid = beg += n/2;

    printf("\n mid points to address %d", mid); // test

    printf("\n enter the number to be searched:");

    scanf("%d",&target);

  • 8/7/2019 Serching + Sorting

    23/26

    binary search, there is an AND in the middle of while()!!!

    while((beg

  • 8/7/2019 Serching + Sorting

    24/26

    // did you find the target?

    if (*mid == target)

    {

    printf("\n %d found!", target);

    }

    else

    {

    printf("\n %d not found!", target);

    }

    getchar(); // trap enter

    getchar(); // wait

    return 0;

    }

  • 8/7/2019 Serching + Sorting

    25/26

    Sorting The process of arranging data in a specified

    order according to a given criteria is calledsorting.

    The numeric type data may be arranged eitherin ascending or in descending order.

    Character type data may be arranged inalphabetical order.

  • 8/7/2019 Serching + Sorting

    26/26

    Sorting Sorting methods can be divided into two types basedupon the complexity of their algorithms.

    One type of sorting algorithms includes

    Bubble Sort

    Insertion Sort

    Selection Sort

    Other type consists is

    Merge Sort

    Choice of a method depends upon the type and size ofdata to be sorted.