排序算法

来源:互联网 发布:tensorflow中文版文档 编辑:程序博客网 时间:2024/06/08 11:51

#include
using namespace std;
void direct_sort(int *a,int len);


void direct_sort(int *a,int len)
{
 int i,j,min,temp;
 for(i=0;i
 {
  for(min=i,j=i+1;j
  {
   if(a[min]>a[j])                //升序排列
   {
    min=j;
    
  }
  if(min!=i)
  {
   temp=a[min];
   a[min]=a[i];
   a[i]=temp;
  }
 }
}
void direct_sort_2(int * a,int len)
 
 int temp;
 for(int i=0;i
 {
  for(int j=i+1;j
  {
   if(a[i]>a[j])
   {
    temp=a[j];
    a[j]=a[i];
    a[i]=temp;
   }
  }
 }
}

//low从零开始  low和high确定想要排序的范围(即low和high是数组下标)
int Find_pos(int *a,int low,int high)
{
 int val=a[low];
 while(low
 {
  while(low=val)
      high--;
    a[low]=a[high];

  while(low<=val)
   low++;
   a[high]=a[low];
 }
 a[low]=val;         //这一步很重要 !low是下标,将val赋给它
 return low;
}
void Quick_sort(int *a,int low,int high)
{
 int pos;
 if(low
 {
  pos=Find_pos(a,low,high);
  Quick_sort(a,low,pos-1);
  Quick_sort(a,pos+1,high);
 }
}

void sort(int a[],int len)
{
 int i,j,temp;
 for(i=0;i
 {
  for(j=0;j
  {
   if(a[j]>a[j+1])
   {
    temp=a[j+1];
    a[j+1]=a[j];
    a[j]=temp;
   }
  }
 }
}

void insert_sort(int *a,int len)//升序

{

  for(int i=1;i

    {

       int pick=a[i];

       j=i-1;

        while(j>=0&&a[j]>pick)

        {

             a[j+1]=a[j];

             j--;

         }

          ++j;

         a[j]=pick;

    

}

0 0
原创粉丝点击