這就不是作業了
因為Sort很Easy寫出來
這版本聽說是聖經本版本
會寫的原因是因為最近借了本書
叫 "Beautiful Code 美麗程式"
因為從來沒寫過漂亮的程式
所以要來訓練一下
一開始是講QuickSort
雞仔猜應該也是改進效率之類的
所以就花一點時間把簡單的QuickSort寫出來
在慢慢照著書裡去修改
等書看完再寫心得 & 筆記
以下是原始的Code
/*
QuickSort
*/
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;
#define Size 100
void Swap(int &a, int &b)
{
int buf;
buf = a;
a = b;
b = buf;
}
int Partition(int *a,int p,int r)
{
int i,j,x;
x = a[r];
i = p-1;
for(j = p;j<r;j++)
{
if( a[j] < x )
{
i++;
Swap(a[i],a[j]);
}
}
Swap(a[i+1],a[r]);
return i+1;
}
void QuickSort(int *a,int p,int r)
{
int mid;
if( p < r )
{
mid = Partition(a,p,r);
QuickSort(a,p,mid-1);
QuickSort(a,mid+1,r);
}
}
void main()
{
srand(time(0));
int *array = new int[Size];
int p = 0 , r = 10; //p is front , r is rail
cout<<"QuickSort:"<<endl<<endl<<"NonSort: ";
for(int i = p;i<=r;i++)
{
array[i] = rand() % 30;
cout<<setiosflags(ios::right)<<setw(2)<<array[i]<<" ";
}
cout<<endl;
QuickSort(array,p,r);
cout<<" Sort: ";
for(i = p;i<=r;i++)
{
cout<<setiosflags(ios::right)<<setw(2)<<array[i]<<" ";
}
cout<<endl<<endl;
system("PAUSE");
}