你看看这个怎么样,排序方法是快速排序的最基础的一部分
你可以研究一下快速排序
#include
#include
int NUM[10][9] = {
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3
};
void jsValue(){
int temp;
int i,j,k;
for(k=0;k<10;k++){/*分别对第一行至第十行进行排序*/
temp=NUM[k][0];
i=0;
j=8;
while(i
i++;
}
while(NUM[k][i]
j--;
}
}
NUM[k][i]=temp;
}
}
writeDat()
{
FILE *fout;
int i,j;
fout=fopen("c:\\out.dat","w");/*我把文件存在了C盘*/
for(i=0;i<10;i++){
for(j=0;j<9;j++) fprintf(fout,"%3d",NUM[i][j]);
fprintf(fout,"\n");
}
}
void main()
{
jsValue();
writeDat();
}
#include
void jsValue(int(*a)[9])
{
int i,j,k,t;
int key;
for(i=0;i<10;i++)
{
key=a[i][0];
for(j=1;j<9;j++)
{
if(a[i][j]
t=a[i][j];
for(k=j;k>0;k--) /* a[i][0]--a[i][j]之间的元素全部向后移动一次 */
{
a[i][k]=a[i][k-1];
}
a[i][0]=t;
}
}
}
}
void writeDat(int(*a)[9])
{
FILE *fp;
int i,j;
fp=fopen("d:\\out.dat","w");
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
fprintf(fp,"%d ",a[i][j]);
}
fprintf(fp,"\n");
}
fclose(fp);
}
int main()
{
int NUM[10][9] =
{
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3
};
int i,j,k;
jsValue(NUM);
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
printf("%d ",NUM[i][j]);
}
printf("\n");
}
writeDat(NUM);
printf("以上已写入文件:d:\\out.dat!!!\n");
return 0;
}
结果:
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
3 4 5 2 1 6 8 9 7
static int NUM[10][9] = {
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3,
6,8,9,1,2,5,4,7,3
};
void Move(int row, int count)
{
for (int i = count - 1; i >= 0; i--)
{
NUM[row][i + 1] = NUM[row][i];
}
}
void jsValue()
{
for (int i = 0; i < 10; i++)
{
int first = NUM[i][0];
for (int k = 0; k < 9; k++)
{
if (first > NUM[i][k])
{
int temp = NUM[i][k];
Move(i, k);
NUM[i][0] = temp;
}
}
}
}
void writeDat()
{
FILE* pFile = fopen("out.dat", "w");
if (!pFile)
{
fclose(pFile);
}
int a = sizeof(NUM);
fwrite(NUM, sizeof(NUM),1, pFile);
fclose(pFile);
}
void ReadData()
{
FILE* pFile = fopen("out.dat", "r");
if (!pFile)
{
fclose(pFile);
}
int a = sizeof(NUM);
fread(NUM, sizeof(NUM),1, pFile);
fclose(pFile);
}
int _tmain(int argc, _TCHAR* argv[])
{
//ReadData();
jsValue();
writeDat();
for (int i = 0; i < 10; i++)
{
for (int k = 0; k < 9; k++)
{
printf("%d ", NUM[i][k]);
}
printf("\n");
}
getchar();
return 0;
}