插入排序

来源:互联网 发布:管理合同的软件 编辑:程序博客网 时间:2024/06/05 18:25
#include <iostream>using namespace std;void exchange(int &a,int &b){        a = a+b;        b = a-b;        a = a-b;}void insertsort(int a[],int n){    int i = 0;    for(i =1;i<n;i++)    {        int temp = a[i];        int j;        for(j = i-1;j>=0&&(a[j]>temp);j--)        {            a[j+1] = a[j];        }        exchange(temp,a[j+1]);    }}int main(){    int arr[] = {4,5,7,2,1,9,3,6};    insertsort(arr,8);    for(int i = 0;i< 8;i++)        cout<<arr[i]<<" ";    return 0;}


0 0