Solve Sorting Technique Problem with 4 Elementary Sorts

  • Thread starter Thread starter shayanahmad
  • Start date Start date
  • Tags Tags
    Sorting
Click For Summary

Discussion Overview

The discussion revolves around a programming problem related to sorting a list of patients based on age and patient ID using four elementary sorting techniques: selection sort, bubble sort, insertion sort, and shell sort. The context includes coding in C/C++ and handling input data.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant requests help with a coding problem involving sorting patient data by age and ID.
  • Another participant suggests that the original poster should demonstrate what they have tried so far to receive more targeted assistance.
  • A participant shares a partial code snippet that collects patient data but does not implement any sorting algorithms.
  • Another participant recommends storing patient data in a text file instead of entering it manually each time the program runs.
  • A later reply critiques the provided code for only filling an array with patient data and not performing any sorting as required by the problem statement.

Areas of Agreement / Disagreement

Participants generally agree that the original poster needs to implement sorting algorithms and improve data handling, but there is no consensus on the best approach to achieve this.

Contextual Notes

There are limitations in the provided code, including missing sorting implementations and potential issues with user input handling. The discussion does not resolve how to effectively parse input data from a file or implement the sorting algorithms.

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;

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

Input input[NUMBER_OF_PATIENTS];
int patientno;

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

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

count<<"Enter patient age:";
cin>>input.age;
}
for (i=0; i<NUMBER_OF_PATIENTS; ++i)
{
count<<"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.