# 帮小弟我看看一个排序有关问题(2)

www.MyException.Cn  网友分享于：2013-03-08  浏览：18次

}
}

int   main(void)
{
ifstream   infile;
int   nTempValue   =   0;
int   nStringLenght   =   0;
int   nIntegerNumber[INTEGER_NUMBER];
char   cString[STRING_LENGHT];
char   cTempChar[SINGLE_NUMBER_LENGHT];
string   strMyString   =   " ";
string   strTempString   =   " ";

infile.open( "myfile01.txt ",ios::in);
if(!infile)
{
return   0;
}
while(!infile.eof())
{
infile.getline*(cString,127);
strMyString   =   cString;
nStringLenght   =   strMyString.length();
for(int   i   =   0;i <nStringLenght;i++)
{
//这部分有问题似乎只取出了一个整数
int   nCommaPos   =   strMyString.find   ( ", ");
memset(cTempChar, '\0 ',10);
strTempString   =   strncpy(cTempChar,strMyString.c_str(),nCommaPos);
nTempValue   =   atoi(cTempChar);
nIntegerNumber[i]   =   nTempValue;
}
}
infile.close();
QuickSort(nIntegerNumber,0,INTEGER_NUMBER-1);
return   0;
}

------解决方案--------------------

#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;

const int TOTAL_NUMBER = 50000;

//***************************************************
//-------------------快速排序算法---------------------
//***************************************************
//将数组分离成左右两部分
template <typename T>
int Partition(T *pA, int p, int r)
{
int i = p;
int j = r;
T x =pA[p];
while(1)
{
while(pA[j] > x) //从右边取出小于等于x的值的索引
j = j - 1;
while(pA[i] <= x)
i = i + 1; //从左边取出大于x的值的索引

if(i < j)
{ //交换使元素分组
std::swap <T> (pA[i], pA[j]);
}
else
{
if(p != j); //考虑最坏情况
std::swap <T> (pA[p], pA[j]);
return j;
}
}

}