• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sort by Rate  RSS feed

 
siva kumark
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help in solving the below problem. I am struck with the code that I prepared till now.

Sort by Rate:

There are 'n' aircrafts plying between Chennai and New York and the Shipping Officer verbally told the rates that each aircraft carrier charge but Ravi wanted your help to sort the shipment rates in ascending order along with the names of the aircrafts, so that he could decide the economical carrier.

Input Format:
The first line of the input consists of an integer 'n', corresponding to number of aircraft carriers plying between Chennai and New York.
The second n line of the input consists of string values corresponds to name of each aircraft carrier.
The third n line of the input consists of double corresponds to shipment rate through each aircraft carrier.

Output Format:
Print the aircraft carrier names along with the rate they offer in ascending order based on rates.
Refer the Sample Input and Output for formatting specifications.

[All text in bold corresponds to input and rest corresponds to output.]
Sample Input and Output:
5
AirIndia
Spicejet
KingFisher
Lufthansa
Bluedart
350.52
580.46
550.98
320.75
530.19
Details are :
Lufthansa - 320.75
AirIndia - 350.52
Bluedart - 530.19
KingFisher - 550.98
Spicejet - 580.46



 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you stuck? Does it compile? Does it run? Are you getting the output you expected?
 
siva kumark
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't produce the desired output. I am not sure how I can sort the rate as well as the corresponding carrier names together and display it.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parallel arrays are almost impossible to manage. Obviously sorting one of the arrays doesn't sort the others. You'll need to create an Aircraft class with these fields and then have a single array that contains Aircraft. Try that first and then we'll get to how to go about sorting it.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is surCraft and surCar?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looks like the "sur" arrays are only used as temporary variables.  Normally a swap of indices looks like this:
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you're trying to do a Bubble sort, is that correct?  The most basic Bubble sort algorithm/psudeocode looks like this:
Is that clear enough?  There are way to optimize the above, but that's the basic idea.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!