⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 最近对排序算法的复习
💻
字号:
// 快速排序.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "windows.h"

void Swap(int &x,int &y) {
	int temp = x;
	x = y;
	y = temp;
}
/*void kspx(int array[],int index,int last) {			//实现方法一
	if(index>last)
		return;
	int i = index;
	int j = last+1;
	int key = array[i];
	while(true) {
		do {
			i+=1;
		}while(key>array[i]);
		do {
			j-=1;
		}while(key<array[j]);
		if(i>=j)
			break;
		Swap(array[i],array[j]);
	}
	array[index] = array[j];
	array[j] = key;
	kspx(array,index,j-1);
	kspx(array,j+1,last);
}*/

void kspx(int array[],int index,int last) {				//实现方法二
	if(index>last)	
		return;
	int i = index;
	int j = last+1;
	int key = array[i];
	while(true) {
		do {
			j-=1;
		}while(key<array[j]);
		if(i>=j)
			break;
		Swap(array[i],array[j]);

		do {
			i+=1;
		}while(key>array[i]);
		if(i>=j)
			break;
		Swap(array[j],array[i]);
	}
	kspx(array,index,j-1);
	kspx(array,j+1,last);
}

int main(int argc, char* argv[])
{
	int array[] = {23,54,12,34,97,60,58,7,73,82};
	for(int i=0;i<10;i++) {
		cout<<array[i]<<" ";
	}
	cout<<endl;

	int Stime,Etime,UseTime;   //测试程序运行时间
	Stime=timeGetTime();
	kspx(array,0,9);	//进行快速排序
	Sleep(1000);		//睡眠增加延时
	Etime=timeGetTime();
	UseTime=Etime-Stime; 
	cout<<"the program cost the time:"<<UseTime<<endl; 
	for(i=0;i<10;i++) {
		cout<<array[i]<<" ";
	}
	cout<<endl;
	return 0;
}

⌨️ 快捷键说明

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