求一个字符排序算法

soloxiao 2006-03-27 05:01:31
source:
123
abcd
456
abcdf
abc
789
A
1


结果:
1
123
456
789
A
abc
abcd
abcdf

-------------------
原数据可能有9999组,什么方法最快? 谢谢了!
...全文
256 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
guyanhun 2006-03-28
  • 打赏
  • 举报
回复
popy007(Twinsen)

字典排序:

bool lexicographer(string& s1, string& s2) {
return lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end());
}

————————————————————————————————

支持 !配合stl 来做。
du51 2006-03-28
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
static int counter=0;
void swap(char **q,int i,int j);
int Mystrcmp(char *str1,char *str2);

void Adjust(char **p,int i,int m)
{
char *temp=p[i];
int j;
for(j=2*i+1;j<=m;j=2*j+1)
{
if(j<m&&Mystrcmp(p[j],p[j+1])<0)j++;
if(Mystrcmp(temp,p[j])>0)break;
p[i]=p[j];
i=j;
}
p[i]=temp;
}

void heapsort(char **q, int n)
{
int m,i;
for(m=n/2;m>-1;m--)
{
Adjust(q,m,n-1);
printf("第%2d次调整后====>",++counter);
for(i=0;i<n;i++)printf("%s ",q[i]);
printf("\n");
}
printf("******以上是初始堆调整******\n");
for(m=n-1;m>0;m--)
{
swap(q,0,m);
Adjust(q,0,m-1);
printf("第%2d次调整后====>",++counter);
for(i=0;i<n;i++)printf("%s ",q[i]);
printf("\n");
}
}
int main()
{
int i,len;
char *list[ ]= {"123","abcd","456","abcdf","abc","789","A","1"};
len=8;
heapsort(list,len);
for(i=0;i<len;i++)printf("%s ",list[i]);
printf("\n");
system("PAUSE");
return 0;
}
void swap(char **q,int i,int j)
{
char *temp=q[i];
q[i]=q[j];
q[j]=temp;
}
int Mystrcmp(char *str1,char *str2)
{
while(*str1&&*str2&&*str1==*str2)str1++,str2++;
return *str1-*str2;
}
postren 2006-03-28
  • 打赏
  • 举报
回复
参考MSDN中的例子:
// crt_qsort.c
// arguments: every good boy deserves favor

/* This program reads the command-line
* parameters and uses qsort to sort them. It
* then displays the sorted arguments.
*/

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int compare( const void *arg1, const void *arg2 );

int main( int argc, char **argv )
{
int i;
/* Eliminate argv[0] from sort: */
argv++;
argc--;

/* Sort remaining args using Quicksort algorithm: */
qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare );

/* Output sorted list: */
for( i = 0; i < argc; ++i )
printf( " %s", argv[i] );
printf( "\n" );
}

int compare( const void *arg1, const void *arg2 )
{
/* Compare all of both strings: */
return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}
shine51151 2006-03-28
  • 打赏
  • 举报
回复
顺便帮顶
shine51151 2006-03-28
  • 打赏
  • 举报
回复
学习
yinqing_yx 2006-03-28
  • 打赏
  • 举报
回复
容器最好~~~~~~`
北狐狸 2006-03-28
  • 打赏
  • 举报
回复
学习
Torrice 2006-03-28
  • 打赏
  • 举报
回复
学习。。。
soloxiao 2006-03-27
  • 打赏
  • 举报
回复
顶!
popy007 2006-03-27
  • 打赏
  • 举报
回复
字典排序:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;

bool lexicographer(string& s1, string& s2) {
return lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end());
}
int main() {
string s[] = {
"123",
"abcd",
"456",
"abcdf",
"abc",
"789",
"A",
"1"};
const int SZ = sizeof s / sizeof *s;
vector<string> vs;
copy(s, s+SZ, back_inserter(vs));
sort(vs.begin(), vs.end(), lexicographer);
copy(vs.begin(), vs.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
iamwiner 2006-03-27
  • 打赏
  • 举报
回复
你的代码怎么这么乱啊
soloxiao 2006-03-27
  • 打赏
  • 举报
回复
define ONE_SIZE = 20

我是这样做的:
void quick_sort(unsigned char* x, int low, int high)
{
int i=low, j=high;
//int t=x[low];
unsigned char t[20];
memcpy(t,&x[low*ONE_SIZE],ONE_SIZE);

while (i<j)
{
//while (i<j && x[j]>t)
//while (i<j && x[j*ONE_SIZE]>t[0])
while (i<j && str_cmp(true,&x[j*ONE_SIZE],t,ONE_SIZE))
j--;

// x[i]=x[j];
memcpy(x+i*ONE_SIZE,x+j*ONE_SIZE,ONE_SIZE);


//while (i<j && x[i]<=t)
//while (i<j && x[i*ONE_SIZE]<=t[0])
while (i<j && str_cmp(false,&x[i*ONE_SIZE],t,ONE_SIZE))
i++;

// x[j]=x[i];
memcpy(x+j*ONE_SIZE,x+i*ONE_SIZE,ONE_SIZE);

//x[i] = t;
memcpy(x+i*ONE_SIZE,t,ONE_SIZE);
quick_sort(x,low,i-1); //递归调用此函数
quick_sort(x,i+1,high);
}

我把所有数据放到x里面, 然后定长取出来再比较,
quick_sort(x,0,count);
个数少的时候还行,多了就挂了!
Could 2006-03-27
  • 打赏
  • 举报
回复
随便放在一个容器里面,sort就行了。
postren 2006-03-27
  • 打赏
  • 举报
回复
ascii码比较,用快速排序法
soloxiao 2006-03-27
  • 打赏
  • 举报
回复
ascii码比较!

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