Data Structures (Weiss) Chapter 7: QuickSort 快速排序
来源:互联网 发布:水印素材软件 编辑:程序博客网 时间:2024/05/29 13:21
//
// main.cpp
// Data Structure TRY1
//
// Created by zr9558 on 6/7/13.
// Copyright (c) 2013 zr9558. All rights reserved.
//
// Data Structure C++, Weiss, P.285 quickSort
#include<iostream>
using namespace std;
#include<vector>
#include<math.h>
template<typename Comparable>
void insertionSort(vector<Comparable> &a,int left, int right)
{
int j;
for(int p=left+1; p<=right; ++p)
{
Comparable temp=a[p];
for( j=p; j>left && temp<a[j-1]; --j)
a[j]=a[j-1];
a[j]=temp;
}
}
// Code to perform median-of-three partitioning
template<typename Comparable>
const Comparable &median3(vector<Comparable> &a,int left, int right)
{
int center=(left+right)/2;
if( a[center]<a[left]) swap(a[left],a[center]);
if( a[right]<a[left]) swap(a[left],a[right]);
if( a[right]<a[center]) swap(a[center],a[right]);
//Place pivot at position right-1
swap(a[center],a[right-1]);
return a[right-1];
}
template<typename Comparable>
void quickSort(vector<Comparable> &a,int left, int right)
{
if( left+10<=right)
{
Comparable pivot=median3(a,left,right);
// Begin partitioning
int i=left, j=right-1;
for(;;)
{
while( a[++i]<pivot) ;
while( pivot<a[--j]) ;
if( i<j) swap(a[i],a[j]);
elsebreak;
}
swap(a[i],a[right-1]);//Restore pivot
quickSort(a, left, i-1);
quickSort(a, i+1, right);
}
else insertionSort(a, left, right);
}
template<typename Comparable>
void quickSort(vector<Comparable> &a)
{
quickSort(a,0,int( a.size())-1);
}
int main()
{
vector<int> ivec;
for(int i=0; i<40; ++i)
ivec.push_back(rand()%200);
for(int i=0; i!=ivec.size(); ++i)
cout<<ivec[i]<<" ";
cout<<endl;
quickSort(ivec);
for(int i=0; i!=ivec.size(); ++i)
cout<<ivec[i]<<" ";
cout<<endl;
return 0;
}
- Data Structures (Weiss) Chapter 7: QuickSort 快速排序
- Data Structures (Weiss) Chapter 7: Insertion Sort
- Data Structures (Weiss) Chapter 7: HeapSort
- Data Structures (Weiss) Chapter 7: MergeSort
- Data Structures (Weiss) Chapter 3: Vector C++
- Data Structures (Weiss) Chapter 3: List C++
- Data Structures (Weiss) Chapter 5: Separate Chain
- Data Structures (Weiss) Chapter 6: Leftist Heap
- Data Structures (Weiss) Chapter 6: Binomial Queue
- Data Structures (Weiss) Chapter 12: Treaps
- Data Structures (Weiss) Chapter 4: Binary Search Tree C++
- Data Structures (Weiss) Chapter 4: AVL Trees 平衡树
- Data Structures (Weiss) Chapter 6: Binary Heap, Priority Queue
- Data Structures (Weiss) Chapter 3: The Queue ADT, Pointer
- Data Structures (Weiss) Chapter 3: The Queue ADT, Array
- Data Structures (Weiss) Chapter 12: Splay Tree 伸展树
- Data Structures (Weiss) Chapter 8: Union and Find, Disjoint Sets, union by sizes, C++
- Data Structures (Weiss) Chapter 5: Hash Tables without Linked Lists (quadratic probing)
- error CS0234: 命名空间“Microsoft.Office.Interop”中不存在类型或命名空间名称“Word”(是否缺少程序集引用?)
- insert all 的使用方法
- nginx 服务并发过10万的linux内核优化配置
- 对比C#中==与equal方法
- 索引的分类
- Data Structures (Weiss) Chapter 7: QuickSort 快速排序
- poj3311 哈密顿回路
- javascript keycode大全 js 验证只能输入数字或拼音
- 初识Mongodb之[CURD]-PHP版
- 位图索引
- javascript调用outlook写邮件以及发起livemeeting
- Android数据存储(总结篇)
- Bringing the power of Google technology to mobile
- UNIX环境高级编程——epoll函数使用详解