#include<stdio.h>
void quick_sort_end(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 first 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_end(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_end(int arr[], int k, int l)
{
if(k<l)
{
int p;
p=partition(arr,k,l);
quick_sort_end(arr, k, p-1);
quick_sort_end(arr, p+1, l);
}
}
int partition(int arr[], int m, int n)
{
int i, j, temp, pivot;
i=n+1;
pivot=m;
for(j=n; j>=m; j--)
{
if(arr[j]>=arr[pivot])
{
i--;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
return i;
}
OutPut:
manoj@ubuntu:~/cp$ gcc quicksort_beg.c -o quicksort_beg
manoj@ubuntu:~/cp$ ./quicksort_beg
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
= = = = = Quick Sort by taking first element as pivot in C = = = = =
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
How many integers you wanna store..? : 8
Enter 8 integers into the array:
2
6
7
3
5
4
8
1
Before sorting the array elements are:
2 6 7 3 5 4 8 1
After sorting the array elements are:
1 2 3 4 5 6 7 8
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
void quick_sort_end(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 first 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_end(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_end(int arr[], int k, int l)
{
if(k<l)
{
int p;
p=partition(arr,k,l);
quick_sort_end(arr, k, p-1);
quick_sort_end(arr, p+1, l);
}
}
int partition(int arr[], int m, int n)
{
int i, j, temp, pivot;
i=n+1;
pivot=m;
for(j=n; j>=m; j--)
{
if(arr[j]>=arr[pivot])
{
i--;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
return i;
}
OutPut:
manoj@ubuntu:~/cp$ gcc quicksort_beg.c -o quicksort_beg
manoj@ubuntu:~/cp$ ./quicksort_beg
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
= = = = = Quick Sort by taking first element as pivot in C = = = = =
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
How many integers you wanna store..? : 8
Enter 8 integers into the array:
2
6
7
3
5
4
8
1
Before sorting the array elements are:
2 6 7 3 5 4 8 1
After sorting the array elements are:
1 2 3 4 5 6 7 8
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
manoj@ubuntu:~/cp$
No comments:
Post a Comment