GameTalk Help | Feedback
Complaints | Requests




Welcome to GameTalk! Enter a word or two from the title of your favorite game:

Then click Search to find the forums where you can get help and exchange tips for those games.
Sample Searches (click to try them): pokemon | mario | zelda | baseball | star wars | wii sports
Hint: The newest games are listed first, so scroll down to find forums for older games.


GameTalk   Requests Complaints Feedback Mod Info GT Help
by Request   RPG Chat Metroid Chat | Metroid Games Jedi Sith | Halo Chat Middle Earth The Pointless Forum | duh
by Request   badlands | Axem Mario | Yoshi | Game Dev Sports | Superbowl Homework | College FanFics | Writers
Game Genres   Fantasy | Sci Fi Wrestling | E-Fed Dueling Cards Classic Games Virtual Pets
Popular Worlds   Pokémon | Pokémon Games Star Wars | Star Wars Games Zelda | Zelda Games Harry Potter | Sonic Digimon | Dragonball
New Platforms   Xbox 360 PlayStation 3 Nintendo Wii Nintendo DS | WFC | PSP Game Slackers
New Platforms   Wii vs PS3 vs 360 GameCube vs PS2 vs Xbox Nintendo DS vs Sony PSP PC Games | Mac Games Phone & Smart Devices
Other Media   Music Movies TV | [adult swim] Books | Graphic Novels Anime
Special Topics   World Life | Spirituality Peace | Holiday Future | Past Depression | Health


 Questions  Your Reviews  Tips  Polls  Chat
View Questions
Ask One
See Reviews
Write One
Best | All Tips
Give One
See Polls
Start One
View Chat
Post One


Homework

For Homework Help & Encouragement

Apply to be the New Moderator of this Forum
Posted by: halolegend1
Apr 4, 2008  (39 days and 13 hours ago)
Question for DeWayne Mann or anyone familiar with Java

What is the fastest java sort for an array of integers that you know of? Thanks!

There are 25 Replies:
Message Person and Time

sorting algorithm*

halolegend1
Apr 4, 2008
(39 days and 12 hours ago)

I'm putting my money on introsort right but I'm not sure if there is anything faster.

halolegend1
Apr 4, 2008
(39 days and 12 hours ago)

right now, but*

halolegend1
Apr 4, 2008
(39 days and 12 hours ago)

OOOOH, you hit on one of my favorite topics!

Introsort is a combination of two very good sorts, quicksort & heapsort. Most people consider quicksort to be the fastest, and it typically is very fast. It does have some weaknesses, and so it's not one of my favorites.

On the other hand, heapsort IS one of my favorites. It's usually as fast as quicksort, without the weaknesses. It's a more complicated algorithm, though.

Finally, my favorite sort is mergesort. It's the "fastest", but that is a bit misleading: it typically performs less comparisons than either of the other two (which is the standard test of speed), but it uses extra memory, and so takes actual physical time to access memory a lot.

So, in theory, mergesort, but since you're probably asking about physical time, introsort is just fine.

By the way, you can look up sorting algorithms on wikipedia, there's a whole collection of them: http://en.wikipedia.org/wiki/Category:Comparison_sorts . I don't know how much you know about big-O notation, but any comparison sort with O(n log n) is VERY good, and, in fact, is the fastest ANY comparison sort can be.

DeWayne Mann
Apr 4, 2008
(39 days and 8 hours ago)

  • hits dewayne mann over the head with hammer*

    The fastest way to sort an array of integers in java is to do it C++.

    Everone loves it!

    Everyone.

  • Mikau
    Apr 4, 2008
    (39 days and 3 hours ago)

    Well then, by that logic, it's fastest to do it in Python.

    DeWayne Mann
    Apr 4, 2008
    (39 days and 3 hours ago)

    nuh uh! with python you're just going to keep crashing into the ceiling!

    Mikau
    Apr 4, 2008
    (39 days and 3 hours ago)

    You can just import open sky.

    DeWayne Mann
    Apr 4, 2008
    (39 days and 3 hours ago)

    ^^^^ this one needs to be done in Java.

    Thanks DeWayne Mann, very big help. I think I will go with intro for the sake of physical time as you mentioned. It's an assignment the prof is grading as a contest, the fastest sorts get extra credit.

    I have learned enough about big-O to avoid the n^2's.

    Do you know a site that gives an example of introsort code? If not, no worries, I'll dig for it, but if you know one off hand I'd love to know, because wiki doesn't seem to have it like they do for the other sorts.

    halolegend1
    Apr 4, 2008
    (39 days and 0 hours ago)

    Well, you should be able to find code for quick sort & heap sort. Then, all you do is, after running quick sort a certain number of times (and that's up to you), switch to heapsort.

    So, in psuedocode, it would be like:

    def introsort: { for (int i = 0; i

    DeWayne Mann
    Apr 5, 2008
    (38 days and 14 hours ago)

    Oops, I'm dumb.

    def introsort{

    for (int i = 0; i < 10;i++){

    run a quicksort pass}

    while (unsorted) {

    run heapsort}

    }

    DeWayne Mann
    Apr 5, 2008
    (38 days and 14 hours ago)

    Awesome, thanks.

    halolegend1
    Apr 5, 2008
    (38 days and 11 hours ago)

    I should add that you should play around with the value in the for loop. I picked 10 at random.

    The general idea is, while quicksort is normally good, it is VERY slow on "nearly sorted" input. There are a number of workarounds for this, but none of them are perfect.

    On the other hand, heapsort runs pretty fast on "nearly sorted" input. Thus, you want to run quicksort until the input is nearly sorted, then switch over to heap to run on that.

    There is an alternative method, which is what I personally do when I have to use quicksort. It's like introsort, only, instead of using heapsort at the end, I use insertion sort. It's the same idea: insertion sort is typically very bad....except on nearly sorted input, in which case it is VERY good. You may want to try that, as well.

    DeWayne Mann
    Apr 5, 2008
    (38 days and 11 hours ago)

    Ah... sounds good, will do. I'll try a couple different things and see how they compare.

    halolegend1
    Apr 5, 2008
    (38 days and 5 hours ago)

    Alright DeWayne Mann, I have another question for you if you don't mind. I'm still pretty new to coding so I'm probably about to embarrass myself, but anyways:

    How exactly would I indicate "unsorted"?

    Because I was planning on using recursion or something until it was sorted (I'm just learning sorting so I might sound silly)

    halolegend1
    Apr 6, 2008
    (37 days and 3 hours ago)

    ...and I suppose I can't use recursion if I'm using two sorts? I don't know, the examples on wiki were hard for me to understand. If you're sick of helping me I'll buckle down and do some trial and error. :P

    halolegend1
    Apr 6, 2008
    (37 days and 3 hours ago)

    Well, this goes back to big O notation, but a linear search through an array takes O(n) time. Thus, you can safely tack a search on to any sort without adding too much time.

    Thus, the easiest way is to write a method:

    boolean isSorted(Array n) {

    boolean returner = true;

    for (int i = 0; i < n.size-1; i++) {

    if (n[i] > n [i+1]) {

    returner = false;

    }

    }

    return returner;

    }

    BUT, it turns out, you don't even need to do that in this case. Where I wrote "while (unsorted)", that was more to demonstrate what was going on. heapsort will actually sort input on ONE pass (unlike quicksort, which takes many passes to work, though they all individually run quickly). thus, that while loop shouldn't even exist. just run heapsort.

    for insertion sort you might need to do a little checking, though. it depends on how you write it.

    (oh, and sorry if my code is a little wrong, syntax wise. i mostly code in python now, and the syntax there is a LOT different from java, and I keep getting the two confused when I switch back and forth. you get the idea though)

    DeWayne Mann
    Apr 6, 2008
    (37 days and 3 hours ago)

    Oh, you can ALWAYS use recursion. Recursion is our friend!

    It's just not that helpful here.

    DeWayne Mann
    Apr 6, 2008
    (37 days and 3 hours ago)

    Alright cool, I'll get to writing it and let you know if I run into any problems. Thanks again for the help.

    halolegend1
    Apr 7, 2008
    (36 days and 14 hours ago)

    No problem. Like I said, sorting algorithms are one of my favorite topics, so I'm always willing to talk about them.

    DeWayne Mann
    Apr 7, 2008
    (36 days and 12 hours ago)

    intro sort? never heard of it... i know quick and heap tho...

    then again there is a radix sort if you kow the range of values is small...

    wow - both you guys learning to code... and me working for a computer company with one other develpoer and a dba...

    you guy betters get good :p

    duanerama
    Apr 10, 2008
    (33 days and 5 hours ago)

    Duane, I know it's been awhile since we've talked...but I'm graduating with my BA in Computer Science (and Math) in a month.

    And I hate radix sort. Bucket sort is SO much better in that situation :P

    DeWayne Mann
    Apr 10, 2008
    (33 days and 4 hours ago)

    ouch :( the place i work is a startup - not sure we will be hiring anyone that soon =\

    might be good to stay in touch tho :) gl anyway :)

    duanerama
    Apr 11, 2008
    (32 days and 11 hours ago)

    well, I've got an internship for this summer. Programming for the American National Corpus, but I don't know anything more than that.

    here's the website for that: http://www.americannationalcorpus.org/

    DeWayne Mann
    Apr 11, 2008
    (32 days and 11 hours ago)

    Hey duane! Yeah crazy huh...after a couple years of milling about I've finally settled into the comp sci major. I don't know much about radix but I do know the algorithm is going to be tested on a huge array.

    halolegend1
    Apr 11, 2008
    (32 days and 8 hours ago)


  • Add Your Own Reply
  • Back to Index


  • Xbox 360   Halo 3 Call of Duty 2 | Three | Four MW The Elder Scrolls IV: Oblivion Gears of War | Two Rainbow Six Vegas | GRAW
    Xbox 360   BioShock Grand Theft Auto IV Viva Pinata Mass Effect Xbox 360 | Xbox Live Arcade
    XBox   Halo | Halo 2 Fable ES 3: Morrowind | GOTY Doom 3 Star Wars: KOTOR | Two
    XBox   Ninja Gaiden Splinter Cell | Chaos Theory Star Wars: Battlefront | Two GTA 3 & Vice City | San Andreas Xbox Live | XBox Chat
    PC Games   Halo | Age of Empires Spore | BioShock Harry Potter: One | Two | Three Final Fantasy VII | VIII | XI F.E.A.R.
    PC Games   RuneScape | Maple Story Sims Two | University | Nightlife GTA 3 | Vice City | Andreas Diablo II | LOD Rise & Fall: Civilizations at War
    PC Games   Half Life | Two | Counterstrike Morrowind | Tribe | BM | Oblivion Starcraft | Two | Warcraft 3 Guild Wars | Factions PC Chat
    WoW   WoW Main | Profiles | Requests Newbies | Pros | Masters Quests | Lands | Items PvP | Guilds | Classes & Races Dragon Lounge | Moonlit Garden



    PlayStation 3   Metal Gear Solid 4: GotP Call of Duty 3 | Four Grand Theft Auto IV Heavenly Sword Warhawk
    PlayStation 3   Sonic the Hedgehog Uncharted: Drake's Fortune Medal of Honor: Airborne Gran Turismo HD | MotorStorm PlayStation 3 Chat
    PlayStation 2   Kingdom Hearts | Two Final Fantasy: X | X-2 | XI | XII MGS 2 | Substance | Three GTA 3 | Vice City | Andreas WWE S vs R | 2006 | 2007 | 2008
    PlayStation 2   God of War | Two Guitar Hero | Two | 80s | Three Gran Turismo 3 | Four Dragon Quest VIII PlayStation 2 Chat
    PSP   Kingdom Hearts: BBS Dynasty Warriors | Untold Legends Castlevania: The DXC Crisis Core: FF VII Twisted Metal
    PSP   Metal Gear Acid Patapon GTA: Liberty City Stories God of War: CoO PSP Chat
    PSX   Final Fantasy IX | VIII | VII FF Tactics | Origins | Chronicles Digimon World 2 | Three | RPG Legend of Dragoon | Chrono Cross PSX Chat



    Wii   Super Smash Bros. Brawl Fire Emblem: Radiant Dawn The Legend of Zelda: TP Wii Sports | Wii Play | Wii Fit Marvel: UA
    Wii   Mario Kart Wii Pokemon Battle Revolution Guitar Hero III | Aerosmith Metroid Prime 3: Corruption No More Heroes
    Wii   Mario Galaxy | Strikers Charged Soulcalibur Legends Emergency Mayhem Resident Evil 4: WE | Umbrella Chronicles WarioWare
    Wii   Super Paper Mario Rayman Raving Rabbids | Two Dragon Quest Swords Zack & Wiki Wii Chat
    GameCube   Super Smash Bros. Melee Animal Crossing | Two Mario Sunshine | Kart DD Tales of Symphonia Phantasy Star EP I & II
    GameCube   Resident Evil | Zero | Four Super Paper Mario | TT-YD Sonic DX | Two | Heroes Pokemon Colosseum | XD: GoD Fire Emblem: Path of Radiance
    GameCube   HM: AWL | Another WL | MM Pikmin | Two Zelda WW | OoT | Bonus Disc Zelda: Twilight Princess GameCube Chat
    Nintendo DS   Super Mario 64 DS | Mario Kart DS The World Ends With You Final Fantasy III | CC: RoF Animal Crossing: Wild World Legend of Zelda: PH
    Nintendo DS   Pokemon Mystery Dungeon: EoD | EoT Pokemon Diamond & Pearl | Wi-Fi Pokemon Ranger | MD: BRT | Battonage Guitar Hero: On Tour Nintendo DS Chat | WFC
    GameBoy Adv   Pokemon R&S | Ruby | Sapphire Fire Red & Leaf Green | Emerald Advance Wars | Two Fire Emblem | Sacred Stones Zelda: ALttP | Minish Cap
    GameBoy Adv   YuGiOh! EDS | WWE | SC Kingdom Hearts: CoM Golden Sun | Two FF Tactics Advance GBA Chat
    Nintendo N64   Paper Mario | Super Mario | SSB Zelda OoT | Majora's Mask Banjo-Kazooie | Banjo-Tooie GoldenEye 007 | Perfect Dark N64 Chat
    GameBoy   Pokemon: Crystal | G&S | R&B DWM | Tara | Cobi Zelda: OoS | OoA | LA Harry Potter YuGiOh! DDS



    Welcome to GameTalk: Getting Started | More About Us | Mod FAQ & Guidelines | Terms of Use

    Copyright © 1995-2008 GameDex Inc.