站在巨人的肩膀上!
善用 STL 的 sort( ) 與 java.util.Arrays.sort( );
ㄟ .. 這些我說過很多次了喔 ..
概念就是善用別人已經寫好的程式庫啦!
C++ STL 整個是女共匪寫的, 裡面一堆與資料結構有關的,
以及一些好用的排序函數 sort( );
阿那個 Java 就全部抄過來放在 java.util.* 這 package 內 !
其中 排序函數放在 java.util.Arrays 與 java.util.Collections 這兩個類別,
用法上很類似, 觀念也類似, 但是文法不太一樣!
現在我把這兩個最基本的範例 sortAry.java 與 sortAry.cpp 再公佈給大家研究!
注意 Java 不能有獨立存在的 function !
所以無法寫獨立使用的比較函數 (compare function)!
請把 sortRec.jar 抓回去解壓縮就可看到各原始碼!
注意, sortRec.jar 也是可以執行喔: java -jar sortRec.jar
點這抓 sortRec.jar
01 //sortAry.java -- CopyLeft by tsaiwn@csie.nctu.edu.tw
02 //use Arrays.sort( ) to sort an array of some Object
03 //Note java.lang.Comparable, java.util.Comparator
04 import java.util.*;
05 class sortAry {
06 java.io.PrintStream cout = System.out;
07 public static void main(String xx[ ]) {
08 new sortAry( );
09 }
10 public sortAry( ) { main( ); } // constructor ; call int main( );
11 int main( ) {
12 String n[ ]={"abc", "bbs", "aaaaa", "cat", "hat", "dog", "god"};
13 int ht[ ] = { 168, 158, 180, 175, 173, 165, 163 };
14 double wt[ ] = { 88.5, 75, 76.5, 66, 68, 70, 60, 99, 33};
15 ST s[ ] = new ST[7];
16 for(int i=0; i<7; ++i)
17 s[i] = new ST(n[i], ht[i], wt[i]); // name, height, weight
18 cout.printf("Original s: \n"); print(s);
19 Arrays.sort(s);
20 cout.println("\nafter Arrays.sort(s);: "); print(s);
21 God arben = new God( ); // arben is a Comparator
22 Arrays.sort(s, arben);
23 cout.println("after Arrays.sort(s, arben);: "); print(s);
24 Arrays.sort(s, Collections.reverseOrder(arben));
25 cout.print("after Arrays.sort(s, ");
26 cout.println("Collections.reverseOrder(arben)); : ");
27 print(s);
28 //Java can NOT pass a compare function to sort program
29 return 0;
30 }// int main( )
31 void print(ST x[ ]) { // print array of ST, 但故意少印兩個元素
32 cout.print(" "+x[0].name+" "+x[0].height+" "+x[0].wet);
33 for(int i=1; i < x.length-2; ++i)
34 cout.print(", "+x[i].name+" "+x[i].height+" "+x[i].wet);
35 cout.println( );
36 }//print(ST
37 }//class
38 class ST implements Comparable {
39 ST(String a, int b, double c) {
40 name=a; height=b; wet=c;
41 }
42 public int compareTo(Object bb) {
43 ST b = (ST)bb; System.out.print(". ");
44 return this.height - b.height;
45 }
46 String name; int height; double wet;
47 }//ST
48 class God implements Comparator <ST>{ // God 是 比較器類別
49 public int compare(ST aa, ST bb){
50 if( aa.wet == bb.wet) return 0;
51 if( aa.wet > bb.wet) return -1; // Descending on wet
52 return 1;
53 }
54 }//class God
55 /*********************
56 D:\COURSE\OOP\ppnt> javac sortAry.java
57
58 D:\COURSE\OOP\ppnt> java sortAry
59 Original s:
60 abc 168 88.5, bbs 158 75.0, aaaaa 180 76.5, cat 175 66.0, hat 173 68.0
61 . . . . . . . . . . . . . . .
62 after Arrays.sort(s);:
63 bbs 158 75.0, god 163 60.0, dog 165 70.0, abc 168 88.5, hat 173 68.0
64 after Arrays.sort(s, arben);:
65 abc 168 88.5, aaaaa 180 76.5, bbs 158 75.0, dog 165 70.0, hat 173 68.0
66 after Arrays.sort(s, Collections.reverseOrder(arben)); :
67 god 163 60.0, cat 175 66.0, hat 173 68.0, dog 165 70.0, bbs 158 75.0
68 *************************/
這是 C++ 範例, 使用女共匪寫的 STL 程式庫 ...
01 //sortAry.cpp -- by tsaiwn@csie.nctu.edu.tw
02 //use sort( ) in <algorithm> to sort an array of some Object
03 #include <algorithm>
04 #include <string>
05 #include <iostream>
06 #include <stdio.h>
07 using namespace std;
08 class ST { public:
09 ST( ) { name="Unknown"; }
10 ST(string a, int b, double c) {
11 name=a; height=b; wet=c;
12 }
13 bool operator< (const ST& b) const { // Java: int compareTo(
14 cout<<". ";
15 return this->height < b.height; // Ascending on height
16 }
17 string name; int height; double wet;
18 };//ST
19 class God { public: // 這 God 是個比較器類別
20 bool operator( ) (const ST& aa, const ST& bb){
21 return aa.wet > bb.wet; // Descending on wet
22 }
23 };//class God
24 void print(ST x[ ], int n) { // print array of ST, 但故意少印兩個元素
25 cout <<" "<<x[0].name<<" "<<x[0].height<<" "<<x[0].wet;
26 for(int i=1; i < n-2; ++i)
27 cout <<", "<<x[i].name<<" "<<x[i].height<<" "<<x[i].wet;
28 cout << endl;
29 }//print(ST
30 bool compf(const ST& aa, const ST& bb){
31 return aa.wet < bb.wet; // Ascending on wet
32 }
33 int main( ) {
34 string n[ ]={"abc", "bbs", "aaaaa", "cat", "hat", "dog", "god"};
35 int ht[ ] = { 168, 158, 180, 175, 173, 165, 163 };
36 double wt[ ] = { 88.5, 75, 76.5, 66, 68, 70, 60, 99, 33};
37 ST s[7]; // Java: ST s[ ] = new ST[7];
38 for(int i=0; i<7; ++i)
39 s[i] = * (new ST(n[i], ht[i], wt[i])); // name, height, weight
40 cout << "Original s: " <<endl;
41 print(s, sizeof s/sizeof s[0]);
42 sort(s, s+(sizeof s/sizeof s[0]));
43 cout << "\nafter sort(s, s+sizeof s/sizeof s[0]); : " <<endl;
44 print(s, sizeof s/sizeof s[0]);
45 God arben; // arben is a Comparator
46 sort(s, s+(sizeof s/sizeof s[0]), arben);
47 cout << "after sort with arben: "<< endl;
48 print(s, sizeof s/sizeof s[0]);
49 sort(s, s+(sizeof s/sizeof s[0]), compf);
50 cout << "after sort with compf: "<< endl;
51 // Note Java can NOT pass a compare function to sort program
52 print(s, sizeof s/sizeof s[0]);
53 return 0;
54 }// int main( )
55 /*********************
56 C:\testc\STL> g++ sortAry.cpp
57
58 C:\testc\STL> a
59 Original s:
60 abc 168 88.5, bbs 158 75, aaaaa 180 76.5, cat 175 66, hat 173 68
61 . . . . . . . . . . . . . . . . . . . . . . .
62 after sort(s, s+sizeof s/sizeof s[0]); :
63 bbs 158 75, god 163 60, dog 165 70, abc 168 88.5, hat 173 68
64 after sort with arben:
65 abc 168 88.5, aaaaa 180 76.5, bbs 158 75, dog 165 70, hat 173 68
66 after sort with compf:
67 god 163 60, cat 175 66, hat 173 68, dog 165 70, bbs 158 75
68 *************************/
|