| | |
how to add a selection sort to this code?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 5
Reputation:
Solved Threads: 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
THE SELECTION SORT CODE IS
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
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; } }
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
That's all. Now you have (ineffective
) struct database array sort function...
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... ![]() |
Similar Threads
- Help with Swap function sort algorithms (C++)
- Selection Sort on a string with char and int? (C++)
- Selection Sort and String (C++)
- linkedlist (C++)
- Bubble sort & File output jibrish errors??? (C)
- Reading an input file as a class memeber function (C++)
- Sorting a selection (Visual Basic 4 / 5 / 6)
- AIRPLANE PROGRAM (C++)
Other Threads in the C++ Forum
- Previous Thread: VS 6.0 Linking Problem
- Next Thread: array
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






