Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
array in deascending order

Posted by RKM (RKM), 28 May 2003
Hi
i am here again with another question  
if I have the following array:
static float [] in1 = { 0,0,0,80,0,0,100,0,0,90,0,0,70,60};
first I want  to sort it  so I will have as the following array  {100,90,80,70,60,0,0,0,0,0,0,0,0,0}
Then I want an array of the positions of  the first ordered values in the original array
I mean that ,I must have the following
{7,10,4,13,14,0,0,0,0,0}

How can I
1.sort the array (in deascending order)
2.build an array of the positions of the first 10 ordered value (its position in the original array).



Posted by admin (Graham Ellis), 28 May 2003
Here are the results you're looking for then (remember that Java gives array positions starting at 0):

Code:
[localhost:~] graham% java Open
100   6
90   9
80   3
70   12
60   13
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0
[localhost:~] graham%


And here's an example of the code to do it;   I've made heavy use of Java Utility classes as I really should be encouraging to use those rather than write your own maths routinse, but you may find a shorter way ...

Code:
import java.util.*;
public class Open {
public static void main (String [] args) {
       int [] in1 = { 0,0,0,80,0,0,100,0,0,90,0,0,70,60};

// Remember Original Positions, giving precedence to earlier occurrence
       ArrayList Values = new ArrayList();
       HashMap OriginalPosition = new HashMap();
       for (int i=in1.length-1; i>=0; i--) {
               OriginalPosition.put(new Integer(in1[i]),new Integer(i));
               Values.add(new Integer(in1[i]));
               }

// Sort incoming array
       Collections.sort(Values);

// List out sorted values in reverse order, with position numbers
       for (int i=Values.size()-1; i>=0; i--) {
               int val = ((Integer)(Values.get(i))).intValue();
               System.out.print("" + val + "   ");
               System.out.println(OriginalPosition.get(new Integer(val)));
               }
       }
}






This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho