473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

bubble sort help.

evilmonkey
I am trying to sort an array by name ( that works) but I only need each sorted name to show up once, regardless of how many times it shows up in the sort. Where do I go from here?

Expand|Select|Wrap|Line Numbers
  1. public class Report {
  2.     Rental[] rentals = new Rental[20];
  3.     /** Creates a new instance of Report */
  4.     public CustomerReport() {
  5.     }
  6.     public Rental[]sort(Rental[] rentals)
  7.   {
  8.         Rental[] newRentals = new Rental[rentals.length];
  9.         for (int i = 0; i < rentals.length; i++){
  10.                  newRentals[i] = rentals[i];
  11.         }
  12.         int n = rentals.length;
  13.         for (int pass=1; pass < n; pass++){
  14.                  for(int i=0; i< n-pass; i++){
  15.                        int result = newRentals[i].getCustomer().getName().compareTo(newRentals[i+1].getCustomer().getName());
  16.                             if(result > 0)
  17.                                  {
  18.                                    Rental temp = newRentals[i];
  19.                                    newRentals[i] = newRentals[i+1];
  20.                                    newRentals[i + 1] = temp;
  21.                                    }
  22.         } 
  23.      } return newRentals;
  24.    }
  25. public Rental[] getRentals(){
  26.     return rentals;
  27.   }
  28.   }
Apr 12 '07 #1
2 1685
JosAH
11,448 Expert 8TB
Does the sorting method has to be a bubble sort? If not simply add all your
array elements to a SortedSet. It will remove the duplicates for you and sort
all the unique members. All you have to do is make your class implement
the Comparable interface.

kind regards,

Jos
Apr 13 '07 #2
r035198x
13,262 8TB
I am trying to sort an array by name ( that works) but I only need each sorted name to show up once, regardless of how many times it shows up in the sort. Where do I go from here?

Expand|Select|Wrap|Line Numbers
  1. public class Report {
  2. Rental[] rentals = new Rental[20];
  3. /** Creates a new instance of Report */
  4. public CustomerReport() {
  5. }
  6. public Rental[]sort(Rental[] rentals)
  7. {
  8. Rental[] newRentals = new Rental[rentals.length];
  9. for (int i = 0; i < rentals.length; i++){
  10. newRentals[i] = rentals[i];
  11. }
  12. int n = rentals.length;
  13. for (int pass=1; pass < n; pass++){
  14. for(int i=0; i< n-pass; i++){
  15. int result = newRentals[i].getCustomer().getName().compareTo(newRentals[i+1].getCustomer().getName());
  16. if(result > 0)
  17. {
  18. Rental temp = newRentals[i];
  19. newRentals[i] = newRentals[i+1];
  20. newRentals[i + 1] = temp;
  21. }
  22. } return newRentals;
  23. }
  24. public Rental[] getRentals(){
  25. return rentals;
  26. }
  27. }
You can also read this tip of the week.
Apr 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
2
by: joesindel | last post by:
So heres the deal... I work for a company in which numbers are dropping. The owners created a game where calling potential clients racks up points for crap like basketball ticekts and such. The...
5
kuchma2000
by: kuchma2000 | last post by:
Hi. I have a C program (see code below) which I need to urgrade. All I need to do is: a. Extract the temps for the City #1 and store in a one dimensional array, display the array. b. Sort the one...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
1
by: Prosdo | last post by:
Hi guys. I have a final program for school and the assignment is to show the top 5 numbers for the Cash 5 lottery over the past few days. I think I have all my loops right.I think I have the output...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.