标签:
//c 实现
1 #include "stdafx.h" 2 #include "stdlib.h" 3 4 void swap(int& a,int& b) 5 { 6 int t =a; 7 a = b; 8 b = t; 9 } 10 int _tmain(int argc, _TCHAR* argv[]) 11 { 12 int arr[6] ={0,5,3,4,6,2}; 13 int i,j; 14 for(i=0;i<6;i++) 15 { 16 int idxmin = i; 17 for (j=i;j<6;j++) 18 { 19 if(arr[j]<arr[i]) 20 idxmin = j; 21 } 22 if(i!=idxmin) 23 swap(arr[i],arr[idxmin]); 24 } 25 for(int i =0 ;i<6;++i) 26 { 27 printf("%d\t",arr[i]); 28 } 29 system("pause"); 30 return 0; 31 }
// lua 实现
1 function sort_t(t) 2 for i = 1,6 do 3 local idx_min = i 4 for j = i+1,6 do 5 if(t[j]<t[i]) then 6 idx_min = j 7 end 8 end 9 if(idx_min ~= i) then 10 tmp = t[i] 11 t[i] = t[idx_min] 12 t[idx_min] = tmp 13 end 14 end 15 end 16 17 local ta = {1,5,4,3,6,2} 18 sort_t(ta) 19 20 for idx = 1,6 do 21 print(ta[idx]) 22 end
标签:
原文地址:http://www.cnblogs.com/nightcatcher/p/4430283.html