Computer Science Canada I'm lost - Sorting |
Author: | Tallguy [ Fri Feb 06, 2009 10:17 am ] | ||||
Post subject: | I'm lost - Sorting | ||||
Hey guys, its been a while since I was programming and i'm back in class. I always found the concept of sorting to be difficult so i'm asking if anyonme could explain the code below.
I understand the arrays and getting the numbers and all, i don't get how to to sort them, i got this code from turing help. Can anyone please explain this in a simple way? Thanks. Mod Edit: Syntax tags, not quote tags!
|
Author: | DemonWasp [ Fri Feb 06, 2009 10:50 am ] |
Post subject: | RE:I\'m lost - Sorting |
Here's a hint: the program works as long as you don't enter any numbers over 998. If you enter 999, 1000, or anything higher, it fails because "where" is never assigned. The sorting algorithm implemented is a fairly simple sort. All it does is finds the lowest number over and over and over again, putting it into a new array. If you take a careful look at the inside of the first for loop, you'll see it trying to find the lowest number still in the list: it assumes the smallest number will be less than 999, and if it sees anything smaller than 999, it takes that as its new "smallest" and keeps searching. Once it's found what it thinks is the smallest in the array, it moves that to the sorted array and puts in the flag value of 999 in the array. The problem comes in if you have, say, 5000 in your array. The algorithm will never find 5000 as the smallest because it keeps setting things to 999, which is decidedly less than 5000(!). So when it's moved all the other numbers outside, it freaks out because the "where" variable never got set to anything, so it has no value. If you just change the 999 to maxint in both cases it works for any integer up to the maximum of 2^31-1 (the most you can represent in a signed 32-bit integer, which is what Turing uses by default). Aside: this method of sorting is quite inefficient, as it consumes O ( N ^ 2 ) computation time and O ( N ) additional space. Not a problem in this case, but if you have more than 1000 numbers it could easily take essentially forever. P.S. Don't forget to use [ syntax ] tags or at the very least some [ code ] tags. |
Author: | Zren [ Fri Feb 06, 2009 9:43 pm ] | ||
Post subject: | Re: I'm lost - Sorting | ||
I just learned another sorting algorithm the other day that works for no matter how high it is. It's basically the same as yours except a few different things. Forgive me if I'm wrong explaning this, this is how I interpreted it.
|
Author: | DarkRider [ Sat Feb 07, 2009 12:08 pm ] | ||||
Post subject: | Re: I'm lost - Sorting | ||||
To make bubble sort more efficient I would suggest only checking the values that need to be checked and not the entire list:
You could also use a variant on bubble sort called "ripple" sort. Instead of just checking the list from beginning to ending you could also check the list from ending to beginning:
If anyone needs a more in-depth explanation of those two algorithms please let me know and I'll explain them better. For other more efficient algorithms check out this Wikipedia article. It lists a bunch of possible sorting algorithms that could be used, and explains how they can be implemented. You could also check out this thread that contains a bunch of already implemented algorithms using Turing. |
Author: | Tallguy [ Mon Feb 09, 2009 12:03 pm ] | ||
Post subject: | Re: I'm lost - Sorting | ||
Hey thanks for the feed back, i found this one that works with my program.... Like this is my assignment Quote: /*1. Create a program which prompts the user for a student's name and their mark in a test
which is marked out of 67. Use a sentinel to determine the end of input. Output the marks in chart form (Name and Mark) in the order input as a percent. Sort the data in descending order according to marks and redisplay the sorted list in chart form. Save as lastname_array#1.t.*/ now i got everything except that i don't know how to connect ttwo arrays so that there linked like 1a =2a 1b=2b etc. this is what i got so far
how do i link the two arrays together? does anyone know what i'm talking about? the name and marks have to correspond |
Author: | DemonWasp [ Mon Feb 09, 2009 1:01 pm ] |
Post subject: | RE:I\'m lost - Sorting |
You're talking about "associated arrays". All you need to do is ensure that the swaps you do in your sorting method swap values in both arrays in the same manner. Wherever you would swap locations i and j of array1, you need to ALSO swap locations i and j of array2. This will keep the associations correct. If you plan to continue with programming, I would recommend looking into Turing's records. Instead of having array1, array2, array3 and so on, you could have array of objects that each have fields 1, 2, and 3. It may not sound like much of a difference, but it's actually a huge improvement in readability, among other things. |
Author: | Tallguy [ Mon Feb 09, 2009 1:05 pm ] |
Post subject: | RE:I\'m lost - Sorting |
i would, but i have to use arrays for this assignment... |
Author: | DemonWasp [ Mon Feb 09, 2009 3:24 pm ] | ||||
Post subject: | RE:I\'m lost - Sorting | ||||
That's exactly it, you use arrays of records, instead of arrays of the basic types (int, char, real, string, etc) So instead of:
you'd have
Either way, I did tell you how to solve the problem with your existing primitive-arrays. |
Author: | Tallguy [ Wed Feb 11, 2009 8:35 am ] |
Post subject: | Re: I'm lost - Sorting |
Thanks DemonWasp, i was going to do it the way you mention, but wasn't allowed to do so.....(don't ask) Thanks all of you for helping me in this assignment, posted below is my finised code....... |
Author: | Insectoid [ Fri Feb 13, 2009 12:32 pm ] |
Post subject: | RE:I\'m lost - Sorting |
After a bit, sorting becomes rather easy. One day it will 'click' and you will wonder why you didn't see the answer before. I personally prefer ripple or 'swap' sorting, as it's really fast to code, albeit slow to run. |