Quick sort_C - My articles - Publisher - Information Technology
Sunday, 2016-12-04, 5:15 PM
ebooks Programming Computer Science
Welcome Guest | RSS
Site menu
Section categories
My articles [23]
Main » Articles » My articles

Quick sort_C
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

void qsort(int [],int,int);

int main()
{
int i,num[20],size;
//printf("How many numbers u want: ");
//scanf("%d",&size);
size = 20;

for(i=0;i<size;++i)
num[i] = rand()%100;
//scanf("%d",&num[i]);
qsort(num,0,size-1);

for(i = 0; i < size; i++)
printf("%d ",num[i]);

return 0;
}

void qsort(int num[],int first,int last)
{

if(last<=first)return;
int pivot,i,j,k,n=first;
int *newaray = (int*) malloc((last-first+1)*sizeof(int));

i = 0;
j = last-first;

pivot = num[first];

for(k = first+1; k <= last; k++)
{
if(num[k]<pivot){
newaray[i] = num[k];
i++;
}
else{
newaray[j] = num[k];
j--;
}
}


qsort(newaray,0,i-1);
qsort(newaray,i+1,last-first);


for(k = 0; k < i; k++)
num[first++] = newaray[k];
num[first++] = pivot; 

for(k = i+1; k <= last-n; k++)
num[first++] = newaray[k];


}
Category: My articles | Added by: Sumrat (2012-03-25)
Views: 261 | Rating: 0.0/0
Total comments: 0
Name *:
Email *:
Code *:
Our poll
Rate my site
Total of answers: 152
Statistics

Total online: 1
Guests: 1
Users: 0
Login form