Solve Sorting Technique Problem with 4 Elementary Sorts

  • Thread starter Thread starter shayanahmad
  • Start date Start date
  • Tags Tags
    Sorting
AI Thread Summary
The discussion revolves around a coding problem that requires sorting a list of patients by age and Patient ID using four elementary sorting techniques: selection sort, bubble sort, insertion sort, and shell sort. The user has provided a C++ code snippet that collects patient data but does not implement the sorting algorithms. Suggestions include reading patient data from a text file instead of user input for better efficiency and parsing the data correctly. The user is encouraged to explore sorting algorithms online and integrate them into their existing code to achieve the desired output. The main goal is to create a program that accurately sorts the patient list as specified.
shayanahmad
Messages
2
Reaction score
0
Hello I'm having problem in solving this question with c/c++ code , please help me ?


An eccentric doctor visits his patients in order of decreasing age. Patients entering the waiting list specify not only their names but also their ages. Each time the doctor wants to visit a patient, the oldest is selected and removed from the list (so children may have to wait a very long time). Write a program, based on elementary sorting techniques to sort this list on first on Age and then on Patient ID, which this doctor can use.


Before
PatientID Name Age
P102 Arif Taj Mairza 50
P203 Sadar Khan 65
P546 Afsheen Bano 34
P605 Kai Whong 45
P340 Hanah Duong 23
P391 Usman Habib 65
P200 Alina Shah 34

After
PatientID Name Age
P203 Sadar Khan 65
P391 Usman Habib 65
P102 Arif Taj Mairza 50
P605 Kai Whong 45
P200 Alina Shah 34
P546 Afsheen Bano 34
P340 Hanah Duong 23



Solve this problem by applying four different elementary sorting techniques that are

a) Selection sort
b) Bubble sort
c) Insertion sort
d) Shell sort
 
Physics news on Phys.org
Since this is appears to be homework, you need to show some idea of what you've tried so far. The different sort algorithms can be found doing a web search if they aren't already describe in some form from your class.

I assume the data is in a text file. What have you learned about reading lines and parsing (separating lines into sub-fields) so far in your class?
 
i have done this so far , now please guide me how to sort this with respect to patient id and age

#include <iostream>
using namespace std;

typedef struct
{
char name[100];
int patientno;
int age;
} Input;

int main (int argc, char *argv[],int pno)
{
int i;
int NUMBER_OF_PATIENTS;

cout<<"Enter number of patients:";
cin>>NUMBER_OF_PATIENTS;

Input input[NUMBER_OF_PATIENTS];
int patientno;

for (i=0; i < NUMBER_OF_PATIENTS; ++i)
{
cout<<"Enter patient no: ";
cin>>input.patientno;

cout<<"Enter patient name:";
cin>>input.name;

cout<<"Enter patient age:";
cin>>input.age;
}
for (i=0; i<NUMBER_OF_PATIENTS; ++i)
{
cout<<"P"<<input.patientno<<" "<<input.name<<" "<<input.age<<endl;
}
system("pause");
return 0;

}
 
Rather that have a user input data each time you run the program, I think it would be better to store the data into a text file (you can use notepad or any text editor to do this). Then have your program read in the entire file, parse for lines and fields, or read the file one line at a time (several functions to do this) and parse each line for fields.
 
Last edited:
In addition to what rcgldr already said, your code is doing nothing more than filling the input array with patient data, and then displaying the data. You aren't sorting the data by any of the four methods you're supposed to use.
 
Back
Top