anyone know how to use bubble sort and also binary search ?
this program cannot search out the name that user insert.
anyone know how to fix it ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include<iostream>
#include<string>
#define size 5

using namespace std;

struct student {
string name;
int id;
string prog;
double cgpa;

};


class stinfo {
    private:
        student data[size];
        int last;
    public:
        stinfo();
        void add(string name, int id, string prog, double cgpa);
        void printAll(int count);
        int searchName(string key, int count);
};


int min(int a[], int pos,int count)
{     //    A , i , 2

    int min=99999;
    int i;
    int c;
    for(i =pos; i<count;i++){

        if(a[i]<min){
            min=a[i];
            c=i;
        }
    }
    return c;
}


stinfo :: stinfo()
{
    last = 0 ;
}


void stinfo::add(string name, int id,string prog, double cgpa)
{
    if (last == size) {
        cout <<"FULL"<<endl;
    return ;
}
data[last].name = name;
data[last].id = id;
data[last].prog = prog;
data[last].cgpa = cgpa;
++last;
}           // all infomation are in data 


int stinfo::searchName(string keyName, int count)
{
    int low = 0;
    int high = count;
    int middle ;

    string A[size];
    int i,j;
    string current;

    for (int p=0; p<last; p++)
    {
        A[p]= data[p].name;
        cout <<A[p]<<endl;
    }




     for(i=0; i<size;i++)
     {   for (j=0; j<size-1; j++)
        {   if(A[j]>A[j+1])
           {
               current = A[j];
               A[j] = A[j+1];
               A[j+1] = current;
           }
         }     
     }
    // start search

    while (low <= high)
    {
        middle = (low + high)/2;
        if (keyName == A[middle])
        {
            cout <<"found "<<endl;
            cout <<A[middle]<<endl;

            return middle;
        //  not found

        }else if (keyName < A[middle]){
            high = middle - 1;
        }else {
            low = middle + 1;
        }
    }
    cout<<"not found"<<endl;
    return -1;


}

void stinfo::printAll(int count)
{
    if (count > size){
        return ;
    }else{

        for (int i =0; i<count; i++)
        {
            cout<<data[i].name<<endl
                <<data[i].id<<endl
                <<data[i].prog<<endl
                <<data[i].cgpa<<endl;
        }

    }
}




int main()
{

    char ans;
    string name1,prog1,keyName;
    int id1;
    double cgpa1;
    int count = 0;

    stinfo st;
    do{
    cout <<">>>>>  STUDENT INFOMATION RECORDING SYSTEM  <<<<< "<<endl;

    cout <<"A.) Insert NEW Student Record."<<endl
         <<"B.) print all Student Record."<<endl
         <<"C.) search."<<endl
         <<"Z.) exit"<<endl;


    cout <<"Please Choice ONE Option : ";
    cin >>ans;
    cout<<endl;

    switch ( ans )
    {
    case 'a':
    case 'A':
        cout <<endl;
        cout <<">>>>> INSERT NEW STUDENT INFORMATION <<<<<"<<endl;

        cout <<"NAME : ";
        cin >>name1;

        cout <<"ID: ";
        cin >>id1;

        cout <<"PROGRAMME : ";
        cin >>prog1;

        cout <<"CGPA: ";
        cin >>cgpa1;

        st.add ( name1,id1,prog1,cgpa1 );

        cout <<endl
             <<"INSERT COMPLETE... "<<endl
             <<endl;
        count ++;
        break;

    case 'b':

        st.printAll(count);
        break;


    case 'c':
        cout<<"insert name :";
        cin >>keyName;
        st.searchName(keyName,count);
        break;



        default :
        cout <<"Please Try Again..."<<endl
             <<endl;
    }

    }while ( toupper(ans) !='Z' ) ;

        cout <<"EXIT The Program..."<<endl
             <<endl;

    system("pause");
    return 0;

}

Yes, to both questions. When inserting a new element into a sorted list (vector/array), don't just add it to bottom and try to re-sort the array. It is MUCH more efficient to find the position that it should be inserted in, move all the rest down one element (possibly requiring a reallocation of the array), and inserting it there directly. This is called an "insertion sort" algorithm. Bubble sorts are notoriously inefficient, especially when all but one element (or a few elements) are already in sorted order. There are methods that minimize this overhead, such as placing new elements (as you do) at the end of the array, but keeping track of the number of sorted elements, so a binary search which doesn't find the target will then go to the new elements and linearly search those. In such an algorithm, when the number of unsorted elements at the bottom of the array reach some maximum number, then the array is re-sorted and that is reset to 0.

Sorting and Searching - see the definitive Knuth volume on that (Google search appropriate here).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.