| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:a few concetps about sorting algorithms and questions
只看楼主 加入收藏
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:5 
a few concetps about sorting algorithms and questions

A few concetps about sorting algorithms and questions

A sorting algorithm is stable if whenever there are two
records R and S with the same key and with R appearing
before S in the original list, R will appear before
S in the sorted list. For example:

(1, 2), (1, 4) --> sorting algo which gives
(1, 2) (1, 4) is stale;
(1, 2), (1, 4) --> sorting algo which gives
(1, 4) (1, 2) is not stale.

A sorting algorithm is in place if only O(1) extra memory is
needed beyond the items being sorted. Comments: when define
the term in place, a book I read said

only O(1) or O(lgn) extra memory is needed.

Let us stick with the first definition: only O(1) memory is allowed
for in place algorithm.

Under these classifications and standard implementations (e.g.,
a not standard implementation of bubble sort can use as such
memory as the coder wants):

bubble, insertion, and merge are stable;
heap and quick are unstable.

bubble, insertion and heap are in place;
merge is not in place.

Q1: Is quick sort considered to be in place? Assume that we use O(lgn)
stack length for quicksort, such as the following implementation:

void quicksort(int* a, int p, int r)
{
while(p<r)
{
int q = partition(a, p, r);

// only sort the smaller part
if(q-p<r-q)
{
quicksort(a, p, q-1);
}
else
{
quicksort(a, q+1, r);
}
}
}


Q2: Is there a stable and in place sorting algorithm (based
on comparsion)?

[此贴子已经被作者于2007-9-22 20:25:29编辑过]

搜索更多相关主题的帖子: questions sorting algorithms few concetps 
2007-09-22 19:57
踏魔狼
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:1322
专家分:33
注 册:2005-9-22
收藏
得分:0 
帅哥!不会说中文吗?

=×&D o I p R e E n C g T l X&×=
2007-09-22 20:06
coachard
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:1251
专家分:0
注 册:2007-8-12
收藏
得分:0 
话说回来,HJin的E语很好》》》

偶学编程,也许本身就是一个错。。。
2007-09-22 22:08
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

Q1:
任何非稳定排序都可以转化为稳定排序的,只是看时空代价多少,我想lgn似乎不行吧,没验证,我想快速的话,nlgn是可以的。时间上,就多了一次比较。

Q2:
nlgn的,没有。不额外处理的话。

[此贴子已经被作者于2007-9-23 12:29:28编辑过]


Fight  to win  or  die...
2007-09-23 11:52
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
以下是引用aipb2007在2007-9-23 11:52:19的发言:

Q1:
任何非稳定排序都可以转化为稳定排序的,只是看时空代价多少,我想lgn似乎不行吧,没验证,我想快速的话,nlgn是可以的。时间上,就多了一次比较。

Q2:
nlgn的,没有。不额外处理的话。


Are there proofs for your statements?


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-09-23 18:00
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
回复:(HJin)以下是引用aipb2007在2007-9-23 11:52:...
自己想的!

因为稳定与否就是因为相同元素的原始顺序问题。加一个附属值(下标序)在元素相同时多判断一次。


Fight  to win  or  die...
2007-09-23 20:32
快速回复:a few concetps about sorting algorithms and questions
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020138 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved