注册 登录
编程论坛 C++教室

[求助]用C++排序(冒泡,选择,快速)

kid1412 发布于 2007-06-27 10:01, 1143 次点击
大哥大姐们 帮帮忙吧 急用啊!!!!!11
用C++编程 排序(冒泡,选择,快速)用函数实现 还要统计出排序次数
谢谢拉!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10 回复
#2
aipb20072007-06-27 10:26
这也太简单了吧


#3
kid14122007-06-27 10:31
回复:(kid1412)[求助]用C++排序(冒泡,选择,快速)

大哥 帮帮忙吧 谢谢拉

#4
aipb20072007-06-27 10:35
书上有啊

数据结构版里也有

百度下也有

最好是自己学学吧,不难的,最多费你半天时间。

实在不行,我给你帖出来做参考。
#5
kid14122007-06-27 16:01
不好意思,贴到你这来了!

[此贴子已经被aipb2007于2007-6-27 17:42:39编辑过]


#6
aipb20072007-06-27 17:41

冒泡:

#include <iostream>

using namespace std;

int main(){
const int size = 5;
int a[size] = {2,56,25,4,8};
//sort
for (int i = 0;i < size - 1;++i){
for (int j = 0;j < size - 1 - i;++j){
if (a[j] > a[j+1]){
//swap
int temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
//display after sorted
for (int i = 0;i < size;++i)
cout << a[i] << " ";
cout << endl;

system("pause");
}

[此贴子已经被作者于2007-6-27 17:43:23编辑过]

#7
aipb20072007-06-27 17:44
[CODE]
选择:

#include <iostream>

using namespace std;

int main(){
const int size = 5;
int a[size] = {2,56,25,4,8};
//sort
int small;
for (int i = 0;i < size-1;++i){
small = i;
for (int j = i+1;j < size;++j){
if (a[j] < a[small])
small = j;
}
//swap
int temp = a[i];
a[i] = a[small];
a[small] = temp;
}
for (int i = 0;i < size;++i)
cout << a[i] << " ";
cout << endl;
system("pause");
}

[/CODE]
#8
aipb20072007-06-27 17:44
[CODE]
快速:

#include <iostream>

using namespace std;

int get_pivot(int A[],int lindex,int rindex){
if (A[lindex] > A[rindex])
swap(A[lindex],A[rindex]);
return A[lindex];
}

void quick_sort(int A[],int lindex,int rindex){
if(lindex < rindex){
int pivot = get_pivot(A,lindex,rindex);
int i = lindex + 1,j = rindex;
do{
while (A[i] < pivot) ++i;
while (A[j] > pivot) --j;
if (i < j)
//swap A[i],A[j]
swap(A[i],A[j]);
else{
if (j != lindex)
//swap A[j],pivot
swap(A[lindex],A[j]);
}
}
while(i < j);
quick_sort(A,lindex,j-1);
quick_sort(A,j+1,rindex);
}
}

int main(){
const int size = 5;
int a[size] = {2,56,25,4,8};
quick_sort(a,0,size-1);
//display after sorted
for (int i = 0;i < size;++i)
cout << a[i] << " ";
cout << endl;

system("pause");
}

[/CODE]
#9
HJin2007-06-27 18:09
以下是引用kid1412在2007-6-27 10:01:56的发言:
大哥大姐们 帮帮忙吧 急用啊!!!!!11
用C++编程 排序(冒泡,选择,快速)用函数实现 还要统计出排序次数
谢谢拉!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

1. you may want to digest apib2007's posts.
2. for quicksort I want to give some comments: the pivot is critical to the average running time O(n lgn). A bad pivot could give you the worst-case running time--- O(n^2). I described my random quicksort below;
3. a better approach is the so-called median of the three pivot technique. Just google for it.

----------------------------------------------------------------------------------------------
/**
p --- left index
r --- right index
both p and r are inclusive.
*/
int partition(int *a, int p, int r)
{
int x = a[r];
int i = p - 1;
for(int j=p; j<r; ++j)
{
if(a[j] <= x )
{
++i;
swap(a[i], a[j]);
}
}
++i;

swap(a[i], a[r]);

return i;
}

int partition_random(int *a, int p, int r)
{
// randomly return an index between p and r, both inclusive
int i = random(p, r);
swap(a[i], a[r]);
return partition(a, p, r);
}

void quicksort(int *a, int p, int r)
{
if( p < r)
{
/**
You could do a normal partition, but a random
partition is guarantteed to give you better
average running time. In this case, the running
time is O(n lgn).
*/

//int q = partition(a, p, r);
int q = partition_random(a, p, r);
quicksort(a, p, q-1);
quicksort(a, q+1, r);
}
}

// my simple pseudo-random number generator.
// This is a bad one, you can find good ones in
// boost library at www.boost.org.
int random(int p, int r)
{
return p+( rand() | (rand() << 16) )% (r-p+1);
}


#10
aipb20072007-06-27 20:11
HJin分析的很好啊!

quick要想高效率,最好还是按你的随机产生 pivot。

GOOD!
#11
野比2007-06-27 20:49
没时间在电脑上看... 还是打出来拿到床上去看...
1