빠른 정렬 C 언어 구현(귀속)
1850 단어 빠른 정렬
#include<stdio.h>
int Partition(int a[],int low,int high)
{
int pivotkey = a[low];
while(low<high)
{
if(low<high && a[high]>=pivotkey) --high;
a[low]=a[high];
if(low<high && a[low]<=pivotkey) ++low;
a[high]=a[low];
}
a[low]=pivotkey;
return low;
}
void Quick_Sort(int a[],int low,int high)
{
if(low<high)
{
int position = Partition(a,low,high);
Quick_Sort(a,low,poisition-1);
Quick_Sort(a,poisition+1,high);
}
}
void main()
{
int a[4]={45,56,23,5};
Quick_Sort(a,0,3);
}
다른 쓰기 방법을 첨부합니다.
void fun(int min,int max,int a[])
{
int key = a[min];
int i = min;
int j = max;
int temp;
if(min>=max)
return;
while(i<j)
{
while((i<j) && (key <= a[j]))
{j--;}
if(key > a[j])
{ a[i] = a[j]; a[j] = key; i++; }
while((i<j) && (key >= a[i]))
{i++;}
if(key < a[i])
{ a[j] = a[i];a[i] = key; j--; }
}
fun(min,i-1,a);
fun(i+1,max,a);
}
int main()
{
int i;
int a[10] = {49,38,65,97,76,13,27,9,2,1};
for(i=0;i<10;i++)
printf(" %d ",a[i]);
printf("
");
fun(0,9,a);
for(i=0;i<10;i++)
printf(" %d ",a[i]);
printf("
");
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA 버전 정렬 알고리즘의 빠른 정렬 예본고는 JAVA의 빠른 정렬 실현 방법을 실례로 다루고 있다.다음과 같이 여러분에게 참고할 수 있도록 공유합니다. 본고에서 기술한 것이 여러분의 자바 프로그램 설계에 도움이 되기를 바랍니다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.