1: public class RecursiveSelectionSort {
2: public static void main(String[] args) {
3: int [] array ={5,2,3,6,1,7,0,9,100,4,8};
4: selectionSort(array, array.length);
5: for(int i=0;i<array.length;i++){
6: System.out.print(array[i]+" ");
7: }
8: }
9: /**
10: * Using the concept of selection sort.
11: * Implemented by an iterative and a recursive mechanisms.
12: *
13: * Following are steps:
14: * 1.Finding the smallest one and its index;
15: * 2.Put the smallest into the right end side;
16: * 3.Decrease the array size;
17: * Recursively calling this method.
18: *
19: * @param array unsorted array
20: * @param length array length
21: */
22: public static void selectionSort(int[] array , int length) {
23: if(length > 0){
24: int smallestIdx = 0 ;
25: int smallest = array[smallestIdx];
26: //1.From left to right find the smallest one and its index;
27: for(int i =0 ;i<length ;i++){
28: if(array[i]< smallest){
29: smallest = array[i];
30: smallestIdx = i;
31: }
32: }
33: //2.Put the smallest into the right end side;
34: swapElements(array,smallestIdx,length -1);
35: //3.Decrease the array size;
36: length -=1;
37: //4.Recursively calling this method.
38: selectionSort(array,length);
39: }
40: }
41: private static void swapElements(int[] array, int idx, int idx1) {
42: int tmp = array[idx];
43: array[idx] = array[idx1];
44: array[idx1] = tmp;
45: }
46: }
網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
2012年3月24日 星期六
Recursive Selection Sort (遞迴選擇排序法)
維護舊有Java程式專案
一中兩區
一中兩區?? 統一不是壞事, 如果跟民主國家統一那沒問題,
要跟共產國家統一??請舉辦公投,如果通過的話,就統一好了,
再這樣下去,會真的成為馬區長.!!
最好是維持現狀,等待中國成為民主國家再說
要跟共產國家統一??請舉辦公投,如果通過的話,就統一好了,
再這樣下去,會真的成為馬區長.!!
最好是維持現狀,等待中國成為民主國家再說
訂閱:
文章 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)