|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- void SELECTSORT(int a[] ,int n)
- {
- int i,j,d;
- int temp;
- for(i = 0;i < n-1;i++)
- {
- d = i;
- for(j = i +1; j < n; j++)
- {
- if (a[j] < a[d])
- {
- d = j;
- }
-
- if (d != i)
- {
- temp = a[d];
- a[d] = a[i];
- a[i] = temp;
- }
- }
-
- }
- int main()
- {
- int a[6] ={5,2,7,1,4,3};
- SELECTSORT(a,&a[5]-&a[0]);
- for (int i = 0;i < 6;i++)
- {
- printf("%d\n",a[i]);
- }
-
- }
复制代码
选择排序,找出错误的地方
- #include<stdio.h>
- void SELECTSORT(int a[] ,int n)
- {
- int i,j,d, temp;
- for(i = 0; i < n-1; i++)
- {
- d = i;
- for(j = i +1; j < n; j++)
- {
- if (a[j] < a[d])
- {
- d = j;
- }
- } //<-------------------
- if (d != i)
- {
- temp = a[d];
- a[d] = a[i];
- a[i] = temp;
- }
- }
- }
-
- int main()
- {
- int a[6] ={5,2,7,1,4,3};
- SELECTSORT(a,6); //<-------------- 为啥用地址?你要把 sizeof(int) 也考虑
- for (int i = 0;i < 6;i++)
- {
- printf("%d ",a[i]);
- }
- return 0;
- }
复制代码
|
|