1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to add a selection sort to this code?

Discussion in 'C++' started by chetoos, Nov 1, 2008.

  1. chetoos

    chetoos New Member

    Joined:
    Nov 1, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i want to add a selection sort to this code by the id_number and i have the selection code but i cant implement it to my code



    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct database {
      int id_number, age;
      float salary;
      string name;
    };
    void input_data(struct database*data_ptr)
    {
      int i_tmp;
      float f_tmp;
      string s_tmp;
      cout<<"enter name:";
      cin >>s_tmp;data_ptr->name = s_tmp;
      cout << "enter id_number :";
      cin >> i_tmp;data_ptr->id_number = i_tmp;
      cout << "enter age :";
      cin >> i_tmp;data_ptr->age = i_tmp;
      cout << "enter salary:";
      cin >> f_tmp;data_ptr->salary = f_tmp;;
      cout<<endl;
    }
    void output_data(struct database*data_ptr)
    {
      cout << "*********************************\n";
      cout << "name = " << data_ptr->name << "\n";
      cout << "id_num = " << data_ptr->id_number << "\n";
      cout << "age = " << data_ptr->age << "\n";
      cout << "salary = " << data_ptr->salary << "\n";
      cout << "*********************************\n";
    }
    
    int main()
    {
       database*employee ;
       int n;
       cout<<"how many employees do you want to enter?";
       cin >> n ;
       employee = new database[n] ;
       for ( int i = 0 ; i  < n ; i++)
           input_data(employee+i);
       for ( int a = 0 ; a  < n ; a++)
           output_data(employee+a);
       return 0;
    }
    
    



    THE SELECTION SORT CODE IS:
    Code:
    void selectionSort(int[] list, int listLength)
     {int index;
    int smallestIndex;
    int minIndex;
    int temp;
    for (index = 0; index < listLength –1; index++) {smallestIndex = index;
    for (minIndex = index + 1; minIndex < listLength; minIndex++)
    if (list[minIndex] < list[smallestIndex])smallestIndex = minIndex;
    temp = list[smallestIndex];
    list[smallestIndex] = list[index];
    list[index] = temp;
    }
    }
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    The selection sort (looks more like bubble sort to me, but then I don't know what a "selection sort" is) sorts a list of integers. So you need to adapt it to take an array of employee rather than an array of int. So the first line will probably end up being something like
    Code:
    void selectionSort(employee[] list, int listLength)
    
    So then you need to adapt the code to use list[x].id_number instead of just list[x], assuming of course you want to sort by id_number.
    Then you'll need to add a call to selectionSort() somewhere in main().
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Sorry, I'm thinking in C++ (of employee being a class rather than a struct). Try instead:
    Code:
    void selectionSort(struct employee *list, int listLength)
    
     

Share This Page