#include<stdio.h>
void quick_sort_mid(int a[], int k, int l);
int partition(int a[], int k, int l);
int main()
{
int arr[100], n, i, j;
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
printf("\n= = = = = Quick Sort by taking middle element as pivot in C = = = = =\n");
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
printf("How many integers you wanna store..? : ");
scanf("%d",&n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf(" %d",&arr[i]);
}
printf("Before sorting the array elements are:\n");
for(j=0; j<n; j++)
{
printf("%d\t", arr[j]);
}
quick_sort_mid(arr,0,n-1);
printf("\n\nAfter sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
return 0;
}
void quick_sort_mid(int arr[], int k, int l)
{
if(k<l)
{
int p;
p=partition(arr,k,l);
quick_sort_mid(arr, k, p-1);
quick_sort_mid(arr, p+1, l);
}
}
int partition(int arr[], int m, int n)
{
int i, j, temp, pivot;
i=m;
j=n;
pivot=(m+n)/2;
while(i<j)
{
while(arr[i]<=arr[pivot] && i<pivot)
{
i++;
}
while(arr[j]>=arr[pivot] && j>pivot)
{
j--;
}
if(i!=pivot && j!=pivot)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
else if(i<pivot && j==pivot)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
pivot=i;
}
else if(j>pivot && i==pivot)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
pivot=j;
}
}
return pivot;
}
OutPut:
manoj@ubuntu:~/cp$ gcc quicksort_mid.c -o quicksort_mid
manoj@ubuntu:~/cp$ ./quicksort_mid
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
= = = = = Quick Sort by taking middle element as pivot in C = = = = =
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
How many integers you wanna store..? : 7
Enter 7 integers into the array:
4
7
5
7
6
7
2
Before sorting the array elements are:
4 7 5 7 6 7 2
After sorting the array elements are:
2 4 5 6 7 7 7
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
void quick_sort_mid(int a[], int k, int l);
int partition(int a[], int k, int l);
int main()
{
int arr[100], n, i, j;
printf("\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
printf("\n= = = = = Quick Sort by taking middle element as pivot in C = = = = =\n");
printf("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\n");
printf("How many integers you wanna store..? : ");
scanf("%d",&n);
printf("Enter %d integers into the array:\n", n);
for(i=0; i<n; i++)
{
scanf(" %d",&arr[i]);
}
printf("Before sorting the array elements are:\n");
for(j=0; j<n; j++)
{
printf("%d\t", arr[j]);
}
quick_sort_mid(arr,0,n-1);
printf("\n\nAfter sorting the array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",arr[i]);
}
printf("\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
return 0;
}
void quick_sort_mid(int arr[], int k, int l)
{
if(k<l)
{
int p;
p=partition(arr,k,l);
quick_sort_mid(arr, k, p-1);
quick_sort_mid(arr, p+1, l);
}
}
int partition(int arr[], int m, int n)
{
int i, j, temp, pivot;
i=m;
j=n;
pivot=(m+n)/2;
while(i<j)
{
while(arr[i]<=arr[pivot] && i<pivot)
{
i++;
}
while(arr[j]>=arr[pivot] && j>pivot)
{
j--;
}
if(i!=pivot && j!=pivot)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
else if(i<pivot && j==pivot)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
pivot=i;
}
else if(j>pivot && i==pivot)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
pivot=j;
}
}
return pivot;
}
OutPut:
manoj@ubuntu:~/cp$ gcc quicksort_mid.c -o quicksort_mid
manoj@ubuntu:~/cp$ ./quicksort_mid
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
= = = = = Quick Sort by taking middle element as pivot in C = = = = =
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
How many integers you wanna store..? : 7
Enter 7 integers into the array:
4
7
5
7
6
7
2
Before sorting the array elements are:
4 7 5 7 6 7 2
After sorting the array elements are:
2 4 5 6 7 7 7
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
No comments:
Post a Comment