Find the size of each element in the array relative to other elements
The question may not be very clear. Here is an example. Assume that an array has three elements: 3 9 2 1 8 3 2, 3 5 2 1 4 3 2, 3 In the output indicates that element 3 is in the third place among all elements of the array, which is larger than 1 2. 5 indicates that 9 is in the fifth place among all elements of the array, that is, the largest. Idea: The first thing we think of is whether we can solve this problem through various sorting methods. We know that the element location information is not retained during sorting, however, the output must be output in the original order of elements in the array. We can make a slight modification on the basis of the Sorting Algorithm to okay. The following implementation draws on the idea of Bubble sorting. Copy the code import java. io. file; import java. io. fileWriter; import java. util. arrayList; import java. util. failed;/* repeat exception class */class RepeatException extends Exception {private static final long serialVersionUID = 1L; static String str = "elements can not repeat !!! ";/* Constructor */RepeatException () {super. printStackTrace (); System. out. println (str) ;}} public class WhatIsMyNumber {/***** @ param al -- ArrayList store elements in original double array * @ param positionInfo -- ArrayList store position information, for example, al = {1, 1} * then, positionInfo = {0, 1}, the second 0 means the relative order of this elements is * store in the first positio N of result (return by computeNumber) * @ return */public static ArrayList <Double> deleteRepeat (ArrayList <Double> al, ArrayList <Integer> positionInfo) {ArrayList <Double> temp = new ArrayList <Double> (); // flag record position information int flag = 0; for (int I = 0; I <al. size (); I ++) {if (temp. contains (al. get (I) {positionInfo. add (temp. indexOf (al. get (I);} else {positionInfo. add (flag); flag + +; Temp. add (al. get (I) ;}} return temp ;} /***** @ param noRepeatArray ArrayList after delete repeat elements * @ return the relative order of every element in noRepeatArray * @ throws Exception throw exception if repeat elements are found in noRepeatArray */ public static int [] computeNumber (ArrayList <Double> noRepeatArray) throws Exception {int [] result = new int [noRepeatArray. size ()]; (Int I = 0; I <result. length; I ++) {result [I] = 1 ;}for (int I = 0; I <noRepeatArray. size (); I ++) {for (int j = I + 1; j <noRepeatArray. size (); j ++) {if (noRepeatArray. get (I) <noRepeatArray. get (j) result [j] ++; if (noRepeatArray. get (I)> noRepeatArray. get (j) result [I] ++; if (noRepeatArray. get (I) = noRepeatArray. get (j) throw new RepeatException () ;}} return result;} public static void outputR Esult (double [] test) {double [] output = new double [test. length]; ArrayList <Double> al = new ArrayList <Double> (); ArrayList <Integer> positionInfo = new ArrayList <Integer> (); for (int I = 0; I <test. length; I ++) {al. add (test [I]);} al = deleteRepeat (al, positionInfo); try {int [] result = computeNumber (al); for (int I = 0; I <output. length; I ++) {output [I] = result [positionInfo. get (I)]; System. out. Print (output [I] + "");} System. out. println ();} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {double [] test = new double [] {8, 8, 2.5, 8, 8, 2.5, 8, 13, 15, 12, 2.5, 15, 2.5, 8, 15}; outputResult (test) ;}copy the code. The main idea of the program is described below. Given an array, suppose it is {, 2,}, the output array is initialized to {,}, then 5 and 2 are compared, 5> 2, the output array is changed, 1, 1}, 5 and 4 comparison, 5> 4, the output array is changed to {3, 1, 1}, 5 and 1 comparison, 5> 1, the output array is changed to {4, 1, 1 }, this is a round. In the next round, 2 is compared with 4 and 1, and 4 and 1 are compared. Finally, the output array is {, 3, 1 }. If there are repeated elements in the array, the relative order obtained by two elements of the same size may be inconsistent. Therefore, a function such as deleteRepeat is written during implementation, positionInfo is used to save location information.