C 插入排序算法

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 100
void insertSort(char *arr, int total){

    int i,j,pos;
    char temp;
    for(i=1;i<total;i++){
        j = i-1;
        temp = arr[i];
        while(j>=0 && temp < arr[j]){
           arr[j+1] = arr[j];
           j--;
        }
        arr[j+1] = temp;
        printf("排序后的数组为%s \n",arr);
    }

}
void main(){
   
    char a[MAX]; 
    printf("请输入要排序的字符串");
    gets(a);
    insertSort(a,strlen(a));
}
 

 

 

请输入要排序的字符串54321
排序后的数组为45321
排序后的数组为34521
排序后的数组为23451
排序后的数组为12345
请按任意键继续. . .

 

posted @ 2012-09-26 23:34  ﹏Sakura  阅读(287)  评论(0编辑  收藏  举报