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

Bubble Sort

Bubble sort is an algorithm that compares adjacent elements in an array and swaps them if they are in the wrong order, repeatedly passing through the list until it is fully sorted. It works by "bubbling up" the largest values to the end of the array similar to how bubbles rise up in water. The example code provided implements bubble sort to sort user input integers into ascending order by repeatedly checking if adjacent elements are in the wrong order and swapping them if needed.

Uploaded by

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

Bubble Sort

Bubble sort is an algorithm that compares adjacent elements in an array and swaps them if they are in the wrong order, repeatedly passing through the list until it is fully sorted. It works by "bubbling up" the largest values to the end of the array similar to how bubbles rise up in water. The example code provided implements bubble sort to sort user input integers into ascending order by repeatedly checking if adjacent elements are in the wrong order and swapping them if needed.

Uploaded by

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

Practical-2

Objective:
To understand Bubble sort and merge sort implement it.

Introduction:
Bubble sort is a sorting algorithm that compares two adjacent elements and swaps
them until they are in the intended order. Just like the movement of air bubbles in the
water that rise up to the surface, each element of the array move to the end in each
iteration. Therefore, it is called a bubble sort.

Example:

Algorithm:
begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

return list

end BubbleSort

Code:
#include <stdio.h>
#include<conio.h>
int main() {
int n, i, j, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int array[n];
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &array[i]);
}
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
printf("The sorted array is: ");
for (i = 0; i < n; i++) {
printf("%d ", array[i]);
}
printf("\n");

return 0;
}

Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy