• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
caizhk的爬坑之旅
博客园    首页    新随笔    联系   管理    订阅  订阅

排序算法学习

字符串内排序
题目描述:

输入一个字符串,长度小于等于200,然后将输出按字符顺序升序排序后的字符串。

输入:

测试数据有多组,输入字符串。

输出:

对于每组输入,输出处理后的结果。

样例输入:
bacd
样例输出:
abcd
来源:
2010哈尔滨工业大学研究生复试题
相关代码如下:
  1 /*
  2  *FileName: 1054
  3  *Author: Cai-ZhongKai
  4  *Version:
  5  *Date: 2016-3-16
  6  *Description:
  7  *Copyright(C),2015-2016,SYSU
  8 */
  9 #include <stdio.h>
 10 #include <stdlib.h>
 11 #include <string.h>
 12 
 13 char str[210];
 14 int n;
 15 
 16 
 17 /*
 18  *function cmp
 19 */
 20 int cmp(const void *a,const void *b)
 21 {
 22   return *(char *)a-*(char *)b;
 23 }
 24 /*
 25  *function insert sort
 26 */
 27 void Insert_Sort(char a[],int num)
 28 {
 29   int i,j;
 30   char temp;
 31   for(i=0;i<num;i++){
 32     temp=a[i];
 33     for(j=i;a[j-1]>temp&&j>0;j--){
 34       a[j]=a[j-1];
 35     }
 36     a[j]=temp;
 37   }
 38 }
 39 
 40 /*
 41 *binary insert sort
 42 */
 43 void Binary_Sort(char a[],int n)
 44 {
 45   int i,j;
 46   char temp;
 47   int low,mid,high;
 48   for(i=1;i<n;i++){
 49     temp=a[i];
 50     low=0;
 51     high=i-1;
 52     while(low<=high){
 53       mid=(low+high)/2;
 54       if(a[mid]>temp){
 55         high=mid-1;
 56       }
 57       else{
 58         low=mid+1;
 59       }
 60     }
 61     for(j=i-1;j>=high+1;--j){
 62       a[j+1]=a[j];
 63     }
 64     a[high+1]=temp;
 65   }
 66 }
 67 
 68 /*
 69  *function bubble sort
 70 */
 71 void Bubble_Sort(char a[],int n)
 72 {
 73   int i,j;
 74   char temp;
 75   int flag;
 76   for(i=0;i<n;i++){
 77     flag=0;
 78     for(j=n-1;j>i;j--){
 79       if(a[j]<a[j-1]){
 80         temp=a[j];
 81         a[j]=a[j-1];
 82         a[j-1]=temp;
 83         flag=1;
 84       }
 85     }
 86     if(flag==0){
 87       return;
 88     }
 89   }
 90 }
 91 /*
 92  *function shell sort
 93 */
 94 void Shell_Sort(char a[],int n)
 95 {
 96   int d;//步长
 97   int i,j;
 98   char temp;
 99   for(d=n/2;d>=1;d/=2){
100     for(i=0;i<n;i=i+d){
101       temp=a[i];
102       for(j=i;j>0&&a[j-d]>temp;j=j-d){
103         a[j]=a[j-d];
104       }
105       a[j]=temp;
106     }
107   }
108 }
109 
110 /*
111  *function select sort
112 */
113 void Select_Sort(char a[],int n)
114 {
115   int i,j;
116   char temp;
117   int min;
118 
119   for(i=0;i<n;i++){
120     temp=a[i];
121     min=i;
122     for(j=i;j<n;j++){
123       if(a[j]<a[min]){
124         min=j;
125       }
126     }
127     a[i]=a[min];
128     a[min]=temp;
129   }
130 }
131 /*
132 * function partition
133 */
134 int partition(char a[],int low,int high)
135 {
136   char pivot=a[low];
137   while(low<high){
138     while(low<high&&a[high]>=pivot){
139       --high;
140     }
141     a[low]=a[high];
142 
143     while(low<high&&a[low]<pivot){
144       ++low;
145     }
146     a[high]=a[low];
147   }
148   a[low]=pivot;
149   return low;
150 }
151 /*
152 * Quick sort
153 */
154 void Quick_Sort(char a[],int low,int high)
155 {
156   int pos;
157   if(low<high){
158     pos=partition(a,low,high);
159     Quick_Sort(a,low,pos-1);
160     Quick_Sort(a,pos+1,high);
161   }
162 }
163 
164 int main(void)
165 {
166   int jdx;
167   while(scanf("%s",str)!=EOF){
168 
169     n=strlen(str);
170     Quick_Sort(str,0,n-1);
171 //    qsort(str,n,sizeof(str[0]),cmp);
172     for(jdx=0;jdx<n;jdx++){
173       printf("%c",str[jdx]);
174     }
175 
176     printf("\n");
177   }
178 
179   return 0;
180 }

 

posted @ 2016-03-16 14:53  caizhk  阅读(171)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3