博文

直接插入排序(C语言)(2005-11-19 11:13:00)

摘要:#include"stdio.h"#include"conio.h"void insertSort(int a[],int count)   /*count为排序数字个数*/{   int i,j,temp;   for(i=1;i<count;i++)    /*依次插入数字到它前面已经排好序的数字中去*/   {      temp=a[i];      j=i-1;      while(a[j]>temp && j>=0)      {        a[j+1]=a[j];         j--;      }      if(j!=(i-1)) /*第i个数字比前面的都大,不需要重新插入*/             {        a[j+1]=temp;      }             }}void main(){   int a[7]={8,10,2,3,1,7,13};   int i;   insertSort(a,7);   clrscr();   for(i=0;i<7;i++)   ......

阅读全文(11120) | 评论:5

直接选择排序(C语言)(2005-11-19 00:04:00)

摘要:#include"stdio.h"#include"conio.h"void selectSort(int a[],int count){  int i,j,min,temp;  for(i=0;i<count-1;i++)  {     min=i;     for(j=i+1;j<count;j++)       if(a[j]<a[min])           min=j;     if(min!=i)     {       temp=a[min];       a[min]=a[i];       a[i]=temp;     }   }}void main(){  int a[7]={8,9,3,5,1,6,4};  int i;  selectSort(a,7);  clrscr();  for(i=0;i<7;i++)    printf("%4d",a[i]);}......

阅读全文(8255) | 评论:1

快速排序法(C语言)(2005-11-18 13:40:00)

摘要:#include"stdio.h"void quickSort(int a[],int left,int right){   int i,j,temp;   i=left;   j=right;   temp=a[left];   if(left>right)      return;   while(i!=j)/*找到最终位置*/   {      while(a[j]>=temp && j>i)         j--;      if(j>i)         a[i++]=a[j];       while(a[i]<=temp && j>i)          i++;       if(j>i)          a[j--]=a[i];            }   a[i]=temp;   quickSort(a,left,i-1);/*递归左边*/   quickSort(a,i+1,right);/*递归右边*/} void main(){    int a[7]={8,2,6,12,1,9,5};    int i;&......

阅读全文(16193) | 评论:6

冒泡法排序(C语言)(2005-11-17 16:53:00)

摘要:#include"stdio.h"#define True 1#define False 0void main(){   int a[7]={9,12,6,15,3,16,10};   int i,temp;   for(i=1;i<=6;i++)  /*m个数字需要m-1轮排序*/  {    int j;    int ok=True;            for(j=0;j<=7-i;j++)/*第i轮排序需要比较m-i次*/   {      if(a[j]>a[j+1])     {        ok=False;        temp=a[j];        a[j]=a[j+1];        a[j+1]=temp;     }    }     if(ok==True)  /*若该轮排序没有发生任何交换,则说明已经排好序了*/          break;   }   for(i=0;i<7;i++)  {      printf("%4d",a[i]);  } }......

阅读全文(15205) | 评论:2