排序——快速排序算法
2015-10-10 21:25
351 查看
快速排序的思想在程序中经常用到,虽然C++给出了快速排序的函数调用,但是很多程序可能需要自己写排序过程,快速排序就会被用到,以下是快速排序算法:
快速排序时间复杂度是O(nlog(n)),在数据有序的情况下最耗时
(程序输入如果使用scanf_s则编译器为vs2013)
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 1<<9
typedef struct {
int key;
}RedType;
typedef struct {
RedType elemword[MAXSIZE];
int count;
}SqList;
void InitialSqList(SqList &);
void QuickSort(SqList &);
void QSort(SqList &, int, int);
int Partition(SqList &, int, int);
void PrintSqList(SqList);
void InitialSqList(SqList &L)
{
int i;
scanf("%d", &L.count);
for (i = 1; i <= L.count; i++)
scanf("%d", &L.elemword[i].key);
}
void PrintSqList(SqList L)
{
int i;
for (i = 1; i <= L.count; i++)
printf("%d", L.elemword[i].key);
printf("\n");
}
void QSort(SqList &L, int low, int high)
{
int pivotloc;
if (low<high)
{
pivotloc = Partition(L, low, high);
QSort(L, low, pivotloc - 1);
QSort(L, pivotloc + 1, high);
}
}
void QuickSort(SqList &L)
{
QSort(L, 1, L.count);
}
int Partition(SqList &L, int low, int high)
{
int pivotkey;
pivotkey = L.elemword[low].key;
while (low<high)
{
while (low<high&&L.elemword[high].key >= pivotkey)
--high;
L.elemword[low].key = L.elemword[high].key;
while (low<high&&L.elemword[low].key <= pivotkey)
++low;
L.elemword[high].key = L.elemword[low].key;
}
L.elemword[low].key = pivotkey;
return low;
}
int main()
{
SqList L;
int i;
while (scanf("%d",&L.count)!=EOF)
{
L.count = L.count + 1;
for (i = 1; i <= L.count; i++)
scanf("%d", &L.elemword[i].key);
QuickSort(L);
PrintSqList(L);
}
return 0;
}
快速排序时间复杂度是O(nlog(n)),在数据有序的情况下最耗时
(程序输入如果使用scanf_s则编译器为vs2013)
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 1<<9
typedef struct {
int key;
}RedType;
typedef struct {
RedType elemword[MAXSIZE];
int count;
}SqList;
void InitialSqList(SqList &);
void QuickSort(SqList &);
void QSort(SqList &, int, int);
int Partition(SqList &, int, int);
void PrintSqList(SqList);
void InitialSqList(SqList &L)
{
int i;
scanf("%d", &L.count);
for (i = 1; i <= L.count; i++)
scanf("%d", &L.elemword[i].key);
}
void PrintSqList(SqList L)
{
int i;
for (i = 1; i <= L.count; i++)
printf("%d", L.elemword[i].key);
printf("\n");
}
void QSort(SqList &L, int low, int high)
{
int pivotloc;
if (low<high)
{
pivotloc = Partition(L, low, high);
QSort(L, low, pivotloc - 1);
QSort(L, pivotloc + 1, high);
}
}
void QuickSort(SqList &L)
{
QSort(L, 1, L.count);
}
int Partition(SqList &L, int low, int high)
{
int pivotkey;
pivotkey = L.elemword[low].key;
while (low<high)
{
while (low<high&&L.elemword[high].key >= pivotkey)
--high;
L.elemword[low].key = L.elemword[high].key;
while (low<high&&L.elemword[low].key <= pivotkey)
++low;
L.elemword[high].key = L.elemword[low].key;
}
L.elemword[low].key = pivotkey;
return low;
}
int main()
{
SqList L;
int i;
while (scanf("%d",&L.count)!=EOF)
{
L.count = L.count + 1;
for (i = 1; i <= L.count; i++)
scanf("%d", &L.elemword[i].key);
QuickSort(L);
PrintSqList(L);
}
return 0;
}
相关文章推荐
- 快速排序
- 文件遍历排序函数
- C#选择排序法实例分析
- C#插入法排序算法实例分析
- C#实现Datatable排序的方法
- SQLSERVER的排序问题结果不是想要的
- Windows Powershell排序和分组管道结果
- C#快速排序算法实例分析
- C#通过IComparable实现ListT.sort()排序
- C#选择法排序实例分析
- SQL学习笔记四 聚合函数、排序方法
- C#对list列表进行随机排序的方法
- 一根网线内的8根线哪4根是传输数据的,哪四根是防干扰的
- C#折半插入排序算法实现方法
- SQL进行排序、分组、统计的10个新技巧分享
- C++实现位图排序实例
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++快速排序的分析与优化详解
- SQL行号排序和分页(SQL查询中插入行号 自定义分页的另类实现)