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

📄 sort.h

📁 含有7种排序算法
💻 H
📖 第 1 页 / 共 2 页
字号:
	}
	void SelectionSort(int array[], int size)
    {
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size; j++)
			{
				if (array[i] < array[j])
					swap(array[i], array[j]);
			}
		}
	}	
	
	void ComboSort(int This[], unsigned int the_len)
	{
		Qsort(This, cmpfun, 0, the_len - 1);
	}
	
	void Display(int dat[], int num)
	{
		for (int x = 0; x < num; x++)
			cout << dat[x] << ' ';
		cout << endl;
	}
	
	void WtoF(int dat[], int num, char filename[])
	{
		ofstream fout(filename);
		for (int x = 0; x < num; x++)
			fout << dat[x] << '\n';
		fout.close();		
	}
	
	void RfromF(int dat[], int num, char filename[])
	{
		ifstream fin(filename);
		fin.seekg(0);
		char buffer[50];
		for (int x = 0; x < num; x++)
		{
			if (fin.eof())
				break;
			fin.getline(buffer, 50, '\n');
			dat[x] = atoi(buffer);
		}			
		fin.close();	
	}
	
	void DetermineFastest(int val)
	{
		if (val > MAXIMUM)
			val = MAXIMUM;

		int dat[MAXIMUM] = {0};
		int dat2[MAXIMUM] = {0};
		int sorts[7] = {0};
		
		int begin = 0, end = 0;
		
		for (int x = 0; x < val; x++)
			dat[x] = rand()%val;

		for (x = 0; x < val; x++)
			dat2[x] = dat[x];
		
		
		begin = t.currentTime();
		BubbleSort(dat, val);
		end = t.currentTime();
		bubbletime = t.elapsedTime(begin, end);
		
		for (x = 0; x < val; x++)
			dat[x] = dat2[x];
		
		begin = t.currentTime();
		ComboSort(dat, val);
		end = t.currentTime();
		combotime = t.elapsedTime(begin, end);
		
		for (x = 0; x < val; x++)
			dat[x] = dat2[x];
		
		begin = t.currentTime();
		InsertionSort(dat, val);
		end = t.currentTime();
		insertiontime = t.elapsedTime(begin, end);

		for (x = 0; x < val; x++)
			dat[x] = dat2[x];
		
		begin = t.currentTime();
		MinSort(dat, val);
		end = t.currentTime();
		mintime = t.elapsedTime(begin, end);

		for (x = 0; x < val; x++)
			dat[x] = dat2[x];
		
		begin = t.currentTime();
		QuickSort(dat, val);
		end = t.currentTime();
		quicktime = t.elapsedTime(begin, end);

		for (x = 0; x < val; x++)
			dat[x] = dat2[x];
		
		begin = t.currentTime();
		SelectionSort(dat, val);
		end = t.currentTime();
		selectiontime = t.elapsedTime(begin, end);

		for (x = 0; x < val; x++)
			dat[x] = dat2[x];
		
		begin = t.currentTime();
		ShellSort(dat, val);
		end = t.currentTime();
		shelltime = t.elapsedTime(begin, end);

		sorts[0] = bubbletime;
		sorts[1] = combotime;
		sorts[2] = insertiontime;
		sorts[3] = mintime;
		sorts[4] = quicktime;
		sorts[5] = selectiontime;
		sorts[6] = shelltime;
		
		QuickSort(sorts, 7);
		
		if (sorts[0] == bubbletime)
			fastest = BUBBLE; 
		else if (sorts[0] == combotime)
			fastest = COMBO;
		else if (sorts[0] == insertiontime)
			fastest = INSERTION; 
		else if (sorts[0] == mintime)
			fastest = MIN; 
		else if (sorts[0] == quicktime)
			fastest = QUICK; 
		else if (sorts[0] == selectiontime)
			fastest = SELECTION;
		else if (sorts[0] == shelltime)
			fastest = SHELL; 
		else 
			fastest = QUICK;
	}
	
	void SortFastest(int data[], int num)
	{
		switch(fastest)
		{
		case COMBO: ComboSort(data, num); break;
		case QUICK: QuickSort(data, num); break;
		case MIN: MinSort(data, num); break;
		case BUBBLE: BubbleSort(data, num); break;
		case INSERTION: InsertionSort(data, num); break;
		case SHELL: ShellSort(data, num); break;
		case SELECTION: SelectionSort(data, num); break;
		default: QuickSort(data, num); break;
		};
	}

	void About()
	{
		cout << "Sort Class by Rob Brasier. \nE-mail: thornclaw@tootr.zzn.com\nAIM: rob the hobbit\n";
	}
	
	char* ReturnFastest()
	{
		switch(fastest)
		{
		case COMBO: return "Combo"; break;
		case QUICK: return "Quick"; break;
		case MIN: return "Min"; break;
		case BUBBLE: return "Bubble"; break;
		case INSERTION: return "Insertion"; break;
		case SHELL: return "Shell"; break;
		case SELECTION: return "Selection"; break;
		default: return "ERROR"; break;
		};
		return "ERROR";
	}

	char* ReturnOrder(int flag = 0)
	{
		int sorts[10] = {0};
		strcpy(returnval, "");

		sorts[0] = bubbletime;
		sorts[1] = combotime;
		sorts[2] = insertiontime;
		sorts[3] = mintime;
		sorts[4] = quicktime;
		sorts[5] = selectiontime;
		sorts[6] = shelltime;
		
		QuickSort(sorts, 7);
		for (int x = 0; x < 7; x++)
		{
			if (x >= 7)
				break;
			if (sorts[x] == bubbletime && !bubbleflag)
			{
				sprintf(returnval, "%s%s", returnval, " Bubble");
				bubbleflag = true;
			}
			else if (sorts[x] == combotime && !comboflag)
			{
				sprintf(returnval, "%s%s", returnval, " Combo");
				comboflag = true;
			}
			else if (sorts[x] == insertiontime && !insertionflag)
			{
				sprintf(returnval, "%s%s", returnval, " Insertion");
				insertionflag = true;
			}
			else if (sorts[x] == mintime && !minflag)
			{
				sprintf(returnval, "%s%s", returnval, " Min");
				minflag = true;
			}
			else if (sorts[x] == quicktime && !quickflag)
			{
				sprintf(returnval, "%s%s", returnval, " Quick");
				quickflag = true;
			}
			else if (sorts[x] == selectiontime && !selectionflag)
			{
				sprintf(returnval, "%s%s", returnval, " Selection");
				selectionflag = true;
			}
			else if (sorts[x] == shelltime && !shellflag)
			{
				sprintf(returnval, "%s%s", returnval, " Shell");
				shellflag = true;
			}
			else 
				sprintf(returnval, "%s%s", returnval, " ERROR");
		}
		if (flag == 0)
			return returnval;
		else
		{
			switch(flag)
			{
			case 1: 
				if (sorts[0] == bubbletime)
					return "Bubble";
				else if (sorts[0] == combotime)
					return "Combo";
				else if (sorts[0] == insertiontime)
					return "Insertion";
				else if (sorts[0] == mintime)
					return "Min";
				else if (sorts[0] == quicktime)
					return "Quick";
				else if (sorts[0] == selectiontime)
					return "Selection";
				else if (sorts[0] == shelltime)
					return "Shell";
				else 
					return "ERROR";
				break;
			case 2:
				if (sorts[1] == bubbletime)
					return "Bubble";
				else if (sorts[1] == combotime)
					return "Combo";
				else if (sorts[1] == insertiontime)
					return "Insertion";
				else if (sorts[1] == mintime)
					return "Min";
				else if (sorts[1] == quicktime)
					return "Quick";
				else if (sorts[1] == selectiontime)
					return "Selection";
				else if (sorts[1] == shelltime)
					return "Shell";
				else 
					return "ERROR";
				break;
			case 3:
				if (sorts[2] == bubbletime)
					return "Bubble";
				else if (sorts[2] == combotime)
					return "Combo";
				else if (sorts[2] == insertiontime)
					return "Insertion";
				else if (sorts[2] == mintime)
					return "Min";
				else if (sorts[2] == quicktime)
					return "Quick";
				else if (sorts[2] == selectiontime)
					return "Selection";
				else if (sorts[2] == shelltime)
					return "Shell";
				else 
					return "ERROR";
				break;
			case 4:
				if (sorts[3] == bubbletime)
					return "Bubble";
				else if (sorts[3] == combotime)
					return "Combo";
				else if (sorts[3] == insertiontime)
					return "Insertion";
				else if (sorts[3] == mintime)
					return "Min";
				else if (sorts[3] == quicktime)
					return "Quick";
				else if (sorts[3] == selectiontime)
					return "Selection";
				else if (sorts[3] == shelltime)
					return "Shell";
				else 
					return "ERROR";
				break;
			case 5: 
				if (sorts[4] == bubbletime)
					return "Bubble";
				else if (sorts[4] == combotime)
					return "Combo";
				else if (sorts[4] == insertiontime)
					return "Insertion";
				else if (sorts[4] == mintime)
					return "Min";
				else if (sorts[4] == quicktime)
					return "Quick";
				else if (sorts[4] == selectiontime)
					return "Selection";
				else if (sorts[4] == shelltime)
					return "Shell";
				else 
					return "ERROR";
				break;
			case 6:
				if (sorts[5] == bubbletime)
					return "Bubble";
				else if (sorts[5] == combotime)
					return "Combo";
				else if (sorts[5] == insertiontime)
					return "Insertion";
				else if (sorts[5] == mintime)
					return "Min";
				else if (sorts[5] == quicktime)
					return "Quick";
				else if (sorts[5] == selectiontime)
					return "Selection";
				else if (sorts[5] == shelltime)
					return "Shell";
				else 
					return "ERROR";
				break;
			case 7:
				if (sorts[6] == bubbletime)
					return "Bubble";
				else if (sorts[6] == combotime)
					return "Combo";
				else if (sorts[6] == insertiontime)
					return "Insertion";
				else if (sorts[6] == mintime)
					return "Min";
				else if (sorts[6] == quicktime)
					return "Quick";
				else if (sorts[6] == selectiontime)
					return "Selection";
				else if (sorts[6] == shelltime)
					return "Shell";
				else 
					return "ERROR";
				break;
			default: return returnval; break;
			}
		}
	}

	int ReturnTime(int flag = 0) //in milliseconds
	{
		int sorts[10] = {0};
		sorts[0] = combotime;
		sorts[1] = bubbletime;
		sorts[2] = insertiontime;
		sorts[3] = mintime;
		sorts[4] = quicktime;
		sorts[5] = selectiontime;
		sorts[6] = shelltime;
		QuickSort(sorts, 7);

		switch(sorts[flag])
		{
		case BUBBLE: return bubbletime;break;
		case COMBO: return combotime;break;
		case INSERTION: return insertiontime; break;
		case MIN:return mintime; break;
		case QUICK: return quicktime; break;
		case SELECTION: return selectiontime; break;
		case SHELL: return shelltime; break;
		default: return quicktime; break;
		}
	}
	
};

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

int cmpfun(int a, int b)
{
	if (a > b)
		return 1;
	else if (a < b)
		return -1;
	else
		return 0;
}

#endif

⌨️ 快捷键说明

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