Re: Sorting the Arrays
From: Bonux (acerpower_at_servihoo.com)
Date: 07/19/04
- Next message: Craven: "Re: just getting started, setting up.."
- Previous message: Olivier Merigon: "TCP client application does detect network failure"
- In reply to:(deleted message) Mark Haase: "Re: Sorting the Arrays"
- Next in thread: Andrew Thompson: "Re: Sorting the Arrays"
- Reply: Andrew Thompson: "Re: Sorting the Arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 19 Jul 2004 08:53:28 +0400
At least please help me , just by advising how to invoke the charSortArray
method and print it after the sort, ....
Im a new to java , so Im trying to learn , but I need help on some part, Im
not taking any courses , I just bought a book and learning it , I can also
buy the solutions but I don't want to buy it , because I want to learn by
myself. I've look at ways to sort arrays on the net but I ddin't get any
help , so I turned to you
I know my program is crap, but you should remember one thing , at the
beginning its always like that
"Mark Haase" <mehaase@earthlink.net> wrote in message
news:mehaase-194EB2.13560818072004@netnews.upenn.edu...
> Holy crap, you're way off!
>
> In article <cde0io$t2l$1@news.intnet.mu>,
> "Bonux" <acerpower@servihoo.com> wrote:
>
> > I've done the following part of the program to sort the array , but Im
stuck
> > at the point where I have to sort the Array
> > How to invoke the charSortArray() method to sort the array and print the
> > result after the sort?>
> > can anyone please help.,
> >
> >
> > public class SortArray
> > {
> >
> > public static void charSortArray(char[] charArray , int[] Length)
>
> "int[] Length" is possible, but probably a very bad idea.
>
> First, instance variables and method parameters should be lowercased,
> with multiple words having each successive word uppercased, as you did
> with "charSortArray" and "charArray".
>
> Second, your "length" variable ought to be just an int, since otherwise
> you're just wasting space by forcing the calling code to pass one number
> as an array. (The length is just a number, right?)
>
> Third, you don't need to pass length as a separate parameter because in
> Java all arrays are objects which have a length parameter built in...
>
> > {
> > for (int i = 0; i< charArray.length; i++)
>
> ..as you figured out here.
>
> > {
> > for (int j= 0 ; i< charArray.length ; j++) {\
>
> This should say "j < charArray.length", I would imagine.
>
> > int temp;
>
> This is best defined outside of the nested for loops. This isn't an
> obvious concept to a beginner, but basically the compiler has to keep
> pushing its local variables onto and off of the stack when you do
> something like this. If a variable is going to be changed a number of
> times in a loop, declare it outside the loop. It will, however, work
> this way; its just not a good practice.
>
> > if(i<j)
> > {
> > temp = i;
> > i=j;
> > j=temp;
> > }
>
> What the hell is going on here? As soon as i=1 and j=0, this code will
> transpose the two, so that j=1 and i=0, which means you'll loop
> infinitely. You haven't even touched the char[] either. How could this
> code possible sort the array?
>
> > }
> > }
> > }
>
> These brackets aren't spaced right. You should pick up a basic Java book
> and read up on how white space and control structures are generally
> formatted in java source.
>
> > public static void main(String[] args)
> > {
> > char[] charArray={ 'a' ,'e' ,'b' ,'z' ,'f'};
> >
> > for (int x =0 ; x< charArray.length ; x++){
> > System.out.println("The Element in the CharArray is:"+charArray[x]);
> > }
>
> This part is fine, but you never call charSortArray(). That's fine
> because it wouldn't work anyway, but at some point you will need to.
> This code looks like it will initialize an array and then print out its
> contents without any problems.
>
>
> You really need to rethink what you're trying to do here. You should
> sketch out on paper first every single step that you need to do to sort
> an array of chars. Make sure you test out your idea on paper before you
> write it in code by running through your algorithm in your head.
>
> I promise you that efficiently sorting a list of chars is harder than
> you think. At some point you should buy a used Intro to Comp. Sci. book
> so that you can read up on basic sorting algorithms. In the meantime,
> you might want to google "bubble sort", "quick sort", and "merge sort".
> These are all algorithms I learned in an intro CS class and they will
> all expand your mind. What you're doing is closest to a bubble sort.
>
> --
> |\/| /| |2 |<
> mehaase(at)sas(dot)upenn(dot)edu
- Next message: Craven: "Re: just getting started, setting up.."
- Previous message: Olivier Merigon: "TCP client application does detect network failure"
- In reply to:(deleted message) Mark Haase: "Re: Sorting the Arrays"
- Next in thread: Andrew Thompson: "Re: Sorting the Arrays"
- Reply: Andrew Thompson: "Re: Sorting the Arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|