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 Read the 'n' numbers from user and sort using bubble sort */
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
// CheckFor: Create new Array
void CreateArray(int A[], int n){
// CheckFor: Declaration of local variable
int i = 0;
// Checkfor: Accept the array elements
printf("\n\nEnter the array element: \n");
// LoopFor: Accepet the array elements
for ( i = 0; i < n; i++)
scanf("%d", &A[i]);
}
// CheckFor: Display the Array
void DisplayArray(int A[], int n){
// CheckFor: Declaration of local variable
int i = 0;
// Checkfor: Display the array elements
printf("The array elements are: \t");
// LoopFor: Display the array elements
for ( i = 0; i < n; i++)
printf("%d \t", A[i]);
}
// CheckFor: Sort the Array Element using Bubble Sort Algorithm
void BubbleSort(int A[], int n){
// CheckFor: Declaration of local variable
int i = 0, j = 0, temp = 0;
// LoopFor: Moving the Array from first to last element
for( i = 0; i < n; i++){
// LoopFor: Get element from unsorted array
// and check with sorted Array element
for( j = 0; j < n-i-1; j++){
// Checkfor: First element is greater than second
// if yes then swap the elements.
if ( A[j] > A[j+1]){
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
// CheckFor: Display the Sorted Array
printf("\n\nThe Sorted Array after Bubble Sort is: \n\n");
for ( i = 0; i < n; i++)
printf("%d \t", A[i]);
}
int main(){
int A[MAX], num = 0;
// Accept the n number from user
printf("Enter How many element we want to in array: ");
scanf("%d", &num);
// Accept the n element from user
CreateArray(A, num);
// Display the given array
DisplayArray(A, num);
// Sort the element using insertion sort algorithm
BubbleSort(A, num);
printf("\n\n");
system("pause");
return 0;
}
No comments:
Post a Comment