searchingandsorting

Upload: tpabbas

Post on 09-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 SearchingAndSorting

    1/78

    1

    MMMMMMMMOOOOOOOODDDDDDDDUUUUUUUULLLLLLLLEEEEEEEE VVVVVVVV SSSSSSSSEEEEEEEEAAAAAAAARRRRRRRRCCCCCCCCHHHHHHHHIIIIIIIINNNNNNNNGGGGGGGG AAAAAAAANNNNNNNNDDDDDDDD SSSSSSSSOOOOOOOORRRRRRRRTTTTTTTTIIIIIIIINNNNNNNNGGGGGGGG

    11111111........ MMMMMMMMOOOOOOOODDDDDDDDUUUUUUUULLLLLLLLEEEEEEEE IIIIIIIINNNNNNNNTTTTTTTTRRRRRRRROOOOOOOODDDDDDDDUUUUUUUUCCCCCCCCTTTTTTTTIIIIIIIIOOOOOOOONNNNNNNN

    The students are already familiar with various types of linear and non-linear data

    structures and their practical applications. Students may have already felt the need

    for searching and sorting the data stored using various data structures. Catering to

    this need, this chapter makes the students familiar with various methods for sorting

    and searching. The merits and demerits of each sorting and searching method are

    to be brought to the attention of the students. The concepts included in this chapter

    may be transacted through the learning strategies such as discussion, seminar, as-

    signment, lab work, illustration, output prediction, laboratory demonstration and

    showcasing. Also, the teachers can utilize activities like error correction and

    program development for evaluation.

    22222222........ CCCCCCCCOOOOOOOONNNNNNNNTTTTTTTTEEEEEEEENNNNNNNNTTTTTTTT DDDDDDDDEEEEEEEETTTTTTTTAAAAAAAAIIIIIIIILLLLLLLLSSSSSSSS

    Searching: Linear Search Binary Search Comparison of searching algorithms

    Sorting: Insertion sort Bubble sort Selection sort Heap sort Quick sort

    Merge sort Comparison of sorting algorithms.

    33333333........ UUUUUUUUNNNNNNNNIIIIIIIITTTTTTTT OOOOOOOOUUUUUUUUTTTTTTTTCCCCCCCCOOOOOOOOMMMMMMMMEEEEEEEE

    Familiarize and understand the need for searching and sorting in real lifecomputing applications.

    Integrate the features and facilities of various searching and sorting algorithmsto develop software for real life applications.

  • 8/7/2019 SearchingAndSorting

    2/78

    2

    44444444........ MMMMMMMMOOOOOOOODDDDDDDDUUUUUUUULLLLLLLLEEEEEEEE CCCCCCCCOOOOOOOOMMMMMMMMPPPPPPPPEEEEEEEETTTTTTTTEEEEEEEENNNNNNNNCCCCCCCCIIIIIIIIEEEEEEEESSSSSSSS

    1. Apply searching algorithms to real life problems2. Apply sorting algorithms to real life problems.

    55555555........ IIIIIIIINNNNNNNNSSSSSSSSTTTTTTTTRRRRRRRRUUUUUUUUCCCCCCCCTTTTTTTTIIIIIIIIOOOOOOOONNNNNNNNAAAAAAAALLLLLLLL OOOOOOOOBBBBBBBBJJJJJJJJEEEEEEEECCCCCCCCTTTTTTTTIIIIIIIIVVVVVVVVEEEEEEEESSSSSSSS

    C5: Understand, analyze for efficiency and implement search algorithms:

    Linear and Binary

    C5G1: Linear search

    C5G1S1: Basic concepts

    TI1: How does linear search works in a list implemented as an

    array?

    TI2: How does linear search works in a list implemented as a linked

    list?

    TI3: Demonstrate the working of linear search to find a given

    element in a list implemented as an array.

    TI4: Demonstrate the working of linear search to find a given

    element in a list implemented as linked list.

    TI5: Analyse best, worst and best case timing complexity of the

    linear search

    C5G1S2: Implementation

    TI1: Write a program to implement linear search algorithm for

    searching a number in a list of numbers maintained as an array.

    TI2: Write a program to implement linear search algorithm for

    searching a name in a list of name maintained as a 2-D array.

  • 8/7/2019 SearchingAndSorting

    3/78

    3

    TI3: Write a program to implement linear search algorithm for

    searching a number in a list of numbers maintained as a linked

    list.

    TI4: Write a program to implement linear search algorithm for

    searching a name in a list of name maintained as a linked list.

    C5G1: Binary search

    C5G1S1: Basic concepts

    TI1: How does Binary search works in a list implemented as an

    array?

    TI2: Can Binary search works in a list implemented as a linked list?

    Justify.

    TI3: Demonstrate the working of Binary search to find a given

    element in a list implemented as an array.

    TI4: Analyse best, worst and best case timing complexity of the

    Binary searchC5G1S2: Implementation

    TI1: Write a program to implement Binary search algorithm for

    searching a number in a list of numbers maintained as an array.

    TI2: Write a program to implement Binary search algorithm for

    searching a name in a list of name maintained as a 2-D array.

    TI3: Write a program to implement Binary search algorithm for

    searching a number in a list of numbers maintained as a linked

    list.

    C6: Understand, analyze for efficiency and implement sorting algorithms: Insertion

    sort, Bubble sort, Selection Sort, Quick sort, Heap sort and Merge sort

    C5G1: Insertion, Bubble, Selection, Quick, Heap and Merge sorts

  • 8/7/2019 SearchingAndSorting

    4/78

    4

    C5G1S1: Basic concepts of Insertion, Bubble, Selection, Quick, Heap

    and Merge sorts

    TI1: How Insertion, Bubble, Selection, Quick, Heap and Merge sorts

    work with a list of numbers maintained as an array?

    TI2: How Insertion, Bubble, Selection, Quick, Heap and Merge sorts

    work with a list of numbers maintained as a linked list?

    TI3: Demonstrate the working of Insertion, Bubble, Selection,

    Quick, Heap and Merge sorts with a list of numbers

    implemented as an array.

    TI4: Demonstrate the working of Insertion, Bubble, Selection,

    Quick, Heap and Merge sorts with a list of numbers

    implemented as a linked list.

    TI5: Analyse best, worst and best case timing complexity of

    Insertion, Bubble, Selection, Quick, Heap and Merge sorts.

    TI6: Compare the performance of the Insertion, Bubble, Selection,Quick, Heap and Merge sorts with each other.

    TI7: What are the merits and demerits of each of the Insertion,

    Bubble, Selection, Quick, Heap and Merge sorts?

    C5G1S1: Implementation

    TI1: Write a program to sort a list of numbers maintained as an array

    using Insertion, Bubble, Selection, Quick, Heap and Merge

    sorts

    TI2: Write a program to sort a list of names maintained as an array

    using Insertion, Bubble, Selection, Quick, Heap and Merge

    sorts

  • 8/7/2019 SearchingAndSorting

    5/78

    5

    TI3: Write a program to sort a list of student records maintained as

    an array of structures using Insertion, Bubble, Selection, Quick,

    Heap and Merge sorts

    TI4: Write a program to sort a list of numbers maintained as a linked

    list using Insertion, Bubble, Selection, Quick, Heap and Merge

    sorts

    TI5: Write a program to sort a list of names maintained as a linked

    list using Insertion, Bubble, Selection, Quick, Heap and Merge

    sorts

    TI6: Write a program to sort a list of student records maintained as a

    linked list of structures using Insertion, Bubble, Selection,

    Quick, Heap and Merge sorts

  • 8/7/2019 SearchingAndSorting

    6/78

    6

    CCCCCCCCOOOOOOOONNNNNNNNCCCCCCCCEEEEEEEEPPPPPPPPTTTTTTTT MMMMMMMMAAAAAAAAPPPPPPPP OOOOOOOOFFFFFFFF TTTTTTTTHHHHHHHHEEEEEEEE MMMMMMMMOOOOOOOODDDDDDDDUUUUUUUULLLLLLLLEEEEEEEE

    77777777........ IIIIIIIINNNNNNNNSSSSSSSSTTTTTTTTRRRRRRRRUUUUUUUUCCCCCCCCTTTTTTTTIIIIIIIIOOOOOOOONNNNNNNNAAAAAAAALLLLLLLL MMMMMMMMAAAAAAAATTTTTTTTEEEEEEEERRRRRRRRIIIIIIIIAAAAAAAALLLLLLLL

    77..11.. SSEEAARRCCHHIINNGG AANNDD SSOORRTTIINNGG

    Sorting and searching are fundamental operations in computer science. Sorting

    refers to the operation of arranging data in some given order, such as increasing or

    decreasing, with numerical data or alphabetically, with character data. Searching

  • 8/7/2019 SearchingAndSorting

    7/78

    7

    refers to the operation of finding the location of a given item in a collection of

    items.

    Sorting and searching frequently apply to files. A file is a collection of records,each record having one or more fields. The fields used to distinguish among the

    records are known as keys. Sorting the file Frefers to sorting Fwith respect to a

    particular primary key, and searching in Frefers to searching for the records with a

    given key value. The association between a record and its key may be simple or

    complex. In the simple form, the key is contained within the record at a specific

    offset from the start of the record. Such a record is called an internal key or an

    embedded key. In other cases, there is a separate table of keys that includes

    pointers to the records. Such keys are external.

    For every file there is at least one set of keys (possibly more) that is unique (that is,no two records have the same key value). Such a key is called primary key. For

    example, if the file is stored as an array, the index within the array of an element is

    a unique external key for that element. However, since any field of record can

    serve as the key in a particular application, key need not always be unique. For

    example, in a file of names and addresses if the name is used as the key for a

    particular search, it will probably not be unique, since there may be two records

    with the same name in a file. Such key is called a secondary key.

    There are many sorting and searching algorithms. The particular algorithm one

    chooses depends on the properties of the data, the location where it resides and theoperations one may perform on data. The data may be an array of records, a linked

    list, a tree or even a graph. Because different sorting and searching techniques may

    be suitable for different file organizations, a file is often designed with a specific

    search technique in mind. The file to be searched or sorted may be contained

    completely in memory, completely in auxiliary memory or it may be divided

    between the two. Sorting or searching in which the entire file is constantly in

    memory are called internal searching/sorting, whereas those in which most of the

    file is kept in auxiliary storage is external sorting/searching. We concentrate

    primarily on internal sorting and searching.

    77..22.. SSEEAARRCCHHIINNGG

    Searching is an operation which finds the location of a given element in a list.

    SupposeDATA is a list maintained in memory. Searching finds the location LOC

    in memory of some givenITEMof information or sends some message thatITEM

    does not belong to DATA. The search is said to be successful or unsuccessful

    depending on whether the ITEMdoes or does not belong to DATA. Here, we will

    discuss two standard searching methods linear and binary search.

  • 8/7/2019 SearchingAndSorting

    8/78

    8

    77..22.. 11.. LLIINNEEAARR SSEEAARRCCHH

    Linear search, also known as sequential search, is the simplest method of searching

    and is suitable for searching a set of data for a particular value. The search is

    applicable to list organized either as a sorted or unsorted array or linked list.

    Suppose a linear array data contains n elements, and suppose a specific item of

    information is given. We want either to find the location loc ofitem in the array

    data or to send some message to indicate that item does not appear in data. Linear

    search operates by checking every element of a list until a match is found. Linear

    search runs in O(n). If the data are distributed randomly, on average n/2

    comparisons will be needed. The best case is that the value is equal to the first

    element tested, in which case only 1 comparison is needed. The worst case is that

    the value is not in the list, in which case n comparisons are needed.

    An algorithm of the linear search is given below:

    int linear_search(int *data, int length, int value)

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

    if(data[i] == value) return 1 ;

    return 0 ;

    }

    The complete C++ implementation of linear search for a list of numbers

    maintained as an array is given below.//Program that searches for a given

    //number using linear search method in

    //an unsorted array

    #include

    #include

    #include

    const int MAX=50;

    class array

    { private:int arr[MAX]; int count;

    public:

    array()

    {count=0;}

    void add(int);

    void display(void);

    int search(int);

    };

    //adds a new element to the array

  • 8/7/2019 SearchingAndSorting

    9/78

    9

    void array::add(int item)

    { if(count

  • 8/7/2019 SearchingAndSorting

    10/78

    10

    while((c4));

    if(c==4)exit(1);

    clrscr();switch(c)

    { case 1:

    clrscr();

    coutn;

    cout

  • 8/7/2019 SearchingAndSorting

    11/78

    11

    #include

    class array

    { private :struct node

    { int data; node *link; }*start;

    public :

    array();

    ~array();

    void add(int);

    void display(void);

    int search(int);

    };

    //Initializes count of elements maintained //in the header nodearray::array()

    { start= new node;

    start->link=NULL;

    start->data=0;

    }

    //Deallocates memory

    array::~array()

    { node *current;

    while(start!=NULL){ current=start->link;

    delete current;

    start=current;

    }

    }

    //Insert element

    void array::add(int val)

    { node *current;

    start->data++;

    current = new node;

    current->link = start->link;

    current->data=val;

    start->link=current;

    }

    //Displays the contents of the list

    void array::display()

    { node *current;

    cout

  • 8/7/2019 SearchingAndSorting

    12/78

    12

  • 8/7/2019 SearchingAndSorting

    13/78

    13

    { case 1:

    clrscr();

    coutn;

    cout

  • 8/7/2019 SearchingAndSorting

    14/78

    14

    { private:

    char emplname[20];

    char emplcode[6];char desig[20];

    int salary;

    public:

    void add(void);

    void display(void);

    void search(void);

    };

    void employee::add(void)

    { char choice;

    fstream file("employee.dat", ios::app|ios::out);if(file.fail())

    { cout

  • 8/7/2019 SearchingAndSorting

    15/78

    15

    file

  • 8/7/2019 SearchingAndSorting

    16/78

    16

    cout.width(6);

    cout

  • 8/7/2019 SearchingAndSorting

    17/78

    17

    gotoxy(10,8);

    }

    }file.close();

    if(flag==0)

    { gotoxy(20,5);

    cout

  • 8/7/2019 SearchingAndSorting

    18/78

    18

    { gotoxy(20,17);

    coutc; gotoxy(43,17); cout

  • 8/7/2019 SearchingAndSorting

    19/78

    19

    Binary search executes in O(log n) time. It is considerably faster than a linear

    search. It can be implemented using recursion or iteration, as shown below,

    although in many languages it is more elegantly expressed recursively.Here is simple recursive pseudo code which determines the index of a given value

    in a sorted list arrbetween indices lowerand upper:

    function BinarySearch(arr, item, lower, upper)

    if upper < lower

    return not found

    mid = floor((lower+upper)/2)

    if arr[mid] = item

    return mid

    if item < arr[mid]return BinarySearch(arr, item, lower, mid-1)

    else

    return BinarySearch(a, item, mid+1, upper)

    This recursive algorithm can be rewritten using loops, as shown below:

    function BinarySearch(arr, item, lower, upper)

    while lower upper

    mid = floor((lower+upper)/2)

    if arr[mid] = item

    return midif item < arr[mid]

    upper = mid-1

    else

    lower = mid+1

    return not found

    In both cases, the algorithm terminates because on each recursive call or iteration,

    the range of indexes upper minus lower always gets smaller, and so must

    eventually become negative.

    Examples: An example of binary search in action is a simple guessing game in

    which a player has to guess a positive integer selected by another player between 1

    and N, using only questions answered with yes or no. Supposing Nis 16 and the

    number 11 is selected, the game might proceed as follows.

    Is the number greater than 8? (Yes) Is the number greater than 12? (No) Is the number greater than 10? (Yes) Is the number greater than 11? (No)

  • 8/7/2019 SearchingAndSorting

    20/78

    20

    Therefore, the number must be 11. At each step, we choose a number right in the

    middle of the range of possible values for the number. For example, once we know

    the number is greater than 8, but less than or equal to 12, we know to choose anumber in the middle of the range [9, 12] (either 10 or 11 will do). At most log2

    N questions are required to determine the number, since each question halves thesearch space. Note that one less question (iteration) is required than for the general

    algorithm, since the number is constrained to a particular range.

    Even if the number we're guessing can be arbitrarily large, in which case there is

    no upper boundN, we can still find the number in at most 2 log2 k steps (where kis the (unknown) selected number) by first finding an upper bound by repeated

    doubling. For example, if the number were 11, we could use the following

    sequence of guesses to find it: Is the number greater than 1? (Yes) Is the number greater than 2? (Yes) Is the number greater than 4? (Yes) Is the number greater than 8? (Yes) Is the number greater than 16? (No, N=16, proceed as above) (We know the number greater than 8 ) Is the number greater than 12? (No) Is the number greater than 10? (Yes) Is the number greater than 11? (No)The following program illustrates the iterative binary search applied to a list

    maintained as a sorted array.

    //Program that searches for a given

    //number using binary search method in a

    //sorted array

    #include

    #include

    #includeconst int MAX=50;

    class array

    { private:

    int arr[MAX]; int count;

    public:

    array()

    {count=0;}

    void add(int);

    void display(void);

  • 8/7/2019 SearchingAndSorting

    21/78

    21

    int search(int);

    void sort(void);

    };//adds a new element to the array

    void array::add(int item)

    { if(count

  • 8/7/2019 SearchingAndSorting

    22/78

    22

    array binary;

    while(1)

    { clrscr();gotoxy(20,10);

    cout

  • 8/7/2019 SearchingAndSorting

    23/78

    23

    cin>>element;

    i=binary.search(element);

    if(i==-1)cout

  • 8/7/2019 SearchingAndSorting

    24/78

    24

    arr[count++]=item;

    else

    cout

  • 8/7/2019 SearchingAndSorting

    25/78

    25

    gotoxy(20,12);

    cout

  • 8/7/2019 SearchingAndSorting

    26/78

    26

    break;

    }

    cout

  • 8/7/2019 SearchingAndSorting

    27/78

    27

    }

    do

    { clrscr();gotoxy(10,3);

    cout

  • 8/7/2019 SearchingAndSorting

    28/78

    28

  • 8/7/2019 SearchingAndSorting

    29/78

    29

    for(int j=0;j arr[j+1].emplcode)

    { empl t=arr[j]; arr[j]=arr[j+1];arr[j+1]=t;

    }

    return;

    }

    void employee::search(void)

    { char no[10],choice;

    fstream file("employee.dat",ios::in);

    if(file.fail())

    { coutarr[i].salary;

    file.ignore(1);

    if(file.eof()) break;i++;

    }

    file.close();

    emplcount=i;

    do

    { clrscr(); gotoxy(10,1);

    coutno;

    int flag=0;

    for(i=0;i

  • 8/7/2019 SearchingAndSorting

    30/78

    30

    cout

  • 8/7/2019 SearchingAndSorting

    31/78

    31

    cout

  • 8/7/2019 SearchingAndSorting

    32/78

    32

    Computational complexity (worst, average and best behaviour) in terms of thesize of the list (n). For typical sorting algorithms good behaviour is O(n

    log n) and bad behaviour is O(n2). Ideal behaviour for a sort is O(n). Sort

    algorithms which only use an abstract key comparison operation always need at

    least O(n

    log n) comparisons on average. Memory usage (and use of other computer resources). In particular, some

    sorting algorithms are "in place", such that little memory is needed beyond the

    items being sorted, while others need to create auxiliary locations for data to be

    temporarily stored.

    Stability: Stable sorting algorithms maintain the relative order of records withequal keys (i.e. values). That is, a sorting algorithm is stable if whenever there

    are two recordsR and S with the same key and withR appearing before S in the

    original list,R will appear before S in the sorted list.

    Whether or not they are a comparison sort. A comparison sort examines thedata only by comparing two elements with a comparison operator.

    General method: insertion, exchange, selection, merging, etc. Exchange sortsinclude bubble sort and quick sort. Selection sorts include heap sort.

    When equal elements are indistinguishable, such as with integers, stability is not an

    issue. However, assume that the following pairs of numbers are to be sorted by

    their first coordinate:

    (4, 1) (3, 1) (3, 7) (5, 6)

    In this case, two different results are possible, one which maintains the relative

    order of records with equal keys, and one which does not:

    (3, 1) (3, 7) (4, 1) (5, 6) (Order maintained)

    (3, 7) (3, 1) (4, 1) (5, 6) (Order changed)

    Unstable sorting algorithms may change the relative order of records with equal

    keys, but stable sorting algorithms never do so. Unstable sorting algorithms can be

    specially implemented to be stable. One way of doing this is to artificially extend

    the key comparison, so that comparisons between two objects with otherwise equal

    keys are decided using the order of the entries in the original data order as a tie-

    breaker. Remembering this order, however, often involves an additional space cost.

  • 8/7/2019 SearchingAndSorting

    33/78

    33

    Some of commonly used internal sorting methods are discussed here.

    77

    .

    .33

    .

    .11

    .

    .IINN

    SS

    EE

    RR

    TT

    IIOO

    NN

    SS

    OO

    RR

    TT

    Insertion sort is a simple comparison type sorting algorithm that is relatively

    efficient for small lists and mostly-sorted lists, and often is used as part of more

    sophisticated algorithms. It works by taking elements from the list one by one and

    inserting them in their correct position into a new sorted list.

    In abstract terms, each iteration of the insertion sort removes an element from the

    input data, inserting it at the correct position in the already sorted list, until no

    elements are left in the input. The choice of which element to remove from the

    input is arbitrary and can be made using almost any choice algorithm. Sorting is

    typically done in-place. The result array after kiterations contains the first kentriesof the input array and is sorted. In each step, the first remaining entry of the input

    is removed, inserted into the result at the right position.

    To save memory, most implementations use an in-place sort that works by sharing

    the arrays space for storing both the sorted new list and the remaining unsorted

    elements. But insertion is expensive, requiring shifting all following elements over

    by one.

    Insertion sort is much less efficient on large lists than the more advanced

    algorithms such as quick sort, heap sort, or merge sort, but it has various

    advantages:

    Simple to implement Efficient on (quite) small data sets Efficient on data sets which are already substantially sorted More efficient in practice than most other simple O(n2) algorithms such as

    selection sort or bubble sort: the average time is n2/4 and it is linear in the best

    case

    Stable (does not change the relative order of elements with equal keys) In-place (only requires a constant amount O(1) of extra memory space) It is an online algorithm, in that it can sort a list as it receives it. (An online

    algorithm is one that can process its input piece-by-piece, without having the

    entire input available from the start. In contrast, an offline algorithm is given the

    whole problem data from the beginning and is required to output an answer,

    which solves the problem at hand. For example, selection sort requires that the

    entire list is given before it can sort it.)

  • 8/7/2019 SearchingAndSorting

    34/78

    34

    Example:The first iteration starts with comparison of 1st

    element with 0th

    element.

    In the second iteration 2nd

    element is compared with the 0th

    and 1st

    elements. In

    general, each iteration compares an element with all elements before it. Duringcomparison if it is found that the elements in question can be inserted at a suitable

    position then space is created for it by shifting the other elements one position to

    the right and inserting the element at the suitable position. This procedure is

    repeated for all the elements in the array. This procedure is illustrated below for an

    array that contains 5 elements.

    In the first iteration, the 1st

    element 17 is compared with the 0th

    element 25. Since

    17 is smaller than 25, 17 is inserted at 0th

    place. The 0th

    element 25 is shifted one

    position to the right. In the second iteration, the 2nd

    element and 0th

    element 17 are

    compared. Since 31 is greater than 17, nothing is done. Then the 2

    nd

    element 31 iscompared with the 1st

    element 25. Again no action is taken as 25 is less than 31. In

    the third iteration, the 3rd element 13 is compared with the 0th element 17. Since, 13

    is smaller than 17, 13 is inserted at 0th

    place in the array and all the elements from

    0th

    till 2nd

    position are shifted to right by one position. In the fourth iteration, the 4th

    element 2 is compared with the 0th

    element. Since 2 is smaller than 13, the 4th

    element is inserted at the 0th

    place in the array and all the elements from 0th

    till 3rd

    are shifted right by one position. As a result, the array now becomes a sorted array.

    Good and Bad Input Cases: In the best case of an already sorted array, thisimplementation of insertion sort takes O(n) time: in each iteration, the first

    remaining element of the input is only compared with the last element of the result.

    It takes O(n2) time in the average and worst cases, which makes it impractical for

    sorting large numbers of elements. However, insertion sort's inner loop is very fast,

    which often makes it one of the fastest algorithms for sorting small numbers of

    elements, typically less than 10 or so.

    Comparisons to Other Sorts: Insertion sort is very similar to bubble sort. In bubble

    sort, after kpasses through the array, the k largest elements have bubbled to the

    25 31 1325 17 31 13 2 17 25 31 13 2

    17 25 31 13 2 13 17 25 31 2

    13 17 25 31 2

    13 17 25 31 2

    2 13 17 25 31

    12 13 17 25 31

    12 13 17 25 31

    First Iteration Second Iteration Third Iteration Fourth Iteration

    Steps in the Insertion

  • 8/7/2019 SearchingAndSorting

    35/78

    35

    top. (Or the ksmallest elements have bubbled to the bottom, depending on which

    way you do it.) In insertion sort, after kpasses through the array, you have a run of

    ksorted elements at the bottom of the array. Each pass inserts another element intothe sorted run. So with bubble sort, each pass takes less time than the previous one,

    but with insertion sort, each pass may take more time than the previous one.

    Algorithm: The formal algorithm of insertion sort is given below:

    void insertSort(int a[], int length)

    { int i, j, value;

    for(i = 1; i < length; i++)

    { value = a[i];

    for (j = i - 1; j >= 0 && a[j] > value; j--)

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

    }

    }

    The following program illustrates the C++ implementation of the insertion sort on

    a list of numbers maintained as an array.

    //Program that implements insertion sort on a list of numbers maintained as

    //an array of numbers

    #include

    #include#include

    const int MAX=50;

    class array

    { private:

    int arr[MAX];

    int count;

    public:

    array()

    {count=0;}void add(int);

    void display(void);

    void sort(void);

    };

    //adds a new element to the array

    void array::add(int item)

    { if(count

  • 8/7/2019 SearchingAndSorting

    36/78

    36

    else

    cout

  • 8/7/2019 SearchingAndSorting

    37/78

    37

    cin>>c; gotoxy(43,17); cout

  • 8/7/2019 SearchingAndSorting

    38/78

    38

    { int data; node *link;}*start;

    public :

    array();~array();

    void add(int);

    void display(void);

    void sort(void);

    };

    // initializes data member

    array::array()

    { start= new node;

    start->link=NULL; start->data=0;

    }// deallocates memory

    array::~array()

    { node *current;

    while(start!=NULL)

    { current=start->link;

    delete current; start=current;

    }

    }

    // insert elementvoid array::add(int val)

    { node *current;

    start->data++;

    current = new node;

    current->link = start->link;

    current->data=val;

    start->link=current;

    }

    // displays the contents of the list

    void array::display()

    { node *current;

    cout

  • 8/7/2019 SearchingAndSorting

    39/78

    39

    { node *t1,*t2,*old1,*old2;

    old1=start->link;

    for(t1=start->link->link; t1!=NULL; t1=t1->link){ old2=start;

    for(t2=start->link;t2!=t1;t2=t2->link)

    { if(t2->data>t1->data)

    { old1->link=t1->link;

    old2->link=t1; t1->link=t2;

    break;

    }

    old2=t2;

    }

    old1=t1;}

    return;

    }

    void main()

    { int element,n,i,c;

    array Insertion;

    while(1)

    { clrscr(); gotoxy(20,10);

    cout

  • 8/7/2019 SearchingAndSorting

    40/78

    40

    { case 1:

    clrscr();

    coutn;

    cout

  • 8/7/2019 SearchingAndSorting

    41/78

    41

    { char emplname[20];

    int emplcode;

    char desig[20];int salary;

    }arr[MAX];

    int emplcount;

    public:

    employee()

    { emplcount=0;}

    void add(void);

    void display(void);

    void search(void);

    void sort(void);};

    void employee::add(void)

    { char choice;

    fstream file("employee.dat",ios::out);

    if(file.fail())

    { cout

  • 8/7/2019 SearchingAndSorting

    42/78

    42

    cin.ignore(1);

    cin.getline(arr[emplcount].desig,20);

    gotoxy(27,9);cin>>arr[emplcount].salary;

    cin.ignore(1);

    do

    { gotoxy(20,12);

    cout

  • 8/7/2019 SearchingAndSorting

    43/78

    43

    }

    emplcount=i;

    cout

  • 8/7/2019 SearchingAndSorting

    44/78

    44

    while (1)

    { clrscr(); gotoxy(20,10);

    cout

  • 8/7/2019 SearchingAndSorting

    45/78

    45

    sorted. The algorithm gets its name from the way the larger values "bubble" to the

    end of the list while smaller values "sink" towards the beginning of the list via the

    swaps. Because it only uses comparisons to read elements, it is a comparison sort.In more detail, the bubble sort algorithm works as follows:

    1. Compare adjacent elements. If the first is greater than the second, swap them.2. Do this for each pair of adjacent elements, starting with the first two and ending

    with the last two. At this point the last element should be the greatest.

    3. Repeat the steps for all elements except the last one.4. Keep repeating for one fewer elements each time, until no more pairs to

    compare. (Alternatively, keep repeating until no swaps are needed.)

    Example: The first iteration of this algorithm begins with comparing 0th

    element

    with the 1st

    element. If it is found to be greater than the 1st

    element, then they are

    interchanged. Next, the 1st element is compared with the 2nd element, if it is found

    to be greater, then they are interchanged. In the same way all the elements

    (excluding the last) are compared with their next element and are interchanged, if

    required. On completing the first iteration, the largest element gets placed at the

    last position. Similarly, in the second iteration, comparisons are made till the last

    but one element and this time the second largest element gets placed at the second

    last position in the list. As a result, after all iterations, the list becomes a sorted list.

    This procedure is illustrated below for an array that contains 5 elements. In the first

    iteration, 0th

    element 25 is compared with the first element 17 and since 25 is

    greater than 17, they are interchanged. Now, the 1st

    element 25 is compared with

    2nd element 31. But 25 being less than 31, they are not interchanged. This process

    is repeated until (n-2)nd

    element is compared with the (n-1)th

    element. During the

    comparison, if (n-2)nd

    element is found to be greater than (n-1)th

    element, then they

    are interchanged. At the end of the first iteration, the (n-1)th

    element holds the

    largest number. Now the second iteration starts with the 0th

    element 17. The above

    process of comparison and interchanging is repeated but this time the last

    comparison is made between (n-3)rd

    and (n-2)nd

    elements. If there are n elements,then (n-1) iterations need to be performed.

    25 17 31 13 2

    First Iteration Second Iteration Third Iteration Fourth Iteration

    Steps in the Bubble Sort

    17 25 31 13 2

    17 25 31 13 2

    17 25 13 31 2

    17 25 13 2 31

    17 25 13 2 31

    17 25 13 2 31

    17 13 25 2 31

    17 13 2 25 31

    17 13 2 25 31

    13 17 2 25 31

    13 17 2 25 31

    13 17 2 25 31

    13 17 2 25 31

  • 8/7/2019 SearchingAndSorting

    46/78

    46

    Algorithm: The formal algorithm of bubble sort is given below

    void BubbleSort(int arr[], int n)

    { int i, j, temp;for (i = 0; i arr[j+1])

    { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; }

    }

    Performance: The bubble sort is generally considered to be the most inefficient

    sorting algorithm in common usage. Under best-case conditions (the list is already

    sorted), the bubble sort can approach a constant O(n) level of complexity. In

    general-case, bubble sort needs O(n2) comparisons to sort n items and can sort in-place. Although the algorithm is one of the simplest sorting algorithms to

    understand and implement, it is too inefficient for use on lists having more than a

    few elements. Even among simple O(n2) sorting algorithms, algorithms like

    insertion, selection and shell sorts are considerably more efficient.

    Bubble sort is asymptotically equivalent in running time to insertion sort in the

    worst case, but the two algorithms differ greatly in the number of swaps necessary.

    Insertion sort needs only O(n) operations if the list is already sorted, whereas nave

    implementations of bubble sort (like the pseudocode above) require O(n2)

    operations. (This can be reduced to O(n) if code is added to stop the outer loopwhen the inner loop performs no swaps.)

    The following program illustrates the C++ implementation of the bubble sort on a

    list of numbers maintained as an array.

    //Program that implements bubble sort

    //on a list of numbers maintained as

    //an array

    #include

    #include

    #includeconst int MAX=50;

    class array

    { private:

    int arr[MAX];

    int count;

    public:

    array()

    {count=0;}

  • 8/7/2019 SearchingAndSorting

    47/78

    47

    void add(int);

    void display(void);

    void sort(void);};

    //adds a new element to the array

    void array::add(int item)

    { if(count

  • 8/7/2019 SearchingAndSorting

    48/78

    48

    gotoxy(20,10);

    cout

  • 8/7/2019 SearchingAndSorting

    49/78

    49

    getch();

    }

    }The following program illustrates the C++ implementation of the bubble sort on a

    list of numbers maintained as a linked list.

    //Program that implements bubble sort on a list of numbers maintained

    //as a linked list

    #include

    #include

    #include

    class array

    { private :struct node

    { int data;

    node *link;

    }*start;

    public :

    array();

    ~array();

    void add(int);

    void display(void);void sort(void);

    };

    // initializes data member

    array::array()

    { start= new node;

    start->link=NULL;

    start->data=0;

    }

    // deallocates memory

    array::~array(){ node *current;

    while(start!=NULL)

    { current=start->link;

    delete current;

    start=current;

    }

    }

    // insert element

  • 8/7/2019 SearchingAndSorting

    50/78

    50

    void array::add(int val)

    { node *current;

    start->data++;current = new node;

    current->link = start->link;

    current->data=val; start->link=current;

    }

    // displays the contents of the list

    void array::display()

    { node *current;

    coutlink->data)

    { temp=t2->link;

    t2->link=t2->link->link;

    old->link=temp;

    temp->link=t2;

    flag=0;

    }

    old=t2;

    }

    if(flag) break;

    }

    return;

    }

    void main()

    { int element,n,i,c;

    array Bubble;

    while(1)

    { clrscr(); gotoxy(20,10);

  • 8/7/2019 SearchingAndSorting

    51/78

    51

    cout

  • 8/7/2019 SearchingAndSorting

    52/78

    52

    }

    }

    The following program illustrates the C++ implementation of the bubble sort on afile of records containing employee details. The file records are temporarily

    maintained as an array of records in memory for the purpose of displaying and

    sorting the file contents.

    //Applying Bubble sort to a file

    #include

    #include

    #include

    #include

    #include#include

    const int MAX=20;

    class employee

    { private:

    struct empl

    { char emplname[20];

    int emplcode;

    char desig[20];

    int salary;}arr[MAX];

    int emplcount;

    public:

    employee()

    { emplcount=0;}

    void add(void);

    void display(void);

    void search(void);

    void sort(void);

    };void employee::add(void)

    { char choice;

    fstream file("employee.dat",ios::out);

    if(file.fail())

    { cout

  • 8/7/2019 SearchingAndSorting

    53/78

    53

    { clrscr(); gotoxy(10,3);

    cout

  • 8/7/2019 SearchingAndSorting

    54/78

    54

    { int i=0;

    fstream file("employee.dat",ios::in);

    if(file.fail()){ coutarr[i].emplcode;

    file.ignore(1);

    file.getline(arr[i].desig,20);

    file>>arr[i].salary;file.ignore(1);

    if(file.eof()) break;

    i++;

    }

    emplcount=i;

    cout

  • 8/7/2019 SearchingAndSorting

    55/78

    55

    arr[j]=arr[j+1]; arr[j+1]=t;

    }

    fstream file("employee.dat",ios::out);if(file.fail())

    { cout

  • 8/7/2019 SearchingAndSorting

    56/78

    56

    case 3:

    e.sort();

    cout

  • 8/7/2019 SearchingAndSorting

    57/78

    57

    1st

    element and so on. This procedure is illustrated below for an array that contains

    5 elements:

    Performance: This algorithm, iterating through a list of n unsorted items, has a

    worst-case, average-case, and best-case run-time of O(n2), assuming that

    comparisons can be done in constant time. Among simple O(n2) algorithms, it is

    generally outperformed by insertion sort, but still tends to outperform contenders

    such as bubble sort. Selection sort is simple and easy to implement. But it is

    inefficient for large lists, so similar to the more efficient insertion sort should be

    used in its place. The selection sort is the unwanted stepchild of the O(n2) sorts. It

    yields a 60% performance improvement over the bubble sort, but the insertion sort

    is over twice as fast as the bubble sort and is just as easy to implement as the

    selection sort. In short, there really isn't any reason to use the selection sort - use

    the insertion sort instead.

    The following program illustrates the C++ implementation of the selection sort on

    a list of numbers maintained as an array.

    //Program that implements Selection sort on a list of numbers maintained as

    //an array

    #include#include

    #include

    const int MAX=50;

    class array

    { private:

    int arr[MAX]; int count;

    public:

    array()

    25 17 31 13 2

    First Iteration Second Iteration Third Iteration Fourth Iteration

    Steps in the Selection Sort

    17 25 31 13 2

    17 25 31 13 2

    13 25 31 17 2

    2 25 31 17 13

    2 25 31 17 13

    2 25 31 17 13

    2 17 31 25 13

    2 13 31 25 17

    2 13 31 25 17

    2 13 25 31 17

    2 13 17 31 25

    2 13 17 31 25

    2 13 17 31 25

  • 8/7/2019 SearchingAndSorting

    58/78

    58

    {count=0;}

    void add(int);

    void display(void);void sort(void);

    };

    //adds a new element to the array

    void array::add(int item)

    { if(count

  • 8/7/2019 SearchingAndSorting

    59/78

    59

    gotoxy(20,12);

    cout

  • 8/7/2019 SearchingAndSorting

    60/78

    60

    77..33..44.. QQUUIICCKK SSOORRTT

    As its name implies, quick sort is the fastest known sorting algorithms. Quick sort

    is a comparison sort and, in efficient implementations, is not a stable sort. It is anin-place, divide-and-conquer, massively recursive sort. It is essentially a faster in-

    place version of the merge sort. Let S be an array, and n the number of elements in

    the array to be sorted. Choose an element v (known as pivot element) from a

    specific position within the array (for example, v can be chosen as the first element

    so that v=S[0]). Suppose that the elements ofS are partitioned so that v is placed

    into positionj and the following conditions hold:

    1. Each of the elements in positions 0 throughj-1 is less than or equal to v.2. Each of the elements in positionsj+1 through n-1 is greater than or equal to v.Notice that if these two conditions hold for a particular v andj, v is thej

    thsmallest

    element ofS, so that v remains in position j when the array is completely sorted.

    The basic recursive algorithm for quick sort consists of the following four steps:

    1. If the number of elements in the array to be sorted (S) is 0 or 1, returnimmediately.

    2. Pick an element (v) in the array to serve as a "pivot" point. (Usually the left-most element in the array is used.)

    3. Split the array into two parts - one with elements larger than the pivot and theother with elements smaller than the pivot.

    4. Recursively repeat the algorithm for both halves of the original array.Suppose that an array arr consists of 10 elements. The quick sort algorithm works

    as follows:

    In the first iteration, we will place the 0 th element 11 at its final position anddivide the array. Here, 11 is the pivot element. To divide the array, two index

    variablesp and q, are taken. The indexes are initialized such a way that,p refers

    to the 1st

    element 2 and q refers to the (n-1)th

    element 3.

    The job of the index variable p is to search an element that is greater than thevalue at 0

    thlocation. So p is incremented by one till the value stored at p is

    greater than 0th

    element. In our case it is incremented till 13, as 13 is greater

    than 11.

    Similarly, q needs to search an element that is smaller than the 0th element. So qis decremented by 1 till the value stored at q is smaller than the value at 0

    th

    location. In our case q is not decremented because 3 is less than 11.

  • 8/7/2019 SearchingAndSorting

    61/78

    61

    Steps in Quick Sort

    When these elements are found, they are interchanged. Again from the currentpositionsp and q are incremented and decremented, respectively and exchanges

    are made appropriately if desired.

    The process ends whenever the index pointers meet or crossover. In our case,they are crossed at the values 1 and 25 for the indexes q and p, respectively.

    Finally, the 0th

    element 11 is interchanged with the value at index q, i.e., 1. The

    position q is now the final position of the pivot element 11.

    As a result, the whole array is divided into two parts. Where all the elementsbefore 11 are less than 11 and all the elements after 11 are greater than 11.

    Now the same procedure is applied for the two sub arrays. As a result, at theend when all the sub-arrays are loft with one element, the original array

    becomes sorted.

    Here, it is not necessary that the pivot element whose position is to be finalized in

    the first iteration must be 0th

    element. It can be any other element as well.

    The efficiency of the algorithm is primarily impacted by which element is chosen

    as the pivot point. The worst-case efficiency of the quick sort, O(n2

    ), occurs whenthe list is sorted and the left-most element is chosen. Randomly choosing a pivot

    point rather than using the left-most element is recommended if the data to be

    sorted is not random. As long as the pivot point is chosen randomly, the quick sort

    has an algorithmic complexity ofO(n log n).

    The quick sort is by far the fastest of the common sorting algorithms. But its

    algorithm is very complex and is massively recursive. In most cases the quick sort

    is the best choice if speed is important (and it almost always is). Use it for

    11 2 9 13 57 25 17 1 90 3

    11 2 9 13 57 25 17 1 90 3

    11 2 9 13 57 25 17 1 90 3

    11 2 9 3 57 25 17 1 90 13

    11 2 9 3 57 25 17 1 90 13

    11 2 9 3 1 25 17 57 90 13

    1 2 9 3 11 25 17 57 90 13

  • 8/7/2019 SearchingAndSorting

    62/78

    62

    repetitive sorting, sorting of medium to large lists, and as a default choice when

    you are not really sure which sorting algorithm to use.

    The following program illustrates the recursive C++ implementation of the quicksort on a list of numbers maintained as an array.

    //Program that implements insertion sort

    //on a list of numbers

    #include

    #include

    #include

    const int MAX=50;

    class array

    { private:int arr[MAX]; int count;

    public:

    array()

    {count=0;}

    int getcount()

    {return count;}

    void add(int);

    void display(void);

    int split(int *, int, int);void q_sort(int,int);

    };

    //adds a new element to the array

    void array::add(int item)

    { if(count

  • 8/7/2019 SearchingAndSorting

    63/78

    63

    { int v = split(arr, lower, upper);

    q_sort(lower, v-1);

    q_sort(v+1, upper);}

    }

    //Sort the array using quick sort

    int array::split(int *arr, int lower,int upper)

    { int v,p,q,t;

    p=lower+1; q=upper; v=arr[lower];

    while(q>=p)

    { while(arr[p]v) q--;

    if(q>p){ t=arr[p];arr[p]=arr[q];arr[q]=t;}

    }

    t=arr[lower];arr[lower]=arr[q];arr[q]=t;

    return q;

    }

    void main()

    { int element,n,i,c;

    array quick;

    while(1){ clrscr(); gotoxy(20,10);

    cout

  • 8/7/2019 SearchingAndSorting

    64/78

    64

    if(c==4)exit(1); clrscr();

    switch(c)

    { case 1:clrscr();

    coutn;

    cout

  • 8/7/2019 SearchingAndSorting

    65/78

    65

    sort is the slowest of the O(n log n) sorting algorithms, but unlike the merge and

    quick sorts it doesn't require massive recursion or multiple arrays to work. This

    makes it the most attractive option for very large data sets of millions of items.The heap sort works as it name suggests - it begins by building a heap out of the

    data set, and then removing the largest item and placing it at the end of the sorted

    array. After removing the largest item, it reconstructs the heap and removes the

    largest remaining item and places it in the next open position from the end of the

    sorted array. This is repeated until there are no items left in the heap and the sorted

    array is full.

    A common approach to implement a heap is to store the heap in an array. The 0th

    element will be the root and left and right child of any node arr[i] would be at

    arr[2i+1] and arr[2i+2]. Note that with an implementation starting at 0 for the root,the parent ofarr[i] is arr[floor(i-1)/2). This approach is particularly useful in the

    heapsort algorithm, where it allows the space in the input array to be reused to

    store the heap.

    The implementation of heapsort requires two arrays - one to hold the heap and the

    other to hold the sorted elements. In doing so, the only extra space required is that

    needed to store the heap. To do an in-place sort and save the space the second array

    would require, we use an algorithm which uses the same array to store both the

    heap and the sorted array. The adjustment of the nodes starts from the level one

    less than the maximum level of the tree (as the leaf nodes are always heap). Eachsubtree of a particular level is made a heap. Then all the subtrees at the level two

    less than the maximum level of tree are made heaps. This procedure is repeated till

    the root node. As a result the final tree becomes a heap.

    As an example, consider an array that contains 15 elements given below:

    7 10 25 17 23 27 16 19 37 42 4 33 1 5 11

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    The tree that can be constructed from the array is shown below:

    To make it a heap, initially the elements that are present at level one less than the

    maximum level of the tree are taken into consideration. In the case of above

    example, these are 17, 23, 27 and 16. They are converted into heap by comparing

    7

    19 37 42 4

    17 23

    10

    33 1 5 11

    27 16

    25

  • 8/7/2019 SearchingAndSorting

    66/78

    66

    and interchanging, if necessary, these nodes with their respective parents. The

    resulting tree looks like the one shown below:

    Now the elements that are present at a level two less than the maximum level of

    the tree are considered. In the case of above example, these are 10 and 25. These

    are also made the heap by the same procedure. The resulting tree looks like the one

    shown below:

    Similarly, each time one level is decremented and all the subtrees at that level are

    converted to heaps. As a result, finally the entire gets converted into heap, as

    shown below:

    The array element arr[i] is placed into its proper place by traversing the path from

    position i in the array to position 0 (root), seeking the first element greater than or

    equal to the element arr[i]. When that element is found, the element arr[i] is

    inserted immediately preceding it in the path (that is arr[i] is inserted as its son).As each element less than arr[i] is passed during the traversal, it is shifted down

    one level in the tree to make room for the element to be inserted.

    The algorithm for creating a binary heap from an array is given below:

    void CreateHeap(int arr[], int size)

    { int i,f,s;

    for(i=1;i

  • 8/7/2019 SearchingAndSorting

    67/78

    67

    son=i; father=(son-1)/2;

    while(son>0 && arr[father]0;i--)

    { //delete arr[i]

    int value=arr[i];arr[i]=arr[0];

    f=0;

    //determining large son

    if(i==1) s=-1;

    else

    s=1;

    if(i>2 && (arr[2]>arr[1])) s=2;

    while(s>=0 && value

  • 8/7/2019 SearchingAndSorting

    68/78

  • 8/7/2019 SearchingAndSorting

    69/78

    69

    2723 25 19 11 5 16 7 17 10 4 1 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    2523 16 19 11 5 1 7 17 10 4 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    2319 16 17 11 5 1 7 4 10 25 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    1917 16 10 11 5 1 7 4 23 25 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    7 17 10 4

    19

    23

    25

    15

    16

    11

    7 4 10

    17

    19

    23

    15

    16

    11

    7 4

    10

    17

    19

    15

    16

    11

    7

    10

    11

    17

    15

    16

    4

  • 8/7/2019 SearchingAndSorting

    70/78

    70

    1711 16 10 4 5 1 7 19 23 25 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    1611 7 10 4 5 1 17 19 23 25 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    1110 7 1 4 5 16 17 19 23 25 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    105 7 1 4 11 16 17 19 23 25 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    7 5 4 1 10 11 16 17 19 23 25 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    5 1 4 7 10 11 16 17 19 23 25 27 33 33 42

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

    10

    11

    16

    15

    7

    4

    1

    10

    11

    5

    7

    4

    1

    5

    10

    7

    4

    1

    5

    7

    4

    1

    5

    4

    1

    4

  • 8/7/2019 SearchingAndSorting

    71/78

    71

    The following program illustrates the C++ implementation of the heap sort on a list

    of numbers maintained as an array. This implementation combines the process of

    creating heap from the array and deleting the root node until the tree is empty.//Program that implements Heap sort on a list of numbers

    #include

    #include

    #include

    const int MAX=50;

    class array

    { private:

    int arr[MAX];

    int size;public:

    array()

    {size=0;}

    void add(int);

    void display(void);

    void sort(void);

    };

    //adds a new element to the array

    void array::add(int item)

    { if(size

  • 8/7/2019 SearchingAndSorting

    72/78

    72

    { cout

  • 8/7/2019 SearchingAndSorting

    73/78

    73

    }

    void main()

    { int element,n,i,c;array Heap;

    while(1)

    { clrscr();

    gotoxy(20,10);

    cout

  • 8/7/2019 SearchingAndSorting

    74/78

    74

    case 3:

    clrscr();

    Heap.sort();cout

  • 8/7/2019 SearchingAndSorting

    75/78

    75

    //Program that implements Merge sort

    //on a list of numbers

    #include#include

    #include

    const int MAX=50;

    class array

    { private:

    int arr[MAX];

    int size;

    public:

    array()

    {size=0;}void add(int);

    void display(void);

    void sort(void);

    };

    //adds a new element to the array

    void array::add(int item)

    { if(size

  • 8/7/2019 SearchingAndSorting

    76/78

    76

    { mid=(right+left)/2;

    m_sort(numbers,temp,left,mid);

    m_sort(numbers,temp, mid+1,right);merge(numbers,temp, left,mid+1,right);

    }

    }

    void merge(int numbers[],int temp[],int left,int mid,int right)

    { int i,left_end,num_elements,tmp_pos;

    left_end=mid-1;

    tmp_pos=left;

    num_elements=right-left+1;

    while((left

  • 8/7/2019 SearchingAndSorting

    77/78

    77

    { clrscr(); gotoxy(20,10);

    cout

  • 8/7/2019 SearchingAndSorting

    78/78

    getch();

    }

    }

    77..33..77.. CCOOMMPPAARRIISSOONN OOFF SSOORRTTIINNGG AALLGGOORRIITTHHMMSS

    The following table provides a comparison of the complexity of the various sorting

    algorithms under best, average and worst cases.

    Name Best Average Worst Stable Method

    Bubble sort O(n2) O(n

    2) O(n

    2) Yes Exchanging

    Selection sort O(n2) O(n

    2) O(n

    2) No Selection

    Insertion sort O(n) O(n2) O(n

    2) Yes Insertion

    Merge sort O(n log n) O(n log n) O(nlogn) Yes Merging

    Heap sort O(n log n) O(n log n) O(n log n) No Selection

    Quick sort O(n log n) O(n log n) O(n2) No Partitioning