数据结构第十四天——几种常见的排序算法

1、插入排序

void insertionSort(ListPtr paraPtr) {
	Node tempNode;
	int i, j;
	
	for (i = 2; i < paraPtr->length; i ++) {
		tempNode = paraPtr->elements[i];
		
		for (j = i - 1; paraPtr->elements[j].key > tempNode.key; j --) {
			paraPtr->elements[j + 1] = paraPtr->elements[j];
		}
		
		paraPtr->elements[j + 1] = tempNode;
	}
}

测试:

void insertionSortTest() {
	int tempUnsortedKeys[] = {-100, 5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'n', 'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 8);
	
	printf("\nBefore insertion sort:\n");
	printList(tempList);
	
	insertionSort(tempList);
	printf("\nAfter insertion sore:\n");
	printList(tempList);
}

结果:

Before insertion sort:
(Keys, values)
-100    5       3       6       10      7       1       9
n       i       t       e       s       c       f       w
After insertion sore:
(Keys, values)
-100    1       3       5       6       7       9       10
n       f       t       i       e       c       w       s

二、希尔排序

void shellSort(ListPtr paraPtr) {
	Node tempNode;
	int tempJumpArray[] = {5, 3, 1};
	int tempJump;
	int p, i, j, k;
	
	for (i = 0; i < 3; i ++) {
		tempJump = tempJumpArray[i];
		for (j = 0; j < tempJump; j ++) {
			for (k = j + tempJump; k < paraPtr->length; k += tempJump) {
				tempNode = paraPtr->elements[k];
				
				for (p = k - tempJump; p >= 0; p -= tempJump) {
					if (paraPtr->elements[p].key > tempNode.key) {
						paraPtr->elements[p + tempJump] = paraPtr->elements[p];
					} else {
						break;
					}
				}
				
				paraPtr->elements[p + tempJump] = tempNode;
			}
		}
	}
}

测试:

void shellSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore shell sort:\n");
	printList(tempList);
	
	shellSort(tempList);
	printf("\nAfter shell sort:\n");
	printList(tempList);
}

结果

Before shell sort:
(Keys, values)
5       3       6       10      7       1       9
i       t       e       s       c       f       w
After shell sort:
(Keys, values)
1       3       5       6       7       9       10
f       t       i       e       c       w       s

三、冒泡排序

void bubbleSort(ListPtr paraPtr) {
	int tempSwapped;
	Node tempNode;
	int i, j;
	
	for (i = paraPtr->length - 1; i > 0; i --) {
		tempSwapped = 0;
		for (j = 0; j < i; j ++) {
			if (paraPtr->elements[j].key > paraPtr->elements[j + 1].key) {
				tempNode = paraPtr->elements[j + 1];
				paraPtr->elements[j + 1] = paraPtr->elements[j];
				paraPtr->elements[j] = tempNode;
				
				tempSwapped = 1;
			}
		}
		
		if (tempSwapped == 0) {
			printf("Premature.\n");
			break;
		}
	}	
}

测试:

void bubbleSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore bubble sort:\n");
	printList(tempList);
	
	shellSort(tempList);
	printf("\nAfter bubble sort:\n");
	printList(tempList);
}

结果

Before bubble sort:
(Keys, values)
5       3       6       10      7       1       9
i       t       e       s       c       f       w
After bubble sort:
(Keys, values)
1       3       5       6       7       9       10
f       t       i       e       c       w       s

四、快速排序

