快速排序为原址排序,无需分配额外的Temp内存,且期望时间复杂度为O(nlgn),可以通过交换数组中的第一个元素和随机一个元素的位置,达到随机化的目的,避开 bad case。
下面是C++实现,原数组元素存放在data.txt文件中:
- 选取第一个元素为主元
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>
//快速排序法
using namespace std;
vector<int> d; int count=0; static int CNT=0;//两个计数器记录比较的次数,用一个即可
void printVector(const vector<int> &a)
{
for (int i = 0; i < a.size(); i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void swap_element(vector<int> &a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
int partition(vector<int> &a, int l, int r)
{
CNT+=r-l;
int i, j, x; // i记录smaller和greater的分界线,j记录遍历位置 ,x记录主元的值
i = l , x = a[l]; //选取第一个元素为主元
for (j = l+1;j < r+1; j++)
{
::count++;
if (a[j] < x)
swap_element(a, ++i, j);
}
swap_element(a, i, l);
return i;
}
void quick_Sort(vector<int> &a, int l,int r)
{
if (l < r)
{
int m = partition(a,l,r);
quick_Sort(a, l, m - 1);
quick_Sort(a, m + 1, r);
}
}
void ReadFromFile()
{
extern vector<int> d;
fstream InFile("E:/QT/QuickSort/data.txt");
int ele = 0;
while (!InFile.eof())
{
InFile >> ele;
d.push_back(ele);
}
d.pop_back();
}
int main()
{
clock_t start, end;
double totalTime;
start = clock();
//vector<int> ss{ 2,7,1,6,3,0,5,8,11,3,2,4,-1 };
//cout << "Unsort: ";
//printVector(ss);
ReadFromFile();
cout << d.size() << endl;
system("pause");//暂停以确认是否完全都入
quick_Sort(d, 0, d.size()-1);
//cout << "Sorted: ";
printVector(d);
cout<<endl<<" count : "<<::count<<endl;
cout<<endl<<" CNT : "<<CNT<<endl;
end = clock();
totalTime = (double)(end - start) / CLOCKS_PER_SEC;//记录程序运行时间
cout << endl<<" Total Running Time : " << totalTime << " seconds !" << endl;
system("pause");
return 0;
}
- 选取最后一个元素为主元
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>
//快速排序法
using namespace std;
vector<int> d;
int CNT = 0; static int count = 0;
void printVector(const vector<int> &a)
{
for (int i = 0; i < a.size(); i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void swap_element(vector<int> &a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
int partition(vector<int> &a, int l, int r)
{
CNT += r - l;
int i, j, x;
i = l - 1, x = a[r]; //选取最后一个元素作为主元
for (j = l;j < r; j++)
{
::count++;
if (a[j] < x)
swap_element(a, ++i, j);
}
swap_element(a, i + 1, r);
return i + 1;
}
void quick_Sort(vector<int> &a, int l,int r)
{
if (l < r)
{
int m = partition(a,l,r);
quick_Sort(a, l, m - 1);
quick_Sort(a, m + 1, r);
}
}
void ReadFromFile()
{
extern vector<int> d;
fstream InFile("data.txt");
int ele = 0;
while (!InFile.eof())
{
InFile >> ele;
d.push_back(ele);
}
}
int main()
{
clock_t start, end;
double totalTime;
start = clock();
//vector<int> ss{ 2,7,1,6,3,0,5,8,11,3,2,4,-1 };
//cout << "Unsort: ";
//printVector(ss);
ReadFromFile();
cout << d.size() << endl;
system("pause");
quick_Sort(d, 0, d.size()-1);
//cout << "Sorted: ";
printVector(d);
cout << endl << "CNT : "<<CNT << endl;
cout << endl << "count : " << ::count << endl;
end = clock();
totalTime = (double)(end - start) / CLOCKS_PER_SEC;
cout << "\nTotal Running Time :" << totalTime << "seconds!" << endl;
system("pause");
return 0;
}
- 随机化,partition前将最后一个元素与随机位置元素交换数值
#include <stdio.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#define MIN 0
//快速排序法
using namespace std;
vector<int> d;
int CNT = 0; static int count = 0;
void printVector(const vector<int> &a)
{
for (int i = 0; i < a.size(); i++)
{
cout << a[i] << " ";
}
cout << endl;
}
void swap_element(vector<int> &a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
int partition(vector<int> &a, int l, int r)
{
srand((unsigned)time(NULL)); //初始化随机数种子
int MAX = a.size();
//cout << rand() % d.size() << endl;//控制随机数范围
int RAND = rand() % MAX;
swap_element(a,r, RAND); //随机交换
CNT += r - l;
int i, j, x;
i = l - 1, x = a[r]; //选取最后一个元素作为主元
for (j = l;j < r; j++)
{
::count++;
if (a[j] < x)
swap_element(a, ++i, j);
}
swap_element(a, i + 1, r);
return i + 1;
}
void quick_Sort(vector<int> &a, int l,int r)
{
if (l < r)
{
int m = partition(a,l,r);
quick_Sort(a, l, m - 1);
quick_Sort(a, m + 1, r);
}
}
void ReadFromFile()
{
extern vector<int> d;
fstream InFile("data.txt");
int ele = 0;
while (!InFile.eof())
{
InFile >> ele;
d.push_back(ele);
}
}
int main()
{
clock_t start, end;
double totalTime;
start = clock();
//vector<int> ss{ 2,7,1,6,3,0,5,8,11,3,2,4,-1 };
//cout << "Unsort: ";
//printVector(ss);
ReadFromFile();
cout << d.size() << endl;
system("pause");
quick_Sort(d, 0, d.size()-1);
//cout << "Sorted: ";
printVector(d);
cout << endl << "CNT : "<<CNT << endl;
cout << endl << "count : " << ::count << endl;
end = clock();
totalTime = (double)(end - start) / CLOCKS_PER_SEC;
cout << "\nTotal Running Time :" << totalTime << "seconds!" << endl;
system("pause");
return 0;
}