
این سورس یک عدد گرفته و به تعداد همان عدد از ورودی دریافت می کند و آنها را با روش . Quick sort مرتب می کند .
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//--------------------------------------------------------------------------
void read_list(int a[],int n){
int i;
for(i=0;i<n;i++){
printf("\n\n\t ENTER THE ELEMENT [%d] :: ",i);
scanf("%d",&a);
}
}
//--------------------------------------------------------------------------
void print_list(int a[],int n){
int i;
for(i=0;i<n;i++)
printf("\n\n\t %d",a);
}
//--------------------------------------------------------------------------
void quick_sort(int a[],int first,int last){
int low,high,temp,pivot;
low=first;
high=last;
pivot=a[(first+last)/2];
do{
while(a[low]<pivot)
low++;
while(a[high]>pivot)
high--;
if(low<=high){
temp=a[low];
a[low]=a[high];
a[high]=temp;
low=low+1;
high=high-1;
}
}while(low<=high);
if(first<high)
quick_sort(a,first,high);
if(low<last)
quick_sort(a,low,last);
}
//--------------------------------------------------------------------------
void main(){
int a[20],n;
clrscr();
printf("\n\n\t ENTER THE ARRAY LENGTH :: ");
scanf("%d",&n);
read_list(a,n);
printf("\n\n\t THE ARRAY ELEMENTS ARE AS FOLLOWS :: ");
print_list(a,n);
quick_sort(a,0,n-1);
printf("\n\n\t THE SOTED LIST IS :: ");
print_list(a,n);
getch();
}
//--------------------------------------------------------------------------