8种排序

#include<stdio.h>
#define SIZE 100

void printArray(int array[], int size){
	int i;
	for (i = 0; i < size; i++)
		printf("%d ", array[i]);
	printf("\n");
}

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

void bubbleSort(int array[], int size){
	int round;
	//数组array中有size个数,只需比较(size - 1)轮
	for (round = 1; round < size; round++){
		int last = size - 1;
		int needContinue = 0;
		int compare;
		//array[compare]跟array[compare + 1]比较,compare最大为倒数第二个下标
		for (compare = 0; compare < last; compare++)
			if (array[compare] > array[compare + 1]){
				needContinue = 1;
				swap(&array[compare], &array[compare + 1]);
			}
		if (!needContinue)
			//没有交换过数据,没必要比较下去
			break;
	}
}

int partition(int array[], int first, int last){
	int pivotValue = array[first];
	while (first < last){
		//注意首先从右往左扫描比较,在array[end] == pivotValue的情况下end也要左移
		while (first < last && array[last] >= pivotValue)
			last--;
		//不用swap,直接赋值可优化
		array[first] = array[last];
		while (first < last && array[first] <= pivotValue)
			first++;
		array[last] = array[first];
	}
	//因为前面不用swap所以别忘记这个赋值语句
	array[first] = pivotValue;
	return first;
}
void quickSort(int array[], int first, int last){
	//用迭代优化尾递归
	while (first < last){
		int pivot = partition(array, first, last);
		quickSort(array, first, pivot - 1);
		first = pivot + 1;
	}
}

void insertSort(int array[], int size){
	int round;
	for (round = 1; round < size; round++){
		int valueInserting = array[round];
		int compare;
		for (compare = round - 1; compare >= 0 && array[compare] > valueInserting; compare--)
			array[compare + 1] = array[compare];
		array[compare + 1] = valueInserting;
	}
}

void shellSort(int array[], int size){
	int increment = size;
	do {
		//希尔排序其实就是增量逐渐变小的插入排序
		increment = increment / 3 + 1;
		int round;
		for (round = 1; round < size; round++){
			int valueInserting = array[round];
			int compare;
			for (compare = round - increment; compare >= 0 && array[compare] > valueInserting; compare -= increment)
				array[compare + increment] = array[compare];
			array[compare + increment] = valueInserting;
		}

	} while (increment > 1);
}

void selectSort(int array[], int size){
	int round;
	for (round = 1; round < size; round++){
		int firstIndex = round - 1;
		int minValueIndex = firstIndex;
		int compare;
		for (compare = minValueIndex + 1; compare < size; compare++)
			if (array[compare] < array[minValueIndex])
				minValueIndex = compare;
		if (minValueIndex != firstIndex)
			swap(&array[firstIndex], &array[minValueIndex]); 

	}
}
void maxHeapify(int array[], int heapSize, int parent){
	int largest = parent;
	int left = parent << 1;
	if (left <= heapSize && array[left] > array[largest])
		largest = left;
	int right = (parent << 1) + 1;
	if (right <= heapSize && array[right] > array[largest])
		largest = right;
	if (largest != parent){
		swap(&array[largest], &array[parent]);
		maxHeapify(array, heapSize, largest);
	}
}
void buildMaxHeap(int array[], int heapSize){
	int parent;
	for (parent = heapSize >> 1; parent >= 1; parent--)
		maxHeapify(array, heapSize, parent);
}
void heapSort(int array[], int size){
	//为了方便利用堆的性质,进行堆排序的数组array从下标1开始存储数据
	int heapSize = size - 1;
	buildMaxHeap(array, heapSize);
	int index;
	for (index = heapSize; index > 1; index--){
		swap(&array[1], &array[index]);
		heapSize--;
		maxHeapify(array, heapSize, 1);
	}
}


void merge(int array[], int first, int mid, int last){
	int left = first;
	int right = mid + 1;
	int arraySorted[SIZE];
	int indexSorted = 0;
	while (left <= mid && right <= last)
		if (array[left] < array[right])
			arraySorted[indexSorted++] = array[left++];
		else 
			arraySorted[indexSorted++] = array[right++];
	while (left <= mid)
		arraySorted[indexSorted++] = array[left++];
	while (right <= last)
		arraySorted[indexSorted++] = array[right++];
	for (indexSorted--; indexSorted >= 0; indexSorted--)
		array[indexSorted] = arraySorted[indexSorted];
}
void mergeSort(int array[], int first, int last){
	if (first >= last)
		return ;
	int mid = first + ( (last - first) >> 1 );
	mergeSort(array, first, mid);
	mergeSort(array, mid + 1, last);
	merge(array, first, mid, last);
}

void countSort(int array[], int size, int range, int arraySorted[]){
	int count[SIZE];
	int i;
	for (i = 0; i <= range; i++)
		count[i] = 0;

	int last = size - 1;
	for (i = 0; i <= last; i++)
		count[ array[i] ]++;

	for (i = 1; i <= range; i++)
		count[i] += count[i - 1];

	for (i = last; i >= 0; i--){
		int num = array[i];
		arraySorted[ count[num] - 1 ] = num;
		count[num]--;
	}
}

int main(){

	return 0;
}


 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值