void quickSortRecursive(ListPtr paraPtr, int paraStart, int paraEnd) {
	int tempPivot, tempLeft, tempRight;
	Node tempNodeForSwap;
	
	if (paraStart >= paraEnd) {
		return;
	}
	
	tempPivot = paraPtr->elements[paraEnd].key;
	
	tempLeft = paraStart;
	tempRight = paraEnd - 1;
	
	while (1) {
		while ((paraPtr->elements[tempLeft].key < tempPivot) && (tempLeft < tempRight)) {
			tempLeft ++;
		}
		
		while ((paraPtr->elements[tempRight].key >= tempPivot) && (tempLeft < tempRight)) {
			tempRight --;
		}
		
		if (tempLeft < tempRight) {
			tempNodeForSwap = paraPtr->elements[tempLeft];
			paraPtr->elements[tempLeft] = paraPtr->elements[tempRight];
			paraPtr->elements[tempRight] = tempNodeForSwap;
		} else {
			break;
		}
	}
	
	if (paraPtr->elements[tempLeft].key > tempPivot) {
		tempNodeForSwap = paraPtr->elements[paraEnd];
		paraPtr->elements[paraEnd] = paraPtr->elements[tempLeft];
		paraPtr->elements[tempLeft] = tempNodeForSwap;
	} else {
		tempLeft ++;
	}
	
	quickSortRecursive(paraPtr, paraStart, tempLeft - 1);
	quickSortRecursive(paraPtr, tempLeft + 1, paraEnd);
}

void quickSort(ListPtr paraPtr) {
	quickSortRecursive(paraPtr, 0, paraPtr->length - 1);
}

测试

void quickSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore quick sort:\n");
	printList(tempList);
	
	quickSort(tempList);
	printf("\nAfter quick sort:\n");
	printList(tempList);
}

结果

Before quick sort:
(Keys, values)
5       3       6       10      7       1       9
i       t       e       s       c       f       w
After quick sort:
(Keys, values)
1       3       5       6       7       9       10
f       t       i       e       c       w       s

五、选择排序

void selectionSort(ListPtr paraPtr) {
	Node tempNode;
	int tempIndexForSmallest, i, j;
	
	for (i = 0; i < paraPtr->length - 1; i ++) {
		tempNode = paraPtr->elements[i];
		tempIndexForSmallest = i;
		
		for (j = i + 1; j < paraPtr->length; j ++) {
			if (paraPtr->elements[j].key < tempNode.key) {
				tempNode = paraPtr->elements[j];
				tempIndexForSmallest = j;
			}
		}
		
		paraPtr->elements[tempIndexForSmallest] = paraPtr->elements[i];
		paraPtr->elements[i] = tempNode;
	}
}

测试

void selectionSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore selection sort:\n");
	printList(tempList);
	
	selectionSort(tempList);
	printf("\nAfter selection sort:\n");
	printList(tempList);
}

结果

Before selection sort:
(Keys, values)
5       3       6       10      7       1       9
i       t       e       s       c       f       w
After selection sort:
(Keys, values)
1       3       5       6       7       9       10
f       t       i       e       c       w       s

六、堆排序

void adjustHeap(ListPtr paraPtr, int paraStart, int paraLength) {
	Node tempNode = paraPtr->elements[paraStart];
	int tempParent = paraStart;
	int tempKey = paraPtr->elements[paraStart].key;
	int tempChild;
	
	for (tempChild = paraStart * 2 + 1; tempChild < paraLength; tempChild = tempChild * 2 + 1) {
		if (tempChild + 1 < paraLength) {
			if (paraPtr->elements[tempChild].key < paraPtr->elements[tempChild + 1].key) {
				tempChild++;
			}
		}
		
		if (tempKey < paraPtr->elements[tempChild].key) {
				paraPtr->elements[tempParent] = paraPtr->elements[tempChild];
				tempParent = tempChild;
			} else {
				break;
		}
	}
	
	paraPtr->elements[tempParent] = tempNode;
}

void heapSort(ListPtr paraPtr) {
	Node tempNode;
	int i;
	
	for (i = paraPtr->length / 2 - 1; i >= 0; i --) {
		adjustHeap(paraPtr, i, paraPtr->length);
	}
	
	for (i = paraPtr->length - 1; i > 0; i --) {
		tempNode = paraPtr->elements[0];
		paraPtr->elements[0] = paraPtr->elements[i];
		paraPtr->elements[i] = tempNode;
		
		adjustHeap(paraPtr, 0, i);
	}
}

