This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

BubbleSort and Selection Sort an Array in java?  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning! I am working on my very last assignment for my Java programming class.  This is my last assignment that determines whether i get my Associates Degree.  I have already passed my Proctor Exam. I am just caught on this assignment.  I have the first half done and have started on the Bubblesort section (doing one at a time to make sure they are right).  I am just having trouble figuring out what i am doing wrong.  The assignment so to user input words til you type done and it will deplay. (which i have), then bubblesort those words and display and then same with selection sort.
Any help would be great!  I am just at my wits end....


Thank you so much for listening to the rabbling!
 
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am just having trouble figuring out what i am doing wrong.  


Please explain why you think it is wrong.  Post any output that shows the problem and add some comments explaining what is wrong.

Do you have a description in English of the steps the code should take (a design) to solve the problem?  
Once you have that design, then write code to implement it.
 
Saloon Keeper
Posts: 5759
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did the magic number '11' come from. Shouldn't that be words.length?
 
Marshal
Posts: 63849
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . Shouldn't that be words.length?

And it shouldn't be accompanied by the <= sign.

. . . and welcome to the Ranch

I think you should start with a method that swaps two elements in an array. You can reuse that method as often as you like.
 
Helen Morgan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK i fixed it a little more! It compiles but still doesnt bubblesort the words i input?
This is the output

Please enter words, or done to stop

apple
car
cat
og
done
The list contains:

apple
car
cat
og
BubbleSort):
BUILD SUCCESSFUL (total time: 15 seconds)




 
Helen Morgan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Carey Brown wrote:. . . Shouldn't that be words.length?

And it shouldn't be accompanied by the <= sign.

. . . and welcome to the Ranch

I think you should start with a method that swaps two elements in an array. You can reuse that method as often as you like.



Thank you!!
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

still doesnt bubblesort the words


What is wrong with the output you posted?  It looks like the words are in order.
 
Helen Morgan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

still doesnt bubblesort the words


What is wrong with the output you posted?  It looks like the words are in order.


i just redid and typed them and it doesnt reorder.  

Please enter words, or done to stop
cat
zebra
dog
snake
done
The list contains:
cat
zebra
dog
snake
BubbleSort:  



 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

it doesnt reorder.  

How do you know that?
Where does the code print out the changed contents of the array after doing the sort? The sort method returns an array but nothing is done with it.

BubbleSort:  


That is printed at the end of the sort method.  The main method needs to print the array's contents after that.
 
Helen Morgan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afternoon!  I finally got the Bubblesort fixed and working,  Now i cant figure out what i have going on with the Selection Sort.....I probably am doing it wrong?  What am i doing wrong?
and  again i greatly appreciate this!  

 
Norm Radder
Rancher
Posts: 3353
33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have the list of steps a program must do to implement a selection sort?  
Could you add them to the code as comments so anyone reading the code can check to see if the code is following the steps shown in the comments.
 
Helen Morgan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what i need to do  - hope this helps
Write console program that repeatedly prompts the user to enter data until they type done
(any case, Upper, Lower, or Mixed). As they enter the data, assign it to a two-dimension array
where the first dimension contains exactly what they type and the second dimension contains
the first non-whitespace character of what they type, in lowercase. If they enter a blank line, it
is acceptable to skip that line. When they type done, do these steps:
1. display: As Entered - Done
2. for each entry in the array, display the index of the first dimension, the second
dimension's value, and the first dimension's value on a single line separated by :
(colon). Done
3. display: Bubble Sorted - Done
4. using a bubble sort, for each entry in the array, display the original index of the first
dimension, the second dimension's value, and the first dimension's value on a single
line separated by : (colon) sorted by the second dimension. Done
5. display: Selection Sorted - This is where i am kind of stuck
6. using a selection sort, for each entry in the array, display the original index of the first
dimension, the second dimension's value, and the first dimension's value on a single
line separated by : (colon) sorted by the first dimension.




Helen Morgan wrote:Afternoon!  I finally got the Bubblesort fixed and working,  Now i cant figure out what i have going on with the Selection Sort.....I probably am doing it wrong?  What am i doing wrong?
and  again i greatly appreciate this!  

 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the logic for the selection sort?  Can you add it as comments in the code so anyone reading the code can see if the code is following the logic shown in the comments?
 
Helen Morgan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:What is the logic for the selection sort?  Can you add it as comments in the code so anyone reading the code can see if the code is following the logic shown in the comments?



Not sure how to edit post.  
But what i am trying to do is what i have inputted originally, (arrlst) - selection sort that string after the bubblesort output. I don't know if i am explaining it right.

 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

selection sort that string


I see several lines of code in the selectionSort method.  What determined what statements  were written there?  In other words what is the logic for that method?

I find it very useful when writing code to have designed the logic first, then write down the steps of the logic as comments in the code
and then write the code to follow the steps in the logic.  


The posted code does not compile without errors.
Please copy the full text of the error message and paste it here. It has important info about the error.
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use while (myScanner.hasNext()) nor while (myScanner.hasNextLine()) when reading from System.in. That Scanner implicitly always has a next line. You don't need to check for null; those methods never return null, though they may throw exceptions if anything goes wrong. I suggest you use a bit of weird syntax (line 2). I know we have a policy of not giving out complete code, but you would never guess this code in a hundred years:-You can only get a 0‑length token returned from next() if you change the delimiter. It is however quite easy to get a 0‑length line from nextLine() by pushing the enter key twice, which is why I wrote line 4.
I don't understand the bit about two arrays. You don't seem to have created an array of arrays, which would be declared as a String[][] whose 2nd length will always be 2.
Why are you using Collections.sort before your bubble sort method? That doesn't look right.
You know you can get a String[] out of your List? Go through its methods.
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!