Sorting technique

  • #1
Hello i'm having problem in solving this question with c/c++ code , plz 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
 

Answers and Replies

  • #2
rcgldr
Homework Helper
8,770
569
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?
 
  • #3
i have done this so far , now plz 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;

}
 
  • #4
rcgldr
Homework Helper
8,770
569
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:
  • #5
35,287
7,140
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.
 

Related Threads on Sorting technique

  • Last Post
Replies
6
Views
2K
Replies
2
Views
908
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
1
Views
5K
Replies
0
Views
5K
  • Last Post
Replies
4
Views
1K
  • Last Post
Replies
0
Views
5K
Replies
6
Views
2K
  • Last Post
Replies
4
Views
2K
Replies
0
Views
1K
Top