http://blog.csdn.net/xiaoben_mao
- #include <iostream>
- using namespace std;
- void sort_Func_optimize_jiaohuan(int *a ,int length)
- {
- int now = 0 ;
- for (int i = 0 ; i < length -1 ; i++ )
- {
- now = i;
- for (int j = i + 1 ; j< length ; j++)
- {
- if (a[ j ] < a[ now ])
- {
- now = j;
- }
- }
- int temp = a[i];
- a[i] = a[now];
- a[now] = temp;
- }
- }
- void sort_Func_jiaohuan(int *a ,int length)
- {
- for (int i = 0 ; i < length -1 ; i++ )
- {
- for (int j = i + 1 ; j< length ; j++)
- {
- if (a[ j ] < a[ i ])
- {
- int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- }
- }
- }
- void sort_Func_bubble(int *a ,int length)
- {
-
- for (int i = 1 ; i < length - 1 ; i ++ )
- {
- for (int j = 0; j < length - i ; j ++)
- {
- if (a[j] > a[j + 1])
- {
- int temp = a[j + 1];
- a[j + 1] = a[j];
- a[j] = temp;
- }
- }
- }
- }
- void print_arry(int *a)
- {
- for (int i = 0 ; i <= sizeof(a) ; i ++)
- {
- cout<<a[i]<<" ";
- }
- }
- void main(){
- int num = 0 ;
- int arry[5] = {0};
- int length = (sizeof(arry)/ sizeof(arry[0]));
- for(int i = 0 ; i < length ; i ++)
- {
- cin >> arry[i];
- }
- cout << "排序前:"<<endl;
- print_arry(arry);
- sort_Func_optimize_jiaohuan(arry ,length);
- //sort_Func_jiaohuan(arry,length);
- //sort_Func_bubble(arry,length);
- cout << endl <<"排序后:"<<endl;
- print_arry(arry);
- system("pause");
- }
复制代码 |