小言_互联网的博客

【面试复习】| 常见排序算法手写

587人阅读  评论(0)

快速排序

#include<iostream>
using namespace std;

int Func(int arr[], int start, int end)
{
	int temp = arr[start];
	while (start < end)
	{
		while (start < end&&arr[end] > temp)
		{
			end--;
		}
		arr[start] = arr[end];
		while (start < end&&arr[start] < temp)
		{
			start++;
		}
		arr[end] = arr[start];
	}
	arr[start] = temp;
	return start;
}
void quicksort(int arr[], int start, int end)
{
	int key = Func(arr, start, end);	//找到枢纽位置
	if (start < end)
	{
		quicksort(arr, start, key - 1);	//递归小于枢纽值的部分
		quicksort(arr, key + 1, end);//递归大于枢纽值的部分
	}
}

//Test函数
int main()
{
	int arr[] = { 9, 5, 7, 8, 3, 6, 4, 1, 2 };
	int n = sizeof(arr) / sizeof(arr[0]);
	quicksort(arr, 0, n - 1);
	for (int i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	return 0;
}

归并排序

#include<iostream>
using namespace std;
void Merge(int *tmp, int *arr, int low, int mid, int high)
{
	int i = low, j = mid + 1, k = low;
	while (i != mid + 1 && j != high+1)
	{
		if (arr[i] < arr[j])
			tmp[k++] = arr[i++];
		else
			tmp[k++] = arr[j++];
	}
	while (i != mid + 1)
		tmp[k++] = arr[i++];
	while (j != high+1)
		tmp[k++] = arr[j++];
	for (i = low; i <= high; i++)
		arr[i] = tmp[i];
}

void Mergesort(int *tmp, int *arr, int low, int high)
{
	int mid = low + (high - low) / 2;
	if (low < high)
	{
		Mergesort(tmp, arr, low, mid);
		Mergesort(tmp, arr, mid+1, high);
		Merge(tmp, arr, low, mid, high);
	}
}

//Test函数
int main()
{
	int arr[] = { 9, 5, 7, 8, 3, 6, 4, 1, 2 };
	int n = sizeof(arr) / sizeof(arr[0]);
	int tmp[9];
	Mergesort(tmp,arr, 0, n - 1);
	for (int i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	return 0;
}

堆排序

#include<iostream>
using namespace std;

void adjust(int *arr, int size, int i)
{
	int left = 2 * i + 1;
	int right = 2 * i + 2;
	int maxid = i;
	if (left < size && arr[left] > arr[maxid])
	{
		maxid = left;
	}
	if (right < size&&arr[maxid] < arr[right])
	{
		maxid = right;
	}
	if (arr[maxid] != arr[i])
	{
		swap(arr[maxid], arr[i]);
		adjust(arr, size, maxid);
	}
}
void bubblesort(int *arr, int size)
{
	for (int i = size /2-1; i >= 0; --i)
	{
		adjust(arr, size, i);	//调整所有非叶子节点 5 4 3 2...
	}
	for (int i = size - 1; i > 0; i--)
	{
		swap(arr[i], arr[0]);	//把最大的堆顶元素放到队尾
		adjust(arr, i, 0);	//调整除过队尾剩余的元素
	}
}

//Test函数
int main()
{
	int arr[] = { 9, 5, 7, 8, 3, 6, 4, 1, 2 };
	int n = sizeof(arr) / sizeof(arr[0]);
	bubblesort(arr, n);
	for (int i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	return 0;
}

希尔排序

#include<iostream>
using namespace std;
void hillsort(int *arr,int size)
{
	for (int gap = size / 2; gap >= 1; gap = gap / 2)
	{
		for (int i = gap; i < size; ++i)
		{
			for (int j = i - gap; j >= 0 && arr[j] > arr[i]; j = j - gap)
			{
				swap(arr[j], arr[i]);
			}
		}
	}
}

//Test函数
int main()
{
	int arr[] = { 9, 5, 7, 8, 3, 6, 4, 1, 2 };
	int n = sizeof(arr) / sizeof(arr[0]);
	hillsort(arr, n);
	for (int i = 0; i < n; ++i)
	{
		cout << arr[i] << " ";
	}
	return 0;
}

转载:https://blog.csdn.net/weixin_39411321/article/details/102251230
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场