冒泡排序(bubble sort)

接下來的幾篇博客都是關於排序的;主要有插入類排序;交換類排序;選擇類排序;

插入類排序主要有直接插入如排序(straight insertion sort);折半插入排序(bianry insertion sort); Shell sort;

交換類排序主要有冒泡排序(bubble sort); 快速排序(quick sort);

選擇類排序主要有;簡單選擇排序(simple selection sort); 堆排序(heap sort);

除此之外還有歸併排序(merge sort); 基數排序等;

本篇博客是關於冒泡排序(bubble sort)的;

排序直接的數據結構介紹;

            所有的排序均以線性表存儲;所有的排序算法避免不了交換;交換就要用到臨時變量;故將線性表中編號爲0的元素不存儲任何有效元素;僅僅當做臨時變量或者記錄的作用來使用;對於有些算法我會給出局部變量做臨時變量的算法;若對線性表不是很瞭解;參見前面關於線性表的博客;

 

頭文件(sort.h);

# ifndef _SORT_

typedef int KeyType;

typedef struct
{
	KeyType key;
}DataType;

# endif

頭文件(SeqList.h);

typedef struct
{
	DataType data[MAXSIZE];
	int length;
}SeqList, * PSeqList;

//線性表排序基本準備;
PSeqList Init_SeqList(void);
bool Full_SeqList(PSeqList p);
int Push_SeqList(PSeqList p, KeyType keyValue);
void Traversal_SeqList(PSeqList p);
ostream & operator<<(ostream & os, DataType dataValue);

//基於線性表的排序方法;默認從小到大;
void StraightInsertSort(PSeqList p);
void BinaryInsertSort(PSeqList p);
void ShellInsert(PSeqList p, int gap);
void ShellSort(PSeqList p, int * gaps, int len);
void BubbleSort(PSeqList p);

實現文件(SeqList.cpp);

# include "SeqList.h"

PSeqList Init_SeqList(void)
{
	PSeqList p = (PSeqList)malloc(sizeof(SeqList));

	if (NULL != p)
	{
		p->length = 0;
		return p;
	}
	else
	{
		cout << "Memory allocate is error! " << endl;
		system("pause");
		exit(0);
	}
}


bool Full_SeqList(PSeqList p)
{
	if (p->length >= MAXSIZE)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int Push_SeqList(PSeqList p, KeyType keyValue)
{
	if (Full_SeqList(p))
	{
		cout << "SeqList is full! " << endl;

		return -1;
	}

	p->data[p->length].key = keyValue;
	p->length++;

	return 0;
}

void Traversal_SeqList(PSeqList p)
{
	for (int i = 0; i < p->length; i++)
	{
		cout << p->data[i] << " ";
	}
	cout << endl;

	return;
}

ostream & operator<<(ostream & os, DataType dataValue)
{
	cout << dataValue.key;

	return os;
}

//基於線性表的排序方法;默認從小到大;

//直接插入排序;
void StraightInsertSort(PSeqList p)
{
	int i = 0;
	int j = 0;

	for (i = 2; i < p->length; i++)
	{
		//複製到前哨站;
		p->data[0] = p->data[i];

		j = i - 1;
		while (p->data[0].key < p->data[j].key)//必須是大於;不然不是穩定排序;
		{
			//移動元素;
			p->data[j + 1] = p->data[j];
			j--;
		}

		//元素最終移入正確位置;
		p->data[j + 1] = p->data[0];
	}

	return;
}

//折半插入排序;
void BinaryInsertSort(PSeqList p)
{
	int i = 0;
	int j = 0;
	int low = 0;
	int mid = 0;
	int high = 0;

	for (i = 2; i < p->length; i++)
	{
		//複製到前哨站;
		p->data[0] = p->data[i];

		//查找插入位置;
		low = 1;
		high = i - 1;
		while (low <= high)
		{
			mid = (low + high) / 2;

			if (p->data[0].key >= p->data[mid].key)//要麼是大於等於要麼是小於;即是相反;不然不是穩定排序;
			{
				low = mid + 1;
			}
			else
			{
				high = mid - 1;
			}
		}

		//移動元素騰出插入位置;這裏high + 1爲最終插入位置;
		for (j = i - 1; j >= high + 1; j--)
		{
			p->data[j + 1] = p->data[j];
		}

		//元素最終移入正確位置;
		p->data[j + 1] = p->data[0];
	}

	return;
}

//Shell插入;
void ShellInsert(PSeqList p, int gap)
{
	int i = 0;
	int j = 0;

	for (i = gap + i; i < p->length; i++)
	{
		if (p->data[i].key < p->data[i - gap].key)
		{
			p->data[0] = p->data[i];

			for (j = i - gap; (j > 0) && (p->data[0].key < p->data[j].key); j = j - gap)
			{
				p->data[j + gap] = p->data[j];
			}
			p->data[j + gap] = p->data[0];
		}
	}

	return;
}

//Shell排序;
void ShellSort(PSeqList p, int * gaps, int len)
{
	int i = 0;

	for (i = 0; i < len; i++)
	{
		ShellInsert(p, gaps[i]);
	}

	return;
}

//冒泡排序;
void BubbleSort(PSeqList p)
{
	int i = 0;
	int j = 0;

	for (i = 1; i < p->length - 1; i++)
	{
		for (j = 1; j < p->length - i; j++)
		{
			if (p->data[j].key > p->data[j + 1].key)
			{
				p->data[0] = p->data[j];
				p->data[j] = p->data[j + 1];
				p->data[j + 1] = p->data[0];
			}
		}
	}

	return;
}



主函數(Main.cpp);

# include "SeqList.h"

int main(int argc, char ** argv)
{
	int val = 0;
	PSeqList p = Init_SeqList();

	for (int i = 0; i <= 10; i++)
	{
		Push_SeqList(p, 11 - i);
	}

	cout << "---------------Sort befor---------------" << endl;
	Traversal_SeqList(p);

	cout << endl << "---------------Sort after---------------" << endl;

	//StraightInsertSort(p);
	//BinaryInsertSort(p);
	BubbleSort(p);
	//QuickSort(p, 1, 10);
	//SelectSort(p);
	//HeapSort(p);
	//MergeSort(p->data, p->data, 1, p->length - 1);
	//StraightInsertAllSort(p);
	//BinaryInsertAllSort(p);

	Traversal_SeqList(p);

	system("pause");
	return 0;
}

上面的算法只是對{ 1..........N}的元素進行排序;下面給出對{ 0.......N}的元素進行排序的算法;

分析;前面沒有排序0號元素是因爲0號元素被當做臨時變量使用了,現在自定義一個臨時變量代替它就可以了;

void BubbleAllSort(PSeqList p)
{
	int i = 0;
	int j = 0;
	DataType tem;

	for (i = 0; i < p->length - 1; i++)
	{
		for (j = 0; j < p->length - 1 - i; j++)
		{
			if (p->data[j].key > p->data[j + 1].key)
			{
				tem = p->data[j];
				p->data[j] = p->data[j + 1];
				p->data[j + 1] = tem;
			}
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章