Page 57 - DCAP407_DATA_STRUCTURE
P. 57
Data Structure
void BUBBLE (void); //BUBBLE Function declaration
void main()
{
int i; //Variable declaration
clrscr();
/*Printing the values in the array*/
printf("\n\nValues present in array A =");
for (i=0; i<8; i++) //Iterations using for loop
printf(" %d, ", A[i]); //Printing the array
BUBBLE(); //BUBBLE function is called
/*Printing the values from the array after sorting*/
printf("\n\nValues present in the array after sorting =");
for (i=0; i<8; i++) //Iterations
printf(" %d, ", A[i]); // Printing the array after sorting
getch(); // waits for a key to be pressed
}
void BUBBLE(void) //BUBBLE Function definition
{
int K, PTR, TEMP; //Declaration variables
for(K=0; K <= (N-2); K++) //Iterations
{
PTR = 0; //Assign 0 to variable PTR
while(PTR <= (N-K-1-1)) //Checking if PTR <= (N-K-1-1)
{
/* Checking if the element at A[PTR] is greater than A[PTR+1]*/
if(A[PTR] > A[PTR+1])
{
TEMP = A[PTR];
A[PTR] = A[PTR+1];
A[ PTR +1] = TEMP;
}
/*Increment the array index*/
PTR = PTR+1;
}
}
}
Output:
Values present in A[8] = 55, 22, 2, 43, 12, 8, 32, 15
Values present in A[8] after sorting = 2, 8, 12, 15, 22, 32, 43, 55
In this example:
1. First, the header files are included using #include directive.
2. Then, the array A is declared globally along with the array elements and the
size.
3. Then, inside the main function the variable i is declared.
50 LOVELY PROFESSIONAL UNIVERSITY