《大話數據結構》C++實現七大排序算法詳細代碼

       如下圖所示的代碼,是《大話數據結構》第9章節中的七大排序算法彙總,本人寫了一個main主函數來進行算法排序的測試,只要把代碼運行起來後在終端中輸入10個數字然後回車就能把所有排序算法的結果打印出來,如果需要輸入其他數目的數字,只需要修改代碼中最前面定義的MAXSIZE函數就行。

 

#include<iostream>

using namespace std;

constexpr auto MAXSIZE = 10;

typedef int status;

typedef struct
{
	int r[MAXSIZE + 1];
	int length;
}SqList;

void swap(SqList* L, int i, int j);

//簡單比較排序
void BubbleSort0(SqList* L);

//冒泡排序函數聲明
void BubbleSort(SqList* L);

//插入排序函數聲明
void InsertSort(SqList* L);

//希爾排序函數聲明
void ShellSort(SqList *L);

//堆排序用到的函數聲明
void HeapSort(SqList *L);
void HeapAdjust(SqList *L,int s,int m);

//歸併排序用到的函數聲明
void MergeSort(SqList *L);
void MSort(int SR[],int TR1[],int s,int t);
void Merge(int SR[],int TR[],int i,int m,int n);

//快速排序用到的函數聲明
void QuickSort(SqList* L);
void QSort(SqList *L,int low,int high);
int Partition(SqList *L,int low,int high);

/*交換L中數組r的下標爲i和j的值*/
void swap(SqList* L, int i, int j)
{
	int temp = L->r[i];
	L->r[i] = L->r[j];
	L->r[j] = temp;
}

//一、直接簡單比較來排序
void BubbleSort0(SqList* L)
{
	for (int i = 1; i < L->length; i++)
	{
		for (int j = i + 1; j <= L->length; j++)
		{
			if (L->r[i] > L->r[j])
			{
				swap(L, i, j);
			}
		}
	}
}

//二、冒泡排序
void BubbleSort(SqList* L)
{
	for (int i = 1; i < L->length; i++)
	{
		for (int j = L->length - 1; j >= i; j--)
		{
			if (L->r[j] > L->r[j + 1])
			{
				swap(L, j, j + 1);
			}
		}
	}
}


//三、直接插入排序
void InsertSort(SqList* L)
{
	int i, j;
	for (i = 2; i <= L->length; i++)
	{
		if (L->r[i] < L->r[i - 1])
		{
			L->r[0] = L->r[i];
			for (j = i - 1; L->r[j] > L->r[0]; j--)
			{
				L->r[j + 1] = L->r[j];
			}
			L->r[j + 1] = L->r[0];
		}
	}
}

//四、希爾排序
void ShellSort(SqList* L)
{
	int i, j;
	int increment = L->length;
	do
	{
		increment = increment / 3 + 1;
		for ( i = 1+increment; i <= L->length; i++)
		{
			if (L->r[i]<L->r[i-increment])
			{
				L->r[0] = L->r[i];
				for ( j = i-increment; j > 0 && L->r[0]<L->r[j]; j-=increment)
				{
					L->r[j + increment] = L->r[j];
				}
				L->r[j + increment] = L->r[0];
			}
		}
	} while (increment>1);
}

//五、堆排序
void HeapSort(SqList* L)
{
	//把L中的順序表r構建成大頂堆的數字排列
	int i;
	for ( i = L->length; i > 0; i--) 
	{
		HeapAdjust(L,i,L->length);
	}

	//開始不斷循環進行堆排序(截取根節點)和不斷進行堆調整
	for ( i = L->length; i > 1; i--)
	{
		swap(L,1,i);
		HeapAdjust(L,1,i-1);
	}
}

//大頂堆構建函數
void HeapAdjust(SqList* L, int s, int m)
{
	int temp, j;
	temp = L->r[s];
	for ( j = 2*s; j <= m; j*=2)
	{
		if (j<m && L->r[j]<L->r[j+1])
		{
			++j;
		}

		if (temp>=L->r[j])
		{
			break;
		}

		L->r[s] = L->r[j];
		s = j;
	}
	L->r[s] = temp;
}



//六、歸併排序
void MergeSort(SqList* L)
{
	MSort(L->r,L->r,1,L->length);
}

void MSort(int SR[], int TR1[], int s, int t)
{
	int m;
	int TR2[MAXSIZE+1];
	if (s==t)
	{
		TR1[s] = SR[s];
	}
	else
	{
		m = (s + t) / 2;
		MSort(SR, TR2, s, m);
		MSort(SR, TR2,m+1, t);
		Merge(TR2,TR1,s,m,t);
	}
}

void Merge(int SR[], int TR[], int i, int m, int n)
{
	int j, k, l;
	for ( j = m+1,k=i;i<=m && j<=n; k++)
	{
		if (SR[i]<SR[j])
		{
			TR[k] = SR[i];
			i++;
		}
		else
		{
			TR[k] = SR[j];
			j++;
		}
	}

	if (i<=m)
	{
		for ( l = 0; l <= m-i; l++)
		{
			TR[k + l] = SR[i + l];
		}
	}
	if (j<=n)
	{
		for (l = 0; l <= n - j; l++)
		{
			TR[k + l] = SR[j + l];
		}
	}
}

//七、快速排序
void QuickSort(SqList* L)
{
	QSort(L,1,L->length);
}

void QSort(SqList* L, int low, int high)
{
	int pivot;
	if (low<high)
	{
		pivot = Partition(L,low,high);

		QSort(L,low,pivot-1);
		QSort(L,pivot+1,high);
	}
}

int Partition(SqList* L, int low, int high)
{
	int pivotkey;
	pivotkey = L->r[low];
	while (low<high)
	{
		while (low<high && L->r[high]>=pivotkey)
		{
			high--;
		}
		swap(L,low,high);
		while (low<high && L->r[low]<=pivotkey)
		{
			low++;
		}
		swap(L,low,high);
	}
	return low;
}

int main()
{
	cout << "請輸入" << MAXSIZE << "個數字:" << endl;
	int num;
	SqList L;
	L.length = MAXSIZE;

	for (int i = 1; i <= MAXSIZE; i++)
	{
		cin >> L.r[i];
	}

	//簡單排序並輸出排序結果
	BubbleSort0(&L);
	cout << "簡單排序後輸出的結果是:" << endl;
	for (int i = 1; i <= MAXSIZE; i++)
	{
		cout << L.r[i] << " ";
	}
	cout << endl;

	//冒泡排序並輸出排序結果
	BubbleSort(&L);
	cout << "冒泡排序後輸出的結果是:" << endl;
	for (int i = 1; i <= MAXSIZE; i++)
	{
		cout << L.r[i] << " ";
	}
	cout << endl;

	//直接插入排序並輸出排序結果
	InsertSort(&L);
	cout << "直接插入排序後輸出的結果是:" << endl;
	for (int i = 1; i <= MAXSIZE; i++)
	{
		cout << L.r[i] << " ";
	}
	cout << endl;

	//希爾排序並輸出排序結果
	ShellSort(&L);
	cout << "希爾排序後輸出的結果是:" << endl;
	for (int i = 1; i <= MAXSIZE; i++)
	{
		cout << L.r[i] << " ";
	}
	cout << endl;

	//堆排序並輸出排序結果
	HeapSort(&L);
	cout << "堆排序後輸出的結果是:" << endl;
	for (int i = 1; i <= MAXSIZE; i++)
	{
		cout << L.r[i] << " ";
	}
	cout << endl;

	//歸併排序並輸出排序結果
	MergeSort(&L);
	cout << "歸併排序後輸出的結果是:" << endl;
	for (int i = 1; i <= MAXSIZE; i++)
	{
		cout << L.r[i] << " ";
	}
	cout << endl;

	//快速排序並輸出排序結果
	QuickSort(&L);
	cout << "快速排序後輸出的結果是:" << endl;
	for (int i = 1; i <= MAXSIZE; i++)
	{
		cout << L.r[i] << " ";
	}
	cout << endl;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章