저자 : GuangshengZhou
QQ : 825672792
문제 설명
최근 빠른 종류의 프로젝트에 사용하지만, 중복 된 데이터를 배열에 표시되면 정렬 프로세스 요소 그룹의 번호를 반복하기 때문에, 빠른 정렬 좋은 기회에 스왑과 동일, 나쁜 통해 죽음에 들어갈. 지금 알고리즘 사소한 수정은 추가 된 판정 조건은 절삭, 즉 제 같아.
인코딩 기능
직접 코드를 다음과 같습니다.
//交换函数
void swap(int & nFirst, int & nSecond){
nFirst = nFirst + nSecond;
nSecond = nFirst - nSecond ;
nFirst = nFirst - nSecond ;
}
// 快速排序
void quicksort(int nArray[], int nLeft, int nRight){
if(nLeft >= nRight){
return;
}
//临时数据
int left = nLeft, lcur = nLeft, lcount = 0;
int right = nRight, rcur = nRight, rcount = 0;
int nValue = nArray[left];
while(left < right){
while(left < right && nValue <= nArray[right])
{
if(nValue == nArray[right]){
swap(nArray[rcur], nArray[right]);
rcur--;
rcount++;
}
right--;
}
if(left < right){
nArray[left++] = nArray[right];
}
while(left < right && nArray[right] >= nValue){
if(nArray[left] == nValue){
swap(nArray[lcur], nArray[left]);
lcur++;
lcount++;
}
left++;
}
if(left < right){
nArray[right--] = nArray[left];
}
}
nArray[left] = nValue;
assert(left == right);
//交换右边相等区域
int index = right + 1;
while(rcount > 0 && index <= rcur && (nRight - (index-(right + 1))>rcur)){
swap(nArray[index,], nArray[nRight - (index - (right + 1)]);
index++;
}
//交换左边相等区域
index = left - 1;
while(lcount > 0 && indx >= lcur && (nLeft + (left - 1 - index) < lcur)){
swap(nArray[index,], nArray[nRight - (nLeft + (left - 1 - index)]);
index--;
}
}
결과는 보여
int main(){
int array[10] = {30, 20, 30, 50, 60, 30, 30, 40, 40, 35};
quicksort(array, 0, 10);
for(int i = 0; i < 10; i++){
printf("%d \n", array[i]);
}
}
말
코드 순수 손으로 회사 네트워크 재생하기 때문에, 프로그램은 학습의 교환을 위해, 최선의 해결책이 될 수 없습니다.