|
|
|||||||
| Home | Register | Downloads | FAQ | Members List | Calendar | Arcade | Mark Forums Read |
» Less advertising throughout
» Post and participate in discussions
» Network with other forum members
» Free private messaging
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
dVM for short
![]() ![]() ![]() ![]() Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
|
I'v waisted 2 hours of my life.
Thats right. 2 miserable hours trying to make a decent sorting algorithm in Java. I don't know, but I tried 3 times, and each time I gave up in the mess of for loops nested in each other. Could anyone help me by sharing some ideas/code or something for sorting arrays in alphabetical order java? Last edited by dynamicVoidMain; October 12th, 2006 at 14:50.. |
|
|
|
| Advertisement | [Remove Advertisement] | ||
|
|
|
#2 |
|
Emulation to the max!
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jan 2004
Location: Canada
Posts: 2,560
|
what do you mean alphabetical order, are there strings or characters in the array? BTW java has some builtin sorting functions.
__________________
Q6600 Core 2 Quad || Soundblaster X-Fi Extreme HD || e-VGA 8800GTX || 4096mb 4-4-4-12-2T Dual-Channel RAM || MSI P6N Diamond || 2x 320GB Hardrive || 5.1 Speakers || Remote control || 8x USB 2.0 || DVD/CD ±RW Drive || 1x IEEE1394 www.coolsvilleman.homedns.org Normally down. E-mail me for a DVD @ coolsvilleman@gmail.com
|
|
|
|
|
|
#3 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
uhh... well normally I'm against making other people homeworks, but you might want to take a look at some easy sorting algorithms like Bubblesort, Insertion sort or others.
|
|
|
|
|
|
#4 | ||
|
dVM for short
![]() ![]() ![]() ![]() Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
|
Quote:
Quote:
|
||
|
|
|
|
|
#5 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Well in that case what I'd do is creating a little function that can compare two strings to see which ones goes first, and then just using that with one of the sorting algorithms that I mentioned
|
|
|
|
|
|
#6 |
|
Registered User
![]() ![]() ![]() ![]() Join Date: Jan 2003
Location: Europe / Germany
Posts: 902
|
sorting? dunno u could try something with getting the ASCI code of each letter and try to sort using that (to apply bubble or soemhting else on that should be easy) i hope i got u right BTW: 2 h wasted LOL if u just started with programming...well here some news for u: programming takes time ![]() what do u think why e.g. PCSX2 took 6 years to its current status? Because the devs were lazy and did not work 24h a days 7 days a week.. no i think not , i guess "because programming needs" some time And if ur not an "Einstein" then it will take even more then 2 h agan to get it workin. BUT the experience u get IF u get it working SHOULD be worth it. (i guess) Einstein said, and IMO hes right: Experince is knowledge, everything else is just information (from gnemu startside) So good luck! just paste some code in here, or try the google search code we might help u ![]() wbr Shin Gouki |
|
|
|
|
|
#7 | ||
|
dVM for short
![]() ![]() ![]() ![]() Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
|
Quote:
Quote:
![]() I'm getting a lot of experience doing it though, so its a win-win situation. I just hope I do get it to work eventually because I know it'll make me really happy (feeling of pety accomplishment no doubt :P) ![]() edit: Ok, so I dropped the project for a little while, I just got back on it. All I did was change the position of where I was incrementing some variables, I also deleted one variable assigment at a certain point in the flow, and BAM! It worked I didn't get it to work in one go, took some tweaking here and there, I found messing with something gave me far more pleasing results, with some more messing of it I eventually got what I wanted.I knew the algorithm I had in mind would work, and I knew the code I had should have been working, and it did, I just removed one variable assimgent and changed where another variable was being incremented and it worked ![]() Oh wow, I feel so proud (in a crappy, I'm so useless a n00b programmer sort of way )
Last edited by dynamicVoidMain; October 12th, 2006 at 23:18.. |
||
|
|
|
|
|
#8 |
|
NextGenerationGaymulation
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2004
Location: Sweden ( Proud Game Developer )
Posts: 2,775
|
Few things is as satisfying for a programmer as getting an idea to work! Congratz!
|
|
|
|
|
|
#9 |
|
Registered User
![]() ![]() ![]() Join Date: Mar 2006
Posts: 415
|
I wouldn't say that you wasted two hours, but more like you learned ways not to solve the problem for two hours That's the way I look at it when scripting in Ruby, ...if I spend two hours trying to get something to work and it doesn't, I know from then on not to try those methods anymore and find a better way
|
|
|
|
|
|
#10 |
|
dVM for short
![]() ![]() ![]() ![]() Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
|
yeah suppose so.. but still... gah, I'm just glad it worked really, I mean when it did I was like 'holy crap! that came out of no where!', I hadn't canged the code much, just a little tweak here and there, then suddenly its sorting everything out! |
|
|
|
|
|
#11 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2001
Location: Montreal, Canada
Posts: 8,090
|
what sort of sorting algorithm you've ended up using? I've looked through some old java stuff i did for school and here are a few ways we had to do : (sorry for the french names and all... and i don't gauranty the accuracy of this code since its old, and i dont feel like going through it )Code:
public static void trier( int table[] ) {
int nbElements = table.length;
int temp=0;
int j;
for (int k=1;k<nbElements;k++){
temp = table[k];
j = k-1;
while (j>=0 && temp < table[j]) {
table[j+1] = table[j];
if (j==0){
table[j]=temp;
}
j--;
}
table[j+1] = temp;
}
}
Code:
public static void trier( int table[] ) {
int nbElements = table.length;
int tableCompteur[] = new int[nbElements];
int tableTrier[] = new int[nbElements];
int indice=0;
for (int k=0;k<nbElements;k++){
tableCompteur[k] = 0;
tableTrier[k] = 0;
}
for (int k=0;k<nbElements;k++){
for (int j=k; j<nbElements;j++){
if (table[k]>=table[j]){
tableCompteur[k]+=1;
}else{
tableCompteur[j]+=1;
}
}
}
for (int k=0;k<nbElements;k++){
indice = tableCompteur[k];
tableTrier[indice] = table[k];
}
for (int k=0;k<nbElements;k++){
table[k] = tableTrier[k];
}
}
Code:
public static void trier( int table[] ) {
int nbSwitch = 0;
int temp;
int fin = table.length;
do {
nbSwitch = 0;
for (int k=0;k<fin-1;k++){
if (table[k+1]<table[k]){
temp=table[k];
table[k]=table[k+1];
table[k+1]=temp;
nbSwitch+=1;
}
}
fin-=1;
} while (fin!=1&&nbSwitch!=0);
}
__________________
|
|
|
|
|
|
#12 |
|
Administrator
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 9,439
|
|
|
|
|
|
|
#13 |
|
NextGenerationGaymulation
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Sep 2004
Location: Sweden ( Proud Game Developer )
Posts: 2,775
|
Quicksort works fine for strings too, just as long as you know what one string is bigger than the other means. ( This could be length... or first letter, or the total ASCII numbers combined or whatever )
|
|
|
|
|
|
#14 | |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2001
Location: Montreal, Canada
Posts: 8,090
|
Quote:
we were still newbies at that time
__________________
|
|
|
|
|
|
|
#15 |
|
dVM for short
![]() ![]() ![]() ![]() Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
|
Yeah, sorting integers is alot easier than strings. With a string you use a.compareTo(b) whcih returns a negative value if a is less than be and a positive one if its the opposite. I thought I could make it work, but didn't turn otu that way. Basically what I did was startw ith the first word, and work my way down the list comparing, ifcompareTo returned a negative value I exchanged their positions and went back to the top of the list. I know this is sa real algorithm with a real name, but the name escapes me
|
|
|
|
|
|
#16 |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2001
Location: Montreal, Canada
Posts: 8,090
|
recursive functions come to think of it, i do beleive we made one using the .compareid have to look through my folders
__________________
|
|
|
|
|
|
#17 | |
|
dVM for short
![]() ![]() ![]() ![]() Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
|
Quote:
|
|
|
|
|
|
|
#18 |
|
Knowledge is the solution
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Pittsburgh, US. Previously in Mexico City
Posts: 7,160
|
Hmm... you can create arrays with dynamic sizes, albeit you can't change their size once they are created if that's what you refer to. C: int myArraySize = ||| some number you get elsewhere |||; char* myArray = malloc(sizeof(char) * myArraySize); or int[] arr = new int[someNumber]; in java or c++ Indeed with Linked Lists and the like you can attach a new element as many times as you want, however because of the secuential nature of these data structures it makes them unpractical for large data sizes... it all depends on your needs I guess... |
|
|
|
|
|
#19 |
|
dVM for short
![]() ![]() ![]() ![]() Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
|
yeah, and the type of questions they'll be asking on the exam ![]() I do need to change it on the fly, I could use vectors except the lecturer made it clear such questions wouldn't be on the exam, if it was going to be about a linked list, it would be about making them. Anyway, right now I'm busting caps trying to work out virtual functions in C++ , atleast its slightly easier now cos the problem is rpetty much out of a text book
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|