测试

void heapSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore heap sort:\n");
	printList(tempList);
	
	heapSort(tempList);
	printf("\nAfter heap sort:\n");
	printList(tempList);
}

结果

Before heap sort:
(Keys, values)
5       3       6       10      7       1       9
i       t       e       s       c       f       w
After heap sort:
(Keys, values)
1       3       5       6       7       9       10
f       t       i       e       c       w       s

七、归并排序

void mergeSort(ListPtr paraPtr) {
	int tempRow;
	int tempGroups;
	int tempActualRow;
	int tempNextRow = 0;
	int tempGroupNumber;
	int tempFirstStart, tempSecondStart, tempSecondEnd;
	int tempFirstIndex, tempSecondIndex;
	int tempNumCopied;
	int i;
	int tempSize;
	
	Node ** tempMatrix = (Node**)malloc(2 * sizeof(Node*));
	tempMatrix[0] = (Node*)malloc(paraPtr->length * sizeof(Node));
	tempMatrix[1] = (Node*)malloc(paraPtr->length * sizeof(Node));
	
	for (i = 0; i < paraPtr->length; i ++) {
		tempMatrix[0][i] = paraPtr->elements[i];
	}
	
	tempRow = -1;
	for (tempSize = 1; tempSize <= paraPtr->length; tempSize *= 2) {
		tempRow ++;
		tempActualRow = tempRow % 2;
		tempNextRow = (tempRow + 1) % 2;
		
		tempGroups = paraPtr->length / (tempSize * 2);
		
		if (paraPtr->length % (tempSize * 2) != 0) {
			tempGroups ++;
		}
		
		for (tempGroupNumber = 0; tempGroupNumber < tempGroups; tempGroupNumber ++) {
			tempFirstStart = tempGroupNumber * tempSize * 2;
			tempSecondStart = tempGroupNumber * tempSize * 2 + tempSize;
			
			if (tempSecondStart > paraPtr->length - 1) {
				for (i = tempFirstStart; i < paraPtr->length; i ++) {
					tempMatrix[tempNextRow][i] = tempMatrix[tempActualRow][i];
				}
				continue;
			}
			
			tempSecondEnd = tempGroupNumber * tempSize * 2 + tempSize * 2 - 1;
			
			if (tempSecondEnd > paraPtr->length - 1) {
				tempSecondEnd = paraPtr->length - 1;
			}
			
			tempFirstIndex = tempFirstStart;
			tempSecondIndex = tempSecondStart;
			tempNumCopied = 0;
			
			while ((tempFirstIndex <= tempSecondStart - 1) && (tempSecondIndex <= tempSecondEnd)) {
				if (tempMatrix[tempActualRow][tempFirstIndex].key <= tempMatrix[tempActualRow][tempSecondIndex].key) {
					tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];
					tempFirstIndex ++;
				} else {
					tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];
					tempSecondIndex ++;
				}
				
				tempNumCopied ++;
			}
			
			while (tempFirstIndex <= tempSecondStart - 1) {
				tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];
				tempFirstIndex ++;
				tempNumCopied ++;
			}
			
			while (tempSecondIndex <= tempSecondEnd) {
				tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];
				tempSecondIndex ++;
				tempNumCopied ++;
			}
		}
		
		for (i = 0; i < paraPtr->length; i ++) {
			paraPtr->elements[i] = tempMatrix[tempNextRow][i];
		}
	}
}

测试

void mergeSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore merge sort:\n");
	printList(tempList);
	
	mergeSort(tempList);
	printf("\nAfter merge sort:\n");
	printList(tempList);
}

结果

Before merge sort:
(Keys, values)
5       3       6       10      7       1       9
i       t       e       s       c       f       w
After merge sort:
(Keys, values)
1       3       5       6       7       9       10
f       t       i       e       c       w       s

