• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

String Input and Bubble Sort

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a program that receives user input to build a string array. Then I want the string of random letters sorted in order of the abc's. I previously built a program using bubble sort to sort an initialized array of numeric values of type int. This program worked out fine, so as I do with most programs I learn, I am trying to modify it to the program stated initially.

With print statements, I can evaluate some of what the program is doing. In this evaluation I am going to attempt to enter peopl into the array, by typing p, enter, e, enter, o, enter, etc. By evaluating the print statements in the output. P enters the array, which is then evaluated in the bubblesort routine, then the program looks for another input value for the array. I enter this value and the program ends. In other words, I think this program is sorting a nearly empty array, but my print statement in the second for loop doesn't reflect that. I think my program needs to collect the elements in the array, then provide the array to Bubble Sort routine, then print the sorted array.

Here is the code:

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edited your post to have code tags. They work like bold or italics - highlight what you want to include, then click the 'code' button. This will let our site preserve your formatting and do context highlighting, making it easier to read.

I don't have time to look at your code right now, but someone will shortly, I have no doubt.
 
fred rosenberger
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I did notice...

after you enter p, you go through your bubble sort (lines 19-35). Your exit condition for your bubble sort's while-loop is setting swapped to false. You then loop back and get the next input character. you then hit the "while (swapped)"...and swapped is still false from the last loop around.
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dan Storjohann: Welcome to Ranch!
I think you must read about strings. In java, Strings are not array of characters.
More over this code of yours is not going to work for mixed case input.
Let us consider that you are going to work with only one case(either lower or upper), and then try to sort out the problem.

Before moving on to you solution:
Few tips:
1)avoid using array of characters.. it doesnt do any good.
2)avoid using complicated form of any algorithm.

Did you work this bubble sort out on pencil and paper?
If not then I will advise you to do it,you will learn much more.
After you do this, just think of how can you sort any thing, compare the values and the replace them in proper position, this is what a comparison based sorting algorithm is all about.
The name and efficiency of the algorithm is derived from the way elements are compared and replaced.

Programmers have different views on each versions of algorithm( google for bubble sort with early exit and so on, sky is the limit.)
for me,a simple bubble sort works like this:
compare the first element with the adjacent element, compare on criteria(increasing or decreasing order), swap them.
repeat the above code for as many times as the number of elements.

So let us now solve your problem of peopleassuming that is supposed to be sorted in ascending order.
so now follw the steps.
compare first element and its adjacent element: is e<p? yes..swap.
now its epople
compare the second element with the adjacent element: is o<p? yes..swap.
now its eopple
compare the third element with the adjacent element: is p<p? no..dont swap.
now its eopple
compare the fourth element with the adjacent element: is l<p? yes..swap.
now its eoplpe
compare the fifth element with the adjacent element: is e<p? yes..swap.
now its eoplep

now repeat this for as many times as the number of characters, in this case its 6 and you can see that your input is sorted.

i will suggest you to write your bubble sort is simple manner as above using just two for loops.
If you still don't get it, then post your work,then lets see further.

 
Frank Metzger
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an example with a nested for loop Bubble Sort. This time with an array of 5 open elements is initialized, then filled with keyboard input. Note however, that the array elements are not the same as the input, "see Original array is: below". The Bubble Sort routine does sort it the right way, see "Sorted array is:" below. This is not the array I originally built.

This code also works when an array is initialized with five values, this is commented out, //int nums[] = {2, 3, 7, 8, 1}; but you have to comment out the "//building array" section in the code also.

Please enter your char: 5
Please enter your char: 4
Please enter your char: 3
Please enter your char: 2
Please enter your char: 1
Original array is: 53 52 51 50 49
a is:1 b is:4
nums[b-1]50 nums[b]49
a is:1 b is:3
nums[b-1]51 nums[b]49
a is:1 b is:2
nums[b-1]52 nums[b]49
a is:1 b is:1
nums[b-1]53 nums[b]49
a is:2 b is:4
nums[b-1]51 nums[b]50
a is:2 b is:3
nums[b-1]52 nums[b]50
a is:2 b is:2
nums[b-1]53 nums[b]50
a is:3 b is:4
nums[b-1]52 nums[b]51
a is:3 b is:3
nums[b-1]53 nums[b]51
a is:4 b is:4
nums[b-1]53 nums[b]52
Sorted array is: 49 50 51 52 53

 
Ashish Schottky
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dan: You are entering the characters in integer array.. perfectly fine, but then this will now be modified into the ASCII values.
just enter p e o p l
.
Is this what you want?
 
Frank Metzger
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help so far everyone.

The array peopl is not that straight forward; the original array output is 112 101 111 112 108 corresponding to peopl; and there is the final sorted array which is 101 108 111 112 112 corresponding to elopp. ellop is what I want but am not getting.

It is possible to use a type cast with individual numbers but I can not get this to work with an array.
int i = 112; // 112 is 'p' in ASCII
char c = (char) i; // c is now 'p'
System.out.println("i is:" + i + " c is:" + c);
Output: i is:112 c is:p

Following above:
cnums[i] = nums[i]
char cnums[] = new char[];
char cnums[] = (char) nums[];


Can somebody help me to get this to work with my nums[i] array? Beyond the comment "display converted array". Here is the code.



 
Ashish Schottky
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dan: Read more on typecasting.
I think you should have also shown us what you expect the program to do.
just modify individually where ever you need a char.

 
permaculture is largely about replacing oil with people. And one tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic