Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Sorting array into an index array?
Author Message
Homer_simpson

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...

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

 code: # quick example in Ruby unsorted_array = [2.3, 4.2, 6.5, 1.2] sorted_array = unsorted_array.sort indexes = [] sorted_array.uniq.each { |sorted_element|    unsorted_array.each_with_index { |unsorted_element, index|       indexes.push(index) if unsorted_element == sorted_element    } } puts indexes.join(", ")

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.

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)

all the basic sorting algorithms w/ source

[url]
http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
[/url]
Dan

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 Canada Help 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?

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 Canada Help 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 Canada Help 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 #include int main() {    using namespace std;    const int LOA = 4;    double array[LOA] = { 5.0 , 2.3 , 3.2 , 1.7 };    double sorted[LOA];    copy(array, array + LOA, sorted);    sort(sorted, sorted + LOA);    double * end_of_unique = unique(sorted, sorted + LOA);    int indexes[LOA];    int counter = 0;    for (double * i = sorted; i != end_of_unique; i++)       for (double * j = array; j < array + LOA; j++)          if (*i == *j)             indexes[counter++] = j - array;    for (int * i = indexes; i < indexes + LOA; i++)       cout << *i << endl;    cout << endl;    for (int * i = indexes + LOA - 1; i >= indexes; i--)       cout << *i << endl;    return 0; }
Dan

Posted: Fri Mar 05, 2004 10:12 pm   Post subject: (No subject)

Homer_simpson wrote:
nice nice... veryuseful..
but i still think my sort method is more efficient...

what type of sort u using? i whould have thougth java whould have used very fast sorting for there api thingy
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Homer_simpson

Posted: Sat Mar 06, 2004 12:01 am   Post subject: (No subject)

fast quick sorting... mixture of quicksorting and insertion sorting... it's pretty damn quick...
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: