Thứ Năm, 13 tháng 11, 2014

Thuật toán sắp xếp InsertSort


#include<conio.h>
#include<stdio.h>
#include<iostream>
using namespace std;

void swap(int &a, int &b)
{
 int temp;
 temp=a;
 a=b;
 b=temp;
}

void InsertSort(int a[], int n)
{
 for(int i=1;i<n;i++)
  for(int j=i;j>0;j--)
   if(a[j]<a[j-1])
    swap(a[j],a[j-1]);
}

main()
{
 int n=8;
 int a[]={1,5,3,4,9,7,3,4};

 for(int i=0;i<n;i++)
  cout<<a[i]<<"  ";

 cout<<endl<<"Insert Sort: "<<endl;

 InsertSort(a,n);

 for(int i=0;i<n;i++)
  cout<<a[i]<<"  ";

 getch();
 return 0;
}

Không có nhận xét nào: