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

Bubble

Uploaded by

ritiktyagi0123
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)
10 views2 pages

Bubble

Uploaded by

ritiktyagi0123
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

Bubble Sort Algorithm

i) Repeat the following steps for each element in the array, starting from the
beginning:

a) For each element up to the second-to-last unsorted element, compare it with the
next element:

- If the current element is greater than the next element, swap them.

b) After each pass, the largest unsorted element will have "bubbled up" to its correct
position, so reduce the range of elements to be checked in the next pass.

ii) Continue this process until no swaps are needed in a pass, indicating that the
array is fully sorted.

3.) Write a program in C to implement a Bubble Sort

#include <stdio.h>

void bubblesort(int arr[], int n) {

int i, j;

for (i = 0; i < n - 1; i++) {

for (j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);

bubblesort(arr, n);

printf("Sorted array: ");

for (int i = 0; i < n; i++) {


printf("%d ", arr[i]);

return 0;

OUTPUT

Sorted array: 11 12 22 25 34 64 90

You might also like