快速排序(自己实现)
来源:互联网 发布:合肥淘宝合作快递 编辑:程序博客网 时间:2024/05/21 17:16
#include<iostream>
#include<string>
#include<ctime>
using namespace std;
class solution
{
public:
void swap(int data[], int randnum, int end)
{
int temp;
temp = data[end];
data[end] = data[randnum];
data[randnum]=temp;
}
int partition(int data[],int start,int end,int length )
{
if (length <= 0 || start < 0 || end>length || (data == nullptr))
throw new exception("wrong input");
srand(unsigned int(NULL));
int randn = rand();
int randnum=randn%(end-start)+start;
swap(data,randnum, end);
int small = start - 1;
//int index = 0;
for (int index = start; index < end; index++)
{
if (data[index] < data[end])
{
small++;//
if (small != index)
{
swap(data,small, index);
}
}
}
small++;
swap(data, small, end);
return small;
}
void quick_sort(int data[],int start,int end,int length)
{
if (data == nullptr || length == 0)
throw new exception("wrong input");
if (start == end)
{
return;
}
int index=partition(data, start, end, length);
if (index > start)
{
//int lengthlf = index - start;
quick_sort(data, start, index-1,length );
}
if (index < end)
{
//int lengthrt = end - index;
quick_sort(data, index+1,end, length);
}
}
};
int main()
{
solution s;
int data[6] = { 0, 2, 4, 3, 1, 5 };
s.quick_sort(data, 0, 5, 6);
for (int i = 0; i < 6; i++)
{
cout << data[i]<<' ';
}
}
#include<string>
#include<ctime>
using namespace std;
class solution
{
public:
void swap(int data[], int randnum, int end)
{
int temp;
temp = data[end];
data[end] = data[randnum];
data[randnum]=temp;
}
int partition(int data[],int start,int end,int length )
{
if (length <= 0 || start < 0 || end>length || (data == nullptr))
throw new exception("wrong input");
srand(unsigned int(NULL));
int randn = rand();
int randnum=randn%(end-start)+start;
swap(data,randnum, end);
int small = start - 1;
//int index = 0;
for (int index = start; index < end; index++)
{
if (data[index] < data[end])
{
small++;//
if (small != index)
{
swap(data,small, index);
}
}
}
small++;
swap(data, small, end);
return small;
}
void quick_sort(int data[],int start,int end,int length)
{
if (data == nullptr || length == 0)
throw new exception("wrong input");
if (start == end)
{
return;
}
int index=partition(data, start, end, length);
if (index > start)
{
//int lengthlf = index - start;
quick_sort(data, start, index-1,length );
}
if (index < end)
{
//int lengthrt = end - index;
quick_sort(data, index+1,end, length);
}
}
};
int main()
{
solution s;
int data[6] = { 0, 2, 4, 3, 1, 5 };
s.quick_sort(data, 0, 5, 6);
for (int i = 0; i < 6; i++)
{
cout << data[i]<<' ';
}
}
阅读全文
0 0
- 快速排序(自己实现)
- 自己实现快速排序
- 自己实现的一个高效快速排序: )
- 归并排序(自己实现)
- 冒泡排序、选择排序、堆排序、快速排序、插入排序算法复杂度分析与算法实现(自己总结与转)
- 快速排序实现(JAVA)
- .net 快速排序 (实现)
- 快速排序(C++实现)
- 快速排序(java实现)
- 快速排序(递归实现)
- 快速排序(java实现)
- 快速排序实现(QuickSort)
- 快速排序(C++实现)
- 快速排序(java实现)
- 快速排序(Python实现)
- 快速排序(C++实现)
- 快速排序实现(Java)
- 快速排序(java实现)
- Java Map的操作示例
- 浅入浅出Lambda表达式
- SVM-支持向量机学习(4):再谈线性SVM-正则化合页损失函数
- pdf转cad转换器转换快速简单方法
- VMWare/ubuntu三种网络连接方式
- 快速排序(自己实现)
- presentViewController多级跳转返回
- 百度网盘下载慢,解决方法
- windows性能监控
- 安卓开发帧动画使用
- PAT-AL 1022. Digital Library
- hdu 1075 map+string
- 论文阅读理解
- [转]ubuntu 14.04 下利用apt-get方式安装opencv