快速排序(算法导论版)
version1:左边第一个元素为主元
#include<iostream>
#include<ctime>
#include<stdlib.h>
using namespace std;
int Partition(int A[], int left, int right)//
{
int x = A[left];//主元
int i = left;
for (int j = left+1; j <=right; j++)
{
if (A[j] < x)
{
i++;
// if(left!=i)
swap(A[i], A[j]);
}
}
swap(A[i], A[left]);
return i;
}
void swap(int &x1, int &x2)
{
int temp;
temp = x2;
x2 = x1;
x1 = temp;
}
void QuickSort(int A[], int left, int right)
{
if (left < right){
int q = Partition(A, left, right);
QuickSort(A, left, q - 1);
QuickSort(A, q + 1, right);
}
}
int main()
{
srand((unsigned)time(NULL));
int A[999];
for (int i = 0; i < 99; i++)
A[i] = rand();
QuickSort(A, 0, 99);
for (int i = 1; i != 99; i++)
cout << A[i] << " ";
system("pause");
return 0;
}
version2:右边最后一个元素为主元
#include<iostream>
#include<ctime>
#include<stdlib.h>
using namespace std;
int Partition(int A[],int left,int right)//
{
int x = A[right];
int i = left - 1;
for (int j = left; j <= right - 1; j++)
{
if (A[j] <= x)
{
i++;
swap(A[i], A[j]);
}
}
swap(A[i + 1], A[right]); //这个版本的swap的是<em>i+1 而不是左边版本的i(因为右边为主元的版本 i+1才是比主元大的“第一个数字”,应该把它换到右边)</em>
return i + 1; //同上
}
void swap(int &x1, int &x2)
{
int temp;
temp = x2;
x2 = x1;
x1 = temp;
}
void QuickSort(int A[], int left, int right)
{
if (left < right){
int q = Partition(A, left, right);
QuickSort(A, left, q - 1);
QuickSort(A, q+1, right);
}
}
int main()
{
srand((unsigned)time(NULL));
int A[999];
for (int i = 0; i < 99; i++)
A[i] = rand();
QuickSort(A, 0, 99);
for (int i = 1; i !=99; i++)
cout << A[i] << " ";
system("pause");
return 0;
}
2.BIS
#include<iostream> //折半插入排序
#include<ctime>
using namespace std;
void BIS(int A[], const int left, const int right)//在lefr到i-1的地方 查找i要插入的位置.
{
int temp;
int low, high, middle;
int i;
for ( i = left + 1; i <=right; i++){
temp = A[i]; low = left; high = i - 1;
while (low <= high){ //与直接插入相比,不再是一个一个地比较,引入了二分搜索.
middle = (low + high) / 2;
if (temp < A[middle])
high = middle - 1;
else low = middle + 1;
}
for (int k = i - 1; k >= low; k--)
{ A[k + 1] = A[k];}
A[low] = temp;
}
}
/*int main()
{
srand((unsigned)time(NULL));
int B[10000];
for (int i = 0; i != 10000; i++)
B[i] = rand();
BIS(B,0,9999);
for (int i = 0; i != 100; i++)
cout << B[i] << " ";
system("pause");
return 0;
}*/
3.冒泡排序
#include<iostream>//冒泡排序
#include<ctime>
using namespace std;
void Bubble(int A[], int n){
bool exchange;
for (int i = 1; i < n; i++)
{
exchange = false;
for (int j = 1; j <= n - i; j++)
{
if (A[j - 1]>A[j])
swap(A[j - 1], A[j]);
exchange = true;
}
if (exchange == false)
return;
}
}
/*int main()
{
srand((unsigned)time(NULL));
int B[10000];
for (int i = 0; i != 10000; i++)
B[i] = rand();
Bubble(B, 10000);
for (int i = 0; i != 10000; i++)
cout << B[i] << " ";
system("pause");
return 0;
}*/
4.直接插入
#include<iostream> //插入排序
#include<ctime>
using namespace std;
void InsertSort(int A[], const int left, const int right){
int temp; int j;
for (int i = left + 1; i <= right; i++){
if (A[i-1] > A[i]){
temp = A[i]; j = i - 1;
do{
A[j + 1] = A[j]; j--;
} while (j >= left && temp < A[j]);
A[j + 1] = temp;
}
}
}
void InsertSort_1(int A[], const int left, const int right){
int temp;
for (int i = left + 1; i <= right; i++){
if (A[i] < A[i - 1]){
temp = A[i]; int back = i - 1;//j做为要后退的数组标号.
do{
A[back + 1] = A[back]; back--;
} while (back >= left && temp < A[back]);
A[back + 1] = temp;
}
}
}
/*int main()
{
srand((unsigned)time(NULL));
int B[10000];
for (int i = 0; i != 10000; i++)
B[i] = rand();
InsertSort_1(B, 0,9999);
for (int i = 0; i != 100; i++)
cout << B[i] << " ";
system("pause");
return 0;
}*/
5.归并排序
#include<iostream>
using namespace std;
//将有二个有序数列a[first...mid]和a[mid...last]合并。
void mergearray(int a[], int first, int mid, int last, int temp[])
{
int i = first, j = mid + 1;
int m = mid, n = last;
int k = 0;
while (i <= m && j <= n)
{
if (a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while (i <= m)
temp[k++] = a[i++];
while (j <= n)
temp[k++] = a[j++];
for (i = 0; i < k; i++)
a[first + i] = temp[i];
}
void mergesort(int a[], int first, int last, int temp[])
{
if (first < last)
{
int mid = (first + last) / 2;
mergesort(a, first, mid, temp); //左边有序
mergesort(a, mid + 1, last, temp); //右边有序
mergearray(a, first, mid, last, temp); //再将二个有序数列合并
}
}
/*bool MergeSort(int a[], int n)
{
int *p = new int[n];
if (p == NULL)
return false;
mergesort(a, 0, n - 1, p);
delete[] p;
return true;
}*/
/*int main()
{
int A[] = { 3, 4, 5, 6, 1, 2, 10, 9, 8, 7 };
int B[10];
mergesort(A, 0, 9, B);
for (int i = 0; i != 10; i++)
cout << B[i] << " ";
return 0;
}*/
6.希尔排序
#include<iostream>
#include<ctime>
using namespace std;
void ShellSort(int A[],int left,int right)
{
int gap = left + right - 1;
int temp;
do{
gap = gap / 3 + 1;
for (int i = left+gap; i <= right; i++)
{
temp = A[i]; int j = i - gap;
if (temp < A[i - 1])
do{
A[j + gap] = A[j];
j = j - gap;
} while (j >= left &&temp < A[j]);
A[j + gap] = temp;
}
} while (gap>1);
}
/*int main()
{
srand((unsigned)time(NULL));
int B[10000];
for (int i = 0; i != 10000; i++)
B[i] = rand();
ShellSort(B, 0, 9999);
for (int i = 0; i != 100; i++)
cout << B[i] << " ";
system("pause");
return 0;
}*/