总代码

#include<stdio.h>
#include<stdlib.h>

typedef struct Node {
	int key;
	char value;
} Node, *NodePtr;

typedef struct SequentialList {
	int length;
	NodePtr elements;
} SequentialList, *ListPtr;

ListPtr initList(int *paraKeys, char *paraValues, int paraLength) {
	int i;
	ListPtr resultPtr = (ListPtr)malloc(sizeof(SequentialList));
	resultPtr->length = paraLength;
	resultPtr->elements = (NodePtr)malloc(sizeof(Node) * paraLength);
	
	for (i = 0; i < paraLength; i ++) {
		resultPtr->elements[i].key = paraKeys[i];
		resultPtr->elements[i].value = paraValues[i];
	}
	
	return resultPtr;
}

void printList(ListPtr paraPtr) {
	int i;
	
	printf("(Keys, values)\n");
	
	for (i = 0; i < paraPtr->length; i ++) {
		printf("%d\t", paraPtr->elements[i].key);
	}
	
	printf("\n");
	
	for (i = 0; i < paraPtr->length; i ++) {
		printf("%c\t", paraPtr->elements[i].value);
	}
}

void insertionSort(ListPtr paraPtr) {
	Node tempNode;
	int i, j;
	
	for (i = 2; i < paraPtr->length; i ++) {
		tempNode = paraPtr->elements[i];
		
		for (j = i - 1; paraPtr->elements[j].key > tempNode.key; j --) {
			paraPtr->elements[j + 1] = paraPtr->elements[j];
		}
		
		paraPtr->elements[j + 1] = tempNode;
	}
}

void insertionSortTest() {
	int tempUnsortedKeys[] = {-100, 5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'n', 'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 8);
	
	printf("\nBefore insertion sort:\n");
	printList(tempList);
	
	insertionSort(tempList);
	printf("\nAfter insertion sore:\n");
	printList(tempList);
}

void shellSort(ListPtr paraPtr) {
	Node tempNode;
	int tempJumpArray[] = {5, 3, 1};
	int tempJump;
	int p, i, j, k;
	
	for (i = 0; i < 3; i ++) {
		tempJump = tempJumpArray[i];
		for (j = 0; j < tempJump; j ++) {
			for (k = j + tempJump; k < paraPtr->length; k += tempJump) {
				tempNode = paraPtr->elements[k];
				
				for (p = k - tempJump; p >= 0; p -= tempJump) {
					if (paraPtr->elements[p].key > tempNode.key) {
						paraPtr->elements[p + tempJump] = paraPtr->elements[p];
					} else {
						break;
					}
				}
				
				paraPtr->elements[p + tempJump] = tempNode;
			}
		}
	}
}

void shellSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore shell sort:\n");
	printList(tempList);
	
	shellSort(tempList);
	printf("\nAfter shell sort:\n");
	printList(tempList);
}

void bubbleSort(ListPtr paraPtr) {
	int tempSwapped;
	Node tempNode;
	int i, j;
	
	for (i = paraPtr->length - 1; i > 0; i --) {
		tempSwapped = 0;
		for (j = 0; j < i; j ++) {
			if (paraPtr->elements[j].key > paraPtr->elements[j + 1].key) {
				tempNode = paraPtr->elements[j + 1];
				paraPtr->elements[j + 1] = paraPtr->elements[j];
				paraPtr->elements[j] = tempNode;
				
				tempSwapped = 1;
			}
		}
		
		if (tempSwapped == 0) {
			printf("Premature.\n");
			break;
		}
	}	
}

void bubbleSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore bubble sort:\n");
	printList(tempList);
	
	shellSort(tempList);
	printf("\nAfter bubble sort:\n");
	printList(tempList);
}

void quickSortRecursive(ListPtr paraPtr, int paraStart, int paraEnd) {
	int tempPivot, tempLeft, tempRight;
	Node tempNodeForSwap;
	
	if (paraStart >= paraEnd) {
		return;
	}
	
	tempPivot = paraPtr->elements[paraEnd].key;
	
	tempLeft = paraStart;
	tempRight = paraEnd - 1;
	
	while (1) {
		while ((paraPtr->elements[tempLeft].key < tempPivot) && (tempLeft < tempRight)) {
			tempLeft ++;
		}
		
		while ((paraPtr->elements[tempRight].key >= tempPivot) && (tempLeft < tempRight)) {
			tempRight --;
		}
		
		if (tempLeft < tempRight) {
			tempNodeForSwap = paraPtr->elements[tempLeft];
			paraPtr->elements[tempLeft] = paraPtr->elements[tempRight];
			paraPtr->elements[tempRight] = tempNodeForSwap;
		} else {
			break;
		}
	}
	
	if (paraPtr->elements[tempLeft].key > tempPivot) {
		tempNodeForSwap = paraPtr->elements[paraEnd];
		paraPtr->elements[paraEnd] = paraPtr->elements[tempLeft];
		paraPtr->elements[tempLeft] = tempNodeForSwap;
	} else {
		tempLeft ++;
	}
	
	quickSortRecursive(paraPtr, paraStart, tempLeft - 1);
	quickSortRecursive(paraPtr, tempLeft + 1, paraEnd);
}

void quickSort(ListPtr paraPtr) {
	quickSortRecursive(paraPtr, 0, paraPtr->length - 1);
}

void quickSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore quick sort:\n");
	printList(tempList);
	
	quickSort(tempList);
	printf("\nAfter quick sort:\n");
	printList(tempList);
}

void selectionSort(ListPtr paraPtr) {
	Node tempNode;
	int tempIndexForSmallest, i, j;
	
	for (i = 0; i < paraPtr->length - 1; i ++) {
		tempNode = paraPtr->elements[i];
		tempIndexForSmallest = i;
		
		for (j = i + 1; j < paraPtr->length; j ++) {
			if (paraPtr->elements[j].key < tempNode.key) {
				tempNode = paraPtr->elements[j];
				tempIndexForSmallest = j;
			}
		}
		
		paraPtr->elements[tempIndexForSmallest] = paraPtr->elements[i];
		paraPtr->elements[i] = tempNode;
	}
}

void selectionSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore selection sort:\n");
	printList(tempList);
	
	selectionSort(tempList);
	printf("\nAfter selection sort:\n");
	printList(tempList);
}

void adjustHeap(ListPtr paraPtr, int paraStart, int paraLength) {
	Node tempNode = paraPtr->elements[paraStart];
	int tempParent = paraStart;
	int tempKey = paraPtr->elements[paraStart].key;
	int tempChild;
	
	for (tempChild = paraStart * 2 + 1; tempChild < paraLength; tempChild = tempChild * 2 + 1) {
		if (tempChild + 1 < paraLength) {
			if (paraPtr->elements[tempChild].key < paraPtr->elements[tempChild + 1].key) {
				tempChild++;
			}
		}
		
		if (tempKey < paraPtr->elements[tempChild].key) {
				paraPtr->elements[tempParent] = paraPtr->elements[tempChild];
				tempParent = tempChild;
			} else {
				break;
		}
	}
	
	paraPtr->elements[tempParent] = tempNode;
}

void heapSort(ListPtr paraPtr) {
	Node tempNode;
	int i;
	
	for (i = paraPtr->length / 2 - 1; i >= 0; i --) {
		adjustHeap(paraPtr, i, paraPtr->length);
	}
	
	for (i = paraPtr->length - 1; i > 0; i --) {
		tempNode = paraPtr->elements[0];
		paraPtr->elements[0] = paraPtr->elements[i];
		paraPtr->elements[i] = tempNode;
		
		adjustHeap(paraPtr, 0, i);
	}
}

void heapSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore heap sort:\n");
	printList(tempList);
	
	heapSort(tempList);
	printf("\nAfter heap sort:\n");
	printList(tempList);
}

void mergeSort(ListPtr paraPtr) {
	int tempRow;
	int tempGroups;
	int tempActualRow;
	int tempNextRow = 0;
	int tempGroupNumber;
	int tempFirstStart, tempSecondStart, tempSecondEnd;
	int tempFirstIndex, tempSecondIndex;
	int tempNumCopied;
	int i;
	int tempSize;
	
	Node ** tempMatrix = (Node**)malloc(2 * sizeof(Node*));
	tempMatrix[0] = (Node*)malloc(paraPtr->length * sizeof(Node));
	tempMatrix[1] = (Node*)malloc(paraPtr->length * sizeof(Node));
	
	for (i = 0; i < paraPtr->length; i ++) {
		tempMatrix[0][i] = paraPtr->elements[i];
	}
	
	tempRow = -1;
	for (tempSize = 1; tempSize <= paraPtr->length; tempSize *= 2) {
		tempRow ++;
		tempActualRow = tempRow % 2;
		tempNextRow = (tempRow + 1) % 2;
		
		tempGroups = paraPtr->length / (tempSize * 2);
		
		if (paraPtr->length % (tempSize * 2) != 0) {
			tempGroups ++;
		}
		
		for (tempGroupNumber = 0; tempGroupNumber < tempGroups; tempGroupNumber ++) {
			tempFirstStart = tempGroupNumber * tempSize * 2;
			tempSecondStart = tempGroupNumber * tempSize * 2 + tempSize;
			
			if (tempSecondStart > paraPtr->length - 1) {
				for (i = tempFirstStart; i < paraPtr->length; i ++) {
					tempMatrix[tempNextRow][i] = tempMatrix[tempActualRow][i];
				}
				continue;
			}
			
			tempSecondEnd = tempGroupNumber * tempSize * 2 + tempSize * 2 - 1;
			
			if (tempSecondEnd > paraPtr->length - 1) {
				tempSecondEnd = paraPtr->length - 1;
			}
			
			tempFirstIndex = tempFirstStart;
			tempSecondIndex = tempSecondStart;
			tempNumCopied = 0;
			
			while ((tempFirstIndex <= tempSecondStart - 1) && (tempSecondIndex <= tempSecondEnd)) {
				if (tempMatrix[tempActualRow][tempFirstIndex].key <= tempMatrix[tempActualRow][tempSecondIndex].key) {
					tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];
					tempFirstIndex ++;
				} else {
					tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];
					tempSecondIndex ++;
				}
				
				tempNumCopied ++;
			}
			
			while (tempFirstIndex <= tempSecondStart - 1) {
				tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempFirstIndex];
				tempFirstIndex ++;
				tempNumCopied ++;
			}
			
			while (tempSecondIndex <= tempSecondEnd) {
				tempMatrix[tempNextRow][tempFirstStart + tempNumCopied] = tempMatrix[tempActualRow][tempSecondIndex];
				tempSecondIndex ++;
				tempNumCopied ++;
			}
		}
		
		for (i = 0; i < paraPtr->length; i ++) {
			paraPtr->elements[i] = tempMatrix[tempNextRow][i];
		}
	}
}

void mergeSortTest() {
	int tempUnsortedKeys[] = {5, 3, 6, 10, 7, 1, 9};
	char tempContents[] = {'i', 't', 'e', 's', 'c', 'f', 'w'};
	ListPtr tempList = initList(tempUnsortedKeys, tempContents, 7);
	
	printf("\nBefore merge sort:\n");
	printList(tempList);
	
	mergeSort(tempList);
	printf("\nAfter merge sort:\n");
	printList(tempList);
}

int main() {
	//insertionSortTest();
	//shellSortTest();
	//bubbleSortTest();
	//quickSortTest();
	//selectionSortTest();
	//heapSortTest();
	mergeSortTest();
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值