0% found this document useful (0 votes)
3 views2 pages

Prog 2

The document presents a Bubble Sort algorithm implemented in C to sort an array of integers in ascending order. It includes the code for the optimized Bubble Sort function, a swap function, and a print function to display the sorted array. The main function initializes an array, calls the sorting function, and outputs the sorted result.

Uploaded by

hritik raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Prog 2

The document presents a Bubble Sort algorithm implemented in C to sort an array of integers in ascending order. It includes the code for the optimized Bubble Sort function, a swap function, and a print function to display the sorted array. The main function initializes an array, calls the sorting function, and outputs the sorted result.

Uploaded by

hritik raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Department of BCA Data Structure LAB Program No:_______

PROGRAM 02: Given {5,3,1,6,0,2,4} order the numbers in ascending order using
Bubble Sort Algorithm.

PROGRAM CODE:

// Optimized implementation of Bubble sort


#include<stdio.h>
#include<stdlib.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// An optimized version of Bubble Sort


void bubbleSort(int arr[], int n)
{
int i, j;
int swapped;
for (i = 0; i < n-1; i++)
{
swapped = 0;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = 1;
}
}

// IF no two elements were swapped by inner loop, then break


if (swapped == 0)
break;
}
}

ACHARYA INSTITUTE OF GRADUATE STUDIES. Page 1


Department of BCA Data Structure LAB Program No:_______

/* Function to print an array */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
//printf("n");
}

// Driver program to test above functions


int main()
{
int arr[] = {5, 3, 1, 6, 0, 2, 4};
int n = sizeof(arr)/sizeof(arr[0]);
clrscr();
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr,n);
return 0;
}
OUTPUT:

ACHARYA INSTITUTE OF GRADUATE STUDIES. Page 2

You might also like