Bootstrap

直接插入排序、希尔排序、简单选择排序、冒泡排序、快速排序、归并排序

#include<iostream>
#include<malloc.h>
using namespace std;

//直接插入排序

void print(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
}

//直接插入排序

void insert(int a[], int n)
{
	int i, j;
	for (int i = 1; i < n; i++)
	{
		int k = a[i];
		for (j = i - 1; j >= 0 && a[j] > k; j--)
		{
			a[j + 1] = a[j];
		}
		a[j + 1] = k;
	}

}

//希尔排序
void shellSort(int a[], int n)
{
	int gap = 3;
	int i, j;
	while (gap--)
	{
		for (i = gap; i < n; i++)
		{
			int k = a[i];
			for (j = i - gap; j >= 0 && a[j] > k; j -= gap)
			{
				a[j + gap] = a[j];
			}
			a[j + gap] = k;
		}
	}
}


//简单选择排序
void selectSort(int a[], int n)
{
	int i, j,min;
	for (i = 0; i < n - 1; i++)
	{
		min = i;;
		for (j = i + 1; j < n; j++)
		{
			if (a[j] < a[min])
			{
				min = j;
			}
		}
		if (min != i)
		{
			int temp = a[i];
			a[i] = a[min];
			a[min] = temp;  //注意交换的是元素,而不是下标
		}
	}
}

//冒泡排序
void bubbleSort(int a[], int n)
{
	int i, j;
	for (i = 0; i < n-1; i++)
	{
		int flag = 0;
		for (j = 0; j < n - 1 - i; j++)
		{
			if (a[j] > a[j + 1])   //注意此处是大于
			{
				int temp = a[j];
				a[j] = a[j + 1];
				a[j + 1] = temp;
				flag = 1;
			}
		}

		if (flag == 0)
		{
			break;
		}

	}

}


//快速排序方法一
//左闭右开[ )
void quickSort(int a[], int low,int high)
{
	if (low + 1 >= high)
	{
		return;
	}
	int i, j;
	int begin = low;
	int end = high - 1;
	int key = a[begin];
	while (begin < end)
	{
		while (begin < end && a[end] >= key)
		{
			end--;
		}
		a[begin] = a[end];
		while (begin < end && a[begin] <= key)
		{
			begin++;
		}
		a[end] = a[begin];
	}
	a[begin] = key;
	quickSort(a, low, begin);
	quickSort(a, begin+1,high);
}

//快速排序方法二
void quickSort2(int a[], int low,int high)
{
	if (low + 1 >= high)
	{
		return;
	}
	int i, j;
	int begin = low;
	int end = high-1;
	int key = a[begin];
	while (begin < end)
	{
		while (begin < end && a[end] >= key)
		{
			end--;
		}
		while (begin < end && a[begin] <= key)
		{
			begin++;
		}
		int temp = a[begin];
		a[begin] = a[end];
		a[end] = temp;
	}
	a[begin] = key;
	quickSort(a, low, begin);
	quickSort(a, begin + 1, high);

}

void swap(int* a, int* b)
{
	int t = *a;
	*a = *b;
	*b = t;
}


//快速排序方法三——快慢指针法
void quickSort3(int a[], int low, int high)
{
	if (low + 1 >= high)
	{
		return;
	}
	int i, j;
	int begin = low;
	int end = high - 1;

	int key = a[begin];
	int prev = begin;
	int cur = begin + 1;
	while (cur <= end)
	{
		// cur找小,把小的往前翻,大的往后翻
		if (a[cur] < key && ++prev != cur)
			swap(&a[cur], &a[prev]);
		++cur;
	}
	 swap(&a[begin], &a[prev]);
	quickSort(a, low, begin);
	quickSort(a, begin + 1, high);

}


//归并排序
void merge(int a[], int m, int mid, int n)
{
	int begin1 = m; 
	int begin2 = mid + 1;
	int end1 = mid;
	int end2 = n;
	int* temp = (int*)malloc((n + 1) * sizeof(int));
	int k = 0;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] <= a[begin2])
		{
			temp[k++] = a[begin1++];
		}
		else
		{
			temp[k++] = a[begin2++];
		}

	}
	while (begin1 <= end1)
	{
		temp[k++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		temp[k++] = a[begin2++];
	}

	for (int i = 0; i < k; i++)
	{
		a[m+i] = temp[i]; //注意此处!!
	}
	free(temp);
}

void mergeSort(int a[], int m,int n)
{
	if (m < n)
	{
		int mid = m + (n - m) / 2;  //注意此处是n-m !!!
		mergeSort(a, m, mid);
		mergeSort(a, mid + 1, n);
		merge(a, m, mid, n);
	}
	
}


int main()
{
	int a[] = { 1,3,5,7,9,2,4,6,8,10 };
	print(a, 10);
	//insert(a, 10);
	//shellSort(a, 10);
	//selectSort(a, 10);
	//bubbleSort(a, 10);
	//quickSort(a, 0, 10);
	//quickSort2(a, 0, 10);
	quickSort3(a, 0, 10);
	//mergeSort(a, 0, 9);
	print(a, 10);
	return 0;
}

;