how to add a selection sort to this code?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 5
Reputation: chetoos is an unknown quantity at this point 
Solved Threads: 0
chetoos chetoos is offline Offline
Newbie Poster

how to add a selection sort to this code?

 
0
  #1
Nov 1st, 2008
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



  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. struct database {
  7. int id_number, age;
  8. float salary;
  9. string name;
  10. };
  11. void input_data(struct database*data_ptr)
  12. {
  13. int i_tmp;
  14. float f_tmp;
  15. string s_tmp;
  16. cout<<"enter name:";
  17. cin >>s_tmp;data_ptr->name = s_tmp;
  18. cout << "enter id_number :";
  19. cin >> i_tmp;data_ptr->id_number = i_tmp;
  20. cout << "enter age :";
  21. cin >> i_tmp;data_ptr->age = i_tmp;
  22. cout << "enter salary:";
  23. cin >> f_tmp;data_ptr->salary = f_tmp;;
  24. cout<<endl;
  25. }
  26. void output_data(struct database*data_ptr)
  27. {
  28. cout << "*********************************\n";
  29. cout << "name = " << data_ptr->name << "\n";
  30. cout << "id_num = " << data_ptr->id_number << "\n";
  31. cout << "age = " << data_ptr->age << "\n";
  32. cout << "salary = " << data_ptr->salary << "\n";
  33. cout << "*********************************\n";
  34. }
  35.  
  36. int main()
  37. {
  38. database*employee ;
  39. int n;
  40. cout<<"how many employees do you want to enter?";
  41. cin >> n ;
  42. employee = new database[n] ;
  43. for ( int i = 0 ; i < n ; i++)
  44. input_data(employee+i);
  45. for ( int a = 0 ; a < n ; a++)
  46. output_data(employee+a);
  47. return 0;
  48. }




THE SELECTION SORT CODE IS
  1. void selectionSort(int[] list, int listLength)
  2. {int index;
  3. int smallestIndex;
  4. int minIndex;
  5. int temp;
  6. for (index = 0; index < listLength –1; index++) {smallestIndex = index;
  7. for (minIndex = index + 1; minIndex < listLength; minIndex++)
  8. if (list[minIndex] < list[smallestIndex])smallestIndex = minIndex;
  9. temp = list[smallestIndex];
  10. list[smallestIndex] = list[index];
  11. list[index] = temp;
  12. }
  13. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to add a selection sort to this code?

 
0
  #2
Nov 1st, 2008
Probably the simplest way to adopt your selectionSort for int arrays function code is as follows.
Look at the function code. There is temp variable of a sorted array base type (to swap elements). There is a compare ints expression in the loop. And, of course, there is a sorted array parameter declaration in the function header.
Now you want to sort an array of the (struct) database type. Change (wrong) parameter declaration to database list[] . Change the type of temp variable to database. Change the comparing expression to list[minindex].id_number < list[[smallestIndex].id_number .
That's all. Now you have (ineffective ) struct database array sort function...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 5
Reputation: chetoos is an unknown quantity at this point 
Solved Threads: 0
chetoos chetoos is offline Offline
Newbie Poster

Re: how to add a selection sort to this code?

 
0
  #3
Nov 1st, 2008
i tried to edit my code as u said but i had alot of errors will u write me the code implemented in the main code
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: how to add a selection sort to this code?

 
0
  #4
Nov 1st, 2008
Yes, I can - but I don't want.
I don't like copypasters...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC