Posted: Wed Mar 03, 2004 7:00 pm Post subject: Sorting array into an index array?
ok let's say i got this array of decimal numbers like so:{5.0 , 2.3 , 3.2 , 1.7 }
now i want to have another array of four which contains the sorted out index of the first array like this: { 4 , 2 , 3 , 1 }
so whenever i print array1[array2[from 1 to 4]] i would get the numbers in order...
i have a coupla methods in my mind in how to do it... i was wondering if anybody knew a quick and efficient method...
Sponsor Sponsor
wtd
Posted: Wed Mar 03, 2004 7:15 pm Post subject: (No subject)
You could use a standard quick sort or such to sort the array, then...
create an array of indexes
for each unique element in the sorted array
for each element in the original array
if the latter equals the former
push the index of the latter onto the end of indexes
wtf?
i believe that's in basic...
the reason i posted it here was that i'm speaking in general...
i already know bubble sort and linear.. looking for something more efficient...
shorthair
Posted: Wed Mar 03, 2004 8:04 pm Post subject: (No subject)
ruby ive posted in your other forum and PM you , this section is pseudo code ,no set language ,
wtd
Posted: Wed Mar 03, 2004 8:22 pm Post subject: (No subject)
shorthair wrote:
ruby ive posted in your other forum and PM you , this section is pseudo code ,no set language ,
Interesting. I haven't received a private message.
I did in fact offer psuedo-code. I didn't put it into code tags in this case for fear of the layout mangling that's occurred in other threads.
I simply offered some real, working code in addition so that the original poster could see that it works, and possibly play with the code, if he felt like it. It just happens that Ruby is the language I could code it fastest in, and one which I feel is rather expressive.
I don't feel bad for using this forum for tutorials on other languages. Being exposed to other programming languages is a good thing, and no other forum seems appropriate. I like to sharemy knowledge, and frankly, this forum doesn't seem too active otherwise.
If you don't like it, simply don't read the threads. :shrug:
Homer_simpson
Posted: Thu Mar 04, 2004 11:23 pm Post subject: (No subject)
well dude there's no array.sort command in language i'm using...
Catalyst
Posted: Thu Mar 04, 2004 11:25 pm Post subject: (No subject)
Posted: Fri Mar 05, 2004 12:38 pm Post subject: (No subject)
Homer_simpson wrote:
well dude there's no array.sort command in language i'm using...
what language are u using? there is an array sort in java (i think thats what u are using from u calling it a method)
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Homer_simpson
Posted: Fri Mar 05, 2004 6:04 pm Post subject: (No subject)
THX catalyst awesome sorting methods!!!
and dan... i'm using java how do u use the array sort in java and is it fast?
Sponsor Sponsor
Dan
Posted: Fri Mar 05, 2004 6:56 pm Post subject: (No subject)
i think so, and i think it is like this:
code:
import java.util.*;
public class temp
{
public static void main(String args[])
{
int mynum[] = {1,2,3,8,7,6,4,3};
Arrays.sort(mynum);
}
}
and it is a speed of n*log(n)
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
rizzix
Posted: Fri Mar 05, 2004 7:00 pm Post subject: (No subject)
speakin of arrays. i think i should metion it here at the least. to improve performance in copying the contents of one array into the other use the optimized system command:
static void System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Dan
Posted: Fri Mar 05, 2004 7:09 pm Post subject: (No subject)
then i think i will add:
fill(int[] a, int val)
so polep can stop puting in thous for loops to set all the array elements to 1 or w/e
tried doesn't work...
Computer Science CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Homer_simpson
Posted: Fri Mar 05, 2004 8:46 pm Post subject: (No subject)
nice nice... veryuseful..
but i still think my sort method is more efficient...
wtd
Posted: Fri Mar 05, 2004 9:04 pm Post subject: (No subject)
Just so I can be the guy to give the C++ example...
Note: The sort algorithm by default sorts in ascending order. If you want descending, just print them out in reverse order, as I hae done (first output is ascending, second is descending).
code:
#include <algorithm>
#include <iostream>
int main()
{
using namespace std;
const int LOA = 4;
double array[LOA] = { 5.0 , 2.3 , 3.2 , 1.7 };
double sorted[LOA];