Data Structures (八) 排序

一、排序的稳定性。

假设ki=kj(i!=j),并且在排序前的系列中ki领先kj。如果排序后ki仍然领先去kj,则此排序方法是稳定的,反之则不稳定。

二、内排序和外排序

内排序在排序过程中,待排序的所有记录全部被放置在内存中。外排序是由于排序的记录个数太多,不能同时放在内存,整个过程要在内外存之间多次交换数据。我们主要研究内排序。

影响内排序性能的三个方面:

1、时间性能;

2、辅助空间;

3、算法的复杂度;

三、排序方法

1、冒泡排序:两两相邻记录的关键字比较,如果反序则交换,直到没有记录。

void Swap(int *x1, int *x2)
{
	int temp = *x1;
	*x1 = *x2;
	*x2 = temp;
}

void printArr(int *arr, int length)
{
	for (int i = 0; i < length; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}
///冒泡排序
///冒牌排序的初阶---简单易懂,效率低
void BubbleSort0(int *arr, int length)
{
	for (int i = 0; i < length; i++)
	{
		for (int j = i; j < length; j++)
		{
			if (arr[i] > arr[j])
			{
				Swap(&arr[i], &arr[j]);
			}
		}
	}
}

///正宗的冒泡排序
void BubbleSort1(int *arr, int length)
{
	for (int i = 0; i < length; i++)
	{
		for (int j = length - 1; j >= i; j--)
		{
			if (arr[j - 1] > arr[j])
			{
				Swap(&arr[j - 1], &arr[j]);
			}
		}
	}
}

///冒泡排序的优化
void BubbleSort2(int *arr, int length)
{
	//添加一个flag,初始值为false,如果产生了数据交换,就把它设为true,如果没有数据交换,说明此趟所有关键字已经排好序,退出循环
	bool flag = true;
	for (int i = 1; i < length&&flag; i++)
	{
		flag = false;
		for (int j = length - 1; j >= i; j--)
		{
			if (arr[j - 1] > arr[j])
			{
				Swap(&arr[j - 1], &arr[j]);
				flag = true;
			}
		}
	}
}

冒泡排序的时间复杂度,最好是O(n),最差是

2、简单选择排序

就是通过n-1次关键字的比较,从n-i+1个记录中选出最小的记录,并和第i个交换

///简单选择排序
void SelectSort(int *arr, int length)
{
	int min;
	for (int i = 0; i < length; i++)
	{
		min = i;
		for (int j = i + 1; j < length; j++)
		{
			if (arr[min] > arr[j])
			{
				min = j;
			}
		}
		if (i != min)
		{
			Swap(&arr[min], &arr[i]);
		}
	}
}

简单选择排序的时间复杂度为,但是简单选择排序的性能略优于冒泡排序。

 

3、直接插入排序

将一个记录插入到一个已经排好的有序表中,从而得到一个新的、记录数+1的有序表

///直接插入排序
void InsertSort(int *arr, int length)
{
	int i, j;
	for (i = 1; i < length; i++)
	{
		if (arr[i - 1] > arr[i])
		{
			int minner = arr[i];
			for (j = i - 1; arr[j] > minner; j--)
			{
				arr[j + 1] = arr[j];
			}
			arr[j + 1] = minner;
		}
	}
}

直接插入排序的时间复杂度为

4、希尔排序

void shellsort(int *arr, int length)
{
	int i;
	int d = length / 2;
	while (d > 0)
	{
		i = 0;
		while (i < length && (i + d) < length)
		{
			if (arr[i] > arr[i + d])
			{
				Swap(&arr[i], &arr[i + d]);
			}
			i++;
		}
		d = d / 2;
	}
}

希尔排序的时间复杂的,比直接插入排序好。

5、堆排序

void siftDown(int *arr, int length, int root)
{
	int parent = root;
	int child = parent * 2 + 1;
	while (child < length)
	{
		if (child + 1 < length&&arr[child] > arr[child + 1])
			child++;
		if (arr[child] < arr[parent])
		{
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

//升序  建大堆
//降序  建小堆
void Heapsort(int *arr, int length)
{
	assert(arr);
	for (int i = (length - 2) / 2; i >= 0; i--)
	{
		siftDown(arr, length, i);
	}
	int end = length - 1;
	while (end > 0)
	{
		Swap(&arr[0], &arr[end]);
		siftDown(arr, end, 0);
		--end;
	}
}

void heapTopK(int *arr, int length, int k)
{
	assert(arr);
	int *heap = (int*)malloc(sizeof(int)*k);
	for (int i = 0; i < k; i++)
	{
		heap[i] = arr[i];
	}
	for (int i = (k - 2) / 2; i >= 0; i--)
	{
		siftDown(heap, k, i);
	}
	for (int i = k; i < length; i++)
	{
		if (arr[i] > heap[0])
		{
			heap[0] = arr[i];
		}
		siftDown(heap, k, 0);
	}
	for (int i = 0; i < k; i++)
	{
		cout << heap[i] << ' ';
	}
	cout << endl;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ Data Structures and Algorithms: Build effective, maintainable and readable code in C++ By 作者: Wisnu Anggoro ISBN-10 书号: 1788835212 ISBN-13 书号: 9781788835213 出版日期: 2018-06-11 pages 页数: 409 $44.99 Book Description to Finelybook sorting C++ is a general-purpose programming language which has evolved over the years and is used to develop software for many different sectors. This book will be your companion as it takes you through implementing classic data structures and algorithms to help you get up and running as a confident C++ programmer. We begin with an introduction to C++ data structures and algorithms while also covering essential language constructs. Next, we will see how to store data using linked lists, arrays, stacks, and queues. Then, we will learn how to implement different sorting algorithms, such as quick sort and heap sort. Along with these, we will dive into searching algorithms such as linear search, binary search and more. Our next mission will be to attain high performance by implementing algorithms to string datatypes and implementing hash structures in algorithm design. We’ll also analyze Brute Force algorithms, Greedy algorithms, and more. By the end of the book, you’ll know how to build components that are easy to understand, debug, and use in different applications. Contents 1: LEARNING DATA STRUCTURES AND ALGORITHMS IN C++ 2: STORING DATA IN LISTS AND LINKED LISTS 3: CONSTRUCTING STACKS AND QUEUES 4: ARRANGING DATA ELEMENTS USING A SORTING ALGORITHM 5: FINDING OUT AN ELEMENT USING SEARCHING ALGORITHMS 6: DEALING WITH THE STRING DATA TYPE 7: BUILDING A HIERARCHICAL TREE STRUCTURE 8: ASSOCIATING A VALUE TO A KEY IN A HASH TABLE 9: IMPLEMENTATION OF ALGORITHMS IN REAL LIFE What You Will Learn Know how to use arrays and lists to get better results in complex scenarios Build enhanced applications by using hashtables, dictionaries, and sets Implement searching algorithms such as linear search, binary search, jump search, exponential search, and more Have a positive impact on the efficiency of applications with tree traversal Explore the design used in sorting algorithms like Heap sort, Quick sort, Merge sort and Radix sort Implement various common algorithms in string data types Find out how to design an algorithm for a specific task using the common algorithm paradigms Authors Wisnu Anggoro Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network Programming – Second Edition and Functional C# by Packt. He has been programming since he was in junior high school, which was about 20 years ago, and started developing computer applications using the BASIC programming language in the MS-DOS environment. He has solid experience in smart card programming, as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication, and other smart card applications that require the use of C# and C/C++. He is currently a senior smart card software engineer at CIPTA, an Indonesian company that specializes in innovation and technology for smart cards. He can be reached through his email at wisnu@anggoro.net

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值