首页 > 编程语言 > 详细

面试题---各种排序。插入,选择,冒泡,shell,堆,快排

时间:2016-04-09 23:39:44      阅读:623      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<string>
#include<vector>

using namespace std;
void swap(int a[], int i, int j);

void insert_sort(int a[], int n)
{
	for (int i = 1; i < n; i++)
	{
		int temp = a[i], j = i;
		while (j&&temp < a[j - 1])
		{
			a[j] = a[j - 1];
			j--;
		}
		a[j] = temp;
	}
}

void shell_sort(int a[], int n)
{
	for (int inc = n / 2; inc; inc /= 2)
	{
		for (int i = inc; i < n; i++)
		{
			int temp = a[i],j=i;
			while (j>=inc&&temp < a[j - inc])
			{
				a[j] = a[j - inc];
				j -= inc;
			}
			a[j] = temp;
		}
	}
}


void select_sort(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		int index = i;
		int temp = a[i];
		for (int j = i+1; j < n; j++)
		{
			if (a[j] < a[index])
			{
				index = j;
			}
		}
		a[i] = a[index];
		a[index] = temp;
	}
}


void bubble_sort(int a[], int n)
{
	/*
	for (int i = n; i > 0; i--)
	{
		for (int j = 1; j < i; j++)
		{
			if (a[j] < a[j - 1])
			{
				swap(a, j, j - 1);
			}
		}
	}
	*/
	for (int i = 0; i < n; i++)
	{
		for (int j = n - 1; j>i; j--)
		{
			if (a[j] < a[j - 1])
			{
				swap(a, j, j - 1);
			}
		}
	}
}




void merge(int a[], int l, int m, int r)
{
	int *temp = new int[r - l];
	int i = 0, j = l, k = m;
	while (j < m&&k < r)
	{
		if (a[j] < a[k])
		{
			temp[i++] = a[j++];
		}
		else
		{
			temp[i++] = a[k++];
		}
	}
	while (j != m)
	{
		temp[i++] = a[j++];
	}
	while (k != r)
	{
		temp[i++] = a[k++];
	}

	for (int i = l; i < r; i++)
	{
		a[i] = temp[i - l];
	}
	delete[] temp;
	temp = nullptr;
}

void merge_sort(int a[], int l, int r)
{
	if (l + 1 >= r)
	{
		return;
	}
	int middle = (l + r) / 2;
	merge_sort(a, l, middle);
	merge_sort(a, middle, r);
	merge(a, l, middle, r);
}

void quick_sort(int a[], int l, int r)
{
	if (l + 1 >= r)
	{
		return;
	}
	int pivot = a[l];
	int i = l, j = r;
	while (true)
	{
		do
		{
			i++;
		} while (a[i] < pivot&&i<r);
		do
		{
			j--;
		} while (a[j] >= pivot&&j > l);
		if (i >= j)break;
		swap(a, i, j);
	}
	a[l] = a[j];
	a[j] = pivot;
	quick_sort(a, l, j);
	quick_sort(a, j + 1, r);
}


void shut_down(int a[], int hole,int n)
{
	while (hole < n)
	{
		int firstIndex = hole * 2 + 1;
		if (firstIndex + 1 < n&&a[firstIndex] < a[firstIndex + 1])
		{
			firstIndex++;
		}
		if (firstIndex < n&&a[hole] < a[firstIndex])
		{
			swap(a, hole, firstIndex);
		}
		hole = firstIndex;
	}
}

void make_heap(int a[], int n)
{
	for (int i = (n-1) / 2; i>=0; i--)
	{
		shut_down(a, i, n);
	}
}
void heap_sort(int a[], int n)
{
	make_heap(a, n);
	for (int i = n - 1; i; i--)
	{
		swap(a, 0, i);
		make_heap(a, i);
	}
}
void swap(int a[], int i, int j)
{
	int temp = a[i];
	a[i] = a[j];
	a[j] = temp;
}
void print(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

/*
int main()
{
	int* a = new int[10];
	for (int i = 0; i < 10; i++)
	{
		a[i] = rand() % 10;
	}
	print(a, 10);
	//insert_sort(a, 10);
	//shell_sort(a, 10);
	//select_sort(a, 10);
	//bubble_sort(a, 10);
	//merge_sort(a,0, 10);
	//quick_sort(a, 0, 10);
	//heap_sort(a, 10);
	//print(a, 10);
}
*/

  

面试题---各种排序。插入,选择,冒泡,shell,堆,快排

原文:http://www.cnblogs.com/jinweiseu/p/5372936.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!