quicksort.cpp

来自「里面包含各种数据结构方面的知识,如链表,树,图等 含有vc代码」· C++ 代码 · 共 48 行

CPP
48
字号
#include <iostream.h>
#include "datatype.h"

void QuickSort(datatype a[], int low, int high)
//用递归方法对对象a[low]--a[high]进行快速排序
{
	int i, j;
	datatype temp;

	i = low;
	j = high;
	temp = a[low];

	while(i < j)
	{
		//在数组的右端扫描
		while(i < j && temp.key <= a[j].key) j--;
		if(i < j)
		{
			a[i] = a[j];
			i++;
		}

		//在数组的左端扫描
		while(i < j && a[i].key < temp.key) i++;
		if(i < j)
		{
			a[j] = a[i];
			j--;
		}
	}
	a[i] = temp;

	//对子对象数组进行递归快速排序
	if(low < i) QuickSort(a, low, i-1);
	if(i < high) QuickSort(a, j+1, high);
}

void main(void)
{
	datatype test[]={60,55,48,37,10,90,84,36};
	int n = 8;
	QuickSort(test, 0, 7);
	for(int i = 0; i < n; i++)
		cout << test[i].key << "  ";
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?