Implementation of Android sorting-for beginners

Source: Internet
Author: User

Bad code:

View plain
Int [] iArray = {12,122,123,124, 1 ,};
 
// Sort iArray
Int iMin = 999999;
Int indexMin = 0;
Int iTmp = 0;
For (int I = 0; I <iArray. length; I ++ ){
// Find the smallest
For (int j = I; j <iArray. length; j ++ ){
If (iMin> iArray [j]) {
IMin = iArray [j];
IndexMin = j;
}
}
// Put it at the beginning
ITmp = iArray [I];
IArray [I] = iArray [indexMin];
IArray [indexMin] = iTmp;
}
 
For (int I = 0; I <iArray. length; I ++ ){
System. out. print (iArray [I] + ",");
}
System. out. println ();

First, there is a BUG. The minimum value is initialized with 999999. If the number in the array is greater than 999999, the sorting will not proceed.

You can change 999999 to Integer. MAX_VALUE.

Second, you just sort an array and do not need to write it yourself. Arrays contains the sorting function. The program can be changed:


View plain
Int [] iArray = {12,122,123,124, 1 ,};
 
// Sort iArray
Arrays. sort (iArray );
 
For (int I = 0; I <iArray. length; I ++ ){
System. out. print (iArray [I] + ",");
}
System. out. println ();

So what if we want to sort an object instead of an integer?

For example, we have a class:

View plain
Public class Point {
Private int x;
Private int y;
Public Point (int x, int y ){
This. x = x;
This. y = y;
}
}

Now we need to sort an array of this class. Can I use the sorting function of the system. Of course.

First, our class must implement the interface: Comparable. That is, this object must be comparable.

Second, we need to implement the function defined by this interface: compareTo.

The modified class is as follows:

View plain
Public class Point implements Comparable {
Private int x;
Private int y;
Public Point (int x, int y ){
This. x = x;
This. y = y;
}

Private int getMix (){
Return (x <8) + y;
}

@ Override
Public int compareTo (Object o ){
Point other = (Point) o;
Return getMix ()-other. getMix ();
}

@ Override
Public String toString (){
Return "(" + x + "," + y + ")";
}
}

The sorting code is as follows:

View plain
// Randomly generate 5 objects
Random random = new Random ();
List <Point> lst = new ArrayList <Point> ();
For (int I = 0; I <5; I ++ ){
Lst. add (new Point (random. nextInt (100), random. nextInt (100 )));
}
Point [] pArray = lst. toArray (new Point [lst. size ()]);
 
// Sort the Point Array
Arrays. sort (pArray );
 
For (int I = 0; I <pArray. length; I ++ ){
System. out. print (pArray [I] + ",");
}
System. out. println ();

In the compareTo function, the values of the objects to be compared are generally the values corresponding to the objects to be compared. If compareTo returns a negative number, it indicates that the current object is smaller than the input parameter.

Author: "yihui823 column"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.