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

📄 quicksort.cpp

📁 利用快速排序算法对dat文件中保存的数据进行排序
💻 CPP
字号:
#include<stdio.h>
#include<fstream.h>
#include<iostream.h>
	
int a[100];

void swap(int a[], int x, int y)
{
	int temp;

	temp = a[x];
	a[x] = a[y];
	a[y] = temp;
}

int partition(int m, int n)
{
	int i = m,
	    j = n + 1;
		    
	int x = a[m];
		
	while(true){
			
		while(a[++i] < x);
		while(a[--j] > x);
			
		if(i >= j)
			break;
				
		swap(a,i,j);
	
	}
		
	a[m] = a[j];
	a[j] = x;
		
	return j;
		    
}
	
void qSort(int m, int n)
{
	if(m < n){
			
		int l = partition(m, n);
		qSort(m, l - 1); //对左半段排序
		qSort(l + 1, n); //对右半段排序
	}
}
	
void main()
{
	int i=0;

	/*stream = fopen("100.dat","r");
	if(stream == NULL)
		printf("文件打开失败!");

	fread(a,sizeof(int),100,stream);

	fclose(stream);*/

	ifstream  input("in.dat");
	ofstream  output("out.dat");

	if(!input||!output)
	{
		input.close();
		output.close();
	}

	while(!input.eof())
		input>>a[i++];

	qSort(0,99);
		
	for(int n=0; n < 100; n++)
	{
		printf("%d ",a[n]);

		output<<a[n]<<endl;

		if((n+1)%10==0)
			printf("\n");
	}

}

⌨️ 快捷键说明

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