Emuforums.com

Go Back   Emuforums.com > General Discussion > Web development / Programming
Home Register Downloads FAQ Members List Calendar Arcade Mark Forums Read

WON'T YOU JOIN US?
You are not a registered member and
are viewing this site as a guest.
Registration is simple and FREE.
Join this CrowdGather community today.
Registration offers the following perks:

» Less advertising throughout
» Post and participate in discussions
» Network with other forum members
» Free private messaging

join

Reply
 
Thread Tools Display Modes
Old October 12th, 2006, 14:42   #1
dynamicVoidMain
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..
dynamicVoidMain is offline   Reply With Quote

Advertisement [Remove Advertisement]
Old October 12th, 2006, 14:51   #2
Coolsvilleman
Emulation to the max!
 
Coolsvilleman's Avatar
 
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
Coolsvilleman is offline   Reply With Quote
Old October 12th, 2006, 15:13   #3
Proto
Knowledge is the solution
 
Proto's Avatar
 
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.
Proto is offline   Reply With Quote
Old October 12th, 2006, 15:20   #4
dynamicVoidMain
dVM for short
 
Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
Quote:
Originally Posted by Coolsvilleman View Post
what do you mean alphabetical order, are there strings or characters in the array? BTW java has some builtin sorting functions.
Strings. I know, its in java.util.Array, but I'd rather not use that.


Quote:
Originally Posted by Proto View Post
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.
While I do learn java at uni this isn't homework
dynamicVoidMain is offline   Reply With Quote
Old October 12th, 2006, 17:21   #5
Proto
Knowledge is the solution
 
Proto's Avatar
 
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
Proto is offline   Reply With Quote
Old October 12th, 2006, 17:31   #6
Shin_Gouki
Registered User
 
Shin_Gouki's Avatar
 
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
Shin_Gouki is offline   Reply With Quote
Old October 12th, 2006, 17:46   #7
dynamicVoidMain
dVM for short
 
Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
Quote:
Originally Posted by Proto View Post
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.
Quote:
Originally Posted by Proto View Post
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
yeah thats the idea, but it really isn't working. Silly code

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..
dynamicVoidMain is offline   Reply With Quote
Old October 15th, 2006, 02:33   #8
ChankastRules
NextGenerationGaymulation
 
ChankastRules's Avatar
 
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!
ChankastRules is offline   Reply With Quote
Old October 15th, 2006, 02:44   #9
GaveUpTomorrow
Registered User
 
GaveUpTomorrow's Avatar
 
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
GaveUpTomorrow is offline   Reply With Quote
Old October 15th, 2006, 07:14   #10
dynamicVoidMain
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!
dynamicVoidMain is offline   Reply With Quote
Old October 15th, 2006, 23:00   #11
Chrono Archangel
 
Chrono Archangel's Avatar
 
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);
	}
Chrono Archangel is offline   Reply With Quote
Old October 15th, 2006, 23:35   #12
Kurbster
Administrator
 
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 9,439
All 3 of those are only able to sort integers. Unfortunately for strings, it's not that easy

The only other thing I can think of is converting the first letter into a hexadecimal value and comparing that.

Or just use the method in java.util.Array, sheesh
__________________



Kurbster is offline   Reply With Quote
Old October 16th, 2006, 00:07   #13
ChankastRules
NextGenerationGaymulation
 
ChankastRules's Avatar
 
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 )
ChankastRules is offline   Reply With Quote
Old October 16th, 2006, 02:33   #14
Chrono Archangel
 
Chrono Archangel's Avatar
 
Join Date: Dec 2001
Location: Montreal, Canada
Posts: 8,090
Quote:
Originally Posted by Kirby View Post
All 3 of those are only able to sort integers. Unfortunately for strings, it's not that easy

The only other thing I can think of is converting the first letter into a hexadecimal value and comparing that.

Or just use the method in java.util.Array, sheesh
of course, our assignments were for integers only we were still newbies at that time
Chrono Archangel is offline   Reply With Quote
Old October 16th, 2006, 04:46   #15
dynamicVoidMain
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
dynamicVoidMain is offline   Reply With Quote
Old October 16th, 2006, 05:14   #16
Chrono Archangel
 
Chrono Archangel's Avatar
 
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 .compare

id have to look through my folders
Chrono Archangel is offline   Reply With Quote
Old October 16th, 2006, 07:15   #17
dynamicVoidMain
dVM for short
 
Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 848
Quote:
Originally Posted by Kirby View Post
All 3 of those are only able to sort integers. Unfortunately for strings, it's not that easy

The only other thing I can think of is converting the first letter into a hexadecimal value and comparing that.

Or just use the method in java.util.Array, sheesh
while I can use arrays/vectors I'd rather practice with linked lists as my end of year exam will probably be asking I create one, besides, arrays aren't dynamic data structures so they can't do everything linked lists can
dynamicVoidMain is offline   Reply With Quote
Old October 18th, 2006, 16:46   #18
Proto
Knowledge is the solution
 
Proto's Avatar
 
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...
Proto is offline   Reply With Quote
Old October 18th, 2006, 19:15   #19
dynamicVoidMain
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
dynamicVoidMain is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +1. The time now is 13:24.

© 2006 - 2012 Emu Forums | About Emu Forums | Advertisers | Investors | Legal | A member of the Crowdgather Forum Community


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.