SAVITIBAI PHULE UNIVERSITY OF PUNE
S. Y. B.Sc. (Computer Science) Semester III
Practical Examination
SUBJECT: CS-233 Practical course based on CS231
/* Q.1 Sort a random array of n integers (accept the value of n from user) in ascending order by using quick sort algorithm */
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
void CreateArray(int A[], int n)
{
int i = 0;
for ( i = 0; i < n; i++)
A[i] = rand();
}
void DisplayArray(int A[], int n)
{
int i = 0;
for ( i = 0; i < n; i++)
printf("%d \t", A[i]);
}
void QuickSort(int A[], int low, int high)
{
int pivot, t, i, j;
if ( low <= high)
{
pivot = A[low];
i = low + 1;
j = high;
while ( i <= j)
{
while ( pivot >= A[i] && i <= high)
i++;
while ( pivot < A[j] && j >= low)
j--;
if ( i < j)
{
t = A[i];
A[i] = A[j];
A[j] = t;
}
}
A[low] = A[j];
A[j] = pivot;
QuickSort(A, low, j-1);
QuickSort(A, j+1, high);
}
}
int main(){
int A[MAX], num = 0;
printf("Enter How many element we want to in array: ");
scanf("%d", &num);
CreateArray(A, num);
printf("\n\nThe given array elements are: \n\n");
DisplayArray(A, num);
QuickSort(A, 0, num-1);
printf("\n\n");
DisplayArray(A, num);
printf("\n\n");
system("pause");
return 0;
}
No comments:
Post a Comment