![]() |
Sorting Data: Selection Sort
A little over a year ago I released an example of how to use the Bubble Sort algorithm to sort a set of data, which can be found here.
I started my Freshman year at my University in the Fall, and for the Programming I course we're learning OOP via Java (going to be a CS Major). The sorting algorithm that we were taught was the Selection Sort method, which is more efficient than the Bubble Sort method; and considering the fact that I posted the Bubble Sort method, it only fits that I show you the Selection Sort method as well. I programmed it in Java since it's the only thing that I could use at the moment to test the code, but the method itself is the same regardless of the language so it shouldn't be a problem to understand how to convert it to GScript for whomever might use it. SelectionSort.java: PHP Code:
TestSelectionSort.java: PHP Code:
Quote:
The above code sorts from least to greatest. To convert from greatest to least, switch: PHP Code:
PHP Code:
NOTE: I used the same set of values that I used for the Bubble Sort algorithm. |
Had to do something like this last week or so, except I went the lame way and used Collections.sort(). Of course I messed up after it sorted it by 1 12 13 2 3 4. Should re-do it using your logic here, thanks. :]
|
I thought we were not allowed to post non-GScript scripts in the Code Library. :noob:
|
I converted it to GScript so that it can be tested by Graal developers.
PHP Code:
Quote:
|
Very nice, I will likely end up using this somewhere.
|
Quote:
My apologizes if you can't, but I haven't played Graal in quite some time so I wouldn't have a method of testing the code since GScript is fabricated and has no standalone compiler. It's not so much that I wouldn't be able to do it in GScript myself, but I'd rather release something that I was certain was error proof rather than just writing code in some text file and assuming everything in it will compile and run. From an educational standpoint, the importance is learning the Selection Sort algorithm so that you can apply it to any programming language. Quote:
Thank you. :) |
Quote:
|
A good example for new scripters :). I have a feeling that you'll be back for one of the O(n log n) algorithms.
Quote:
|
Quote:
|
Good efficient code there :) Good job
|
If you're looking for something even a bit more efficient, I would look up the quicksort algorithm.
|
Quote:
Yeah, that would also fall under the O(n log n) that napo addressed. I believe that the algorithms course that we have over here is an upper-division course, so I suppose I'll get into the higher-level sorting methods as I progress into the major. |
Quote:
|
If you need an algorithm then invoke the power of Wikipedia.
|
Quote:
Well I'm taking Programming II with Discrete Math next semester (Discrete Math being a corequisite), so it's possible. I've already read through all of the notes for my Programming I course so I know that the only sorting algorithm that we're supposed to be taught in this class is the Selection Sort method, so I simply have to look on ahead and hope the next course offers a wider range of options. |
I'd suggest learning Python, its a great programming and scripting language. With Python and C++ together you can't loose.
Python is also open-source, allowing you to create C/C++ extensions for Python or embed Python into a C/C++ application which is what I'm doing. Python Code: PHP Code:
Quote:
And you're making global variables. |
Quote:
PHP Code:
|
Quote:
PHP Code:
Python is superior though. :D |
HTML Code:
temp.numbers = {1,4,6,2,65,20,200,4000,40,200}; |
Quote:
Quote:
Plus, that's definitely not Selection Sort. >_< |
I don't understand why they still waste time with classics like these :P The concept of hashing has pretty much crushed any algorithm that didn't use it before.
|
Quote:
|
| All times are GMT +2. The time now is 06:52 PM. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.