Re: Sorting the Arrays

From: Bonux (acerpower_at_servihoo.com)
Date: 07/19/04


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



Relevant Pages

  • Re: Sorting the Arrays
    ... > at the point where I have to sort the Array ... > How to invoke the charSortArray() method to sort the array and print the ... times in a loop, ...
    (comp.lang.java.help)
  • Sorting an ArrayList twice...
    ... What I found out in the Java Doc is that there is no easy solution to sort ... an ArrayList but nevertheless it is possible to sort an array. ... sort routine and the other Arrays.sortfor the second sort ...
    (comp.lang.java.help)
  • Re: Sorting the Arrays
    ... >> at the point where I have to sort the Array ... but you never call charSortArray(). ... > so that you can read up on basic sorting algorithms. ...
    (comp.lang.java.help)
  • Re: Using Java Classes to Sort a Small Array Quickly
    ... Any sort you pick, short of bogosort, would complete in at most a millisecond or two, even on crappy old hardware. ... The standard sort methods are quicksort (used in Java for primitive arrays) and mergesort. ... However, recursion induces a high overhead cost, so when the array size is very small, it just sorts the array using insertion sort. ... Since you commented that your insertion sort beat this method, i am betting that it's the multiple copies that is making it slower than your hand-built sort. ...
    (comp.lang.java.programmer)
  • Re: Newbie: sorting an array of custom objects
    ... In Java, I can implement the Comparable interface to make the Array.sort method do this for me. ... An even more elegant solution would be to able to say to the array 'just put this object where it belongs', without really having to sort the whole array. ...
    (comp.lang.ruby)