When I run this program in Dev C++, it executes until "enter the no of elements u want in an array" and when I give 5 numbers as input it crashes. Can anyone help me out? here is an implementation of quick sort using c
#include<stdio.h>
#include<stdlib.h>
void swap(int *a,int *b)//function to swap array
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void quicksort(int arr[],int start,int end)//function for performig quicksort
{
if(start>=end)
{
return;
}
else
{
int pindex= partition(arr,start,end);
quicksort(arr,start,pindex-1);
quicksort(arr,pindex+1,end);
}
}
int partition(int arr[],int start,int end)//function to partition the array
{
int i;
int pivot=arr[end];//always making the last element in the array as pivot
int pindex=arr[start];
for(i=0;i<end;i++)
{
if(arr[i]<=pivot)
{
swap(&arr[i],&arr[pindex]);
}
swap(&arr[pindex],&arr[end]);
}
}
void display(int arr[],int n)//function to display the sorted array
{
int i=0;
for(i=0;i<n;i++)
{
printf("%d",arr[i]);
}
}
void main() // main function initializing here
{
int n,i,arr[20];
printf("enter the no of elements u want in an array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
printf("\n");
}
quicksort(arr,0,n);
display(arr,n);
getch();
}
This is my complete code of quick sort program.i used different functions for different displaying output,swapping numbers partitioning etc.