慕课网学习笔记-选择排序代码实现

写在前面

高级程序员与码农的区别就是算法和数据结构

本次使用了c++泛型编程,途中遇到了点小问题,但已经解决,鄙人不是很熟悉c++这么语言,有错误之处还请指出

More code and more thinking

#include 
using namespace std;template <typename T>//The vital code of selectedsort
void selectedSort(T arr[],int n)
{for(int i=0; i<n; i++){int minIndex = i;for(int j=i+1; j<n; j++)if(arr[j]<arr[minIndex])minIndex=j;//s the value of two variablesswap(arr[i],arr[minIndex]);}
}template <typename T>
//The output of sorted array
void outputArray(T arr[],int n)
{for(int i=0; i<n; i++){if(i==0)cout<<arr[i];elsecout<<" "<<arr[i];}cout<<endl;
}int main()
{int n;cin>>n;int arr[n];for(int i=0;i<n;i++)cin>>arr[i];selectedSort(arr,n);outputArray(arr,n);return 0;
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部