Q28.Write a code to sort array elements in ascending order using bubble sort
Bubble Sort :- Bubble sort is also known as sinking sort. Bubble sort is a simple sorting algorithm that compares adjacent elements and swaps them if order is wrong.
Ascending Order :- Numbers are said to be in ascending order when they are arranged from smallest to largest number. Such as 2, 3, 5, 9 and 11 are arranged in ascending order.
Arrange Array elements in ascending order using Bubble Sort Algorithm
START
Step 1: Repeat Step 2 For i = 0 to N
Step 2: Repeat For J = 0 to N – I
Step 3: IF A[J] > A[J+1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP
Step 4: EXIT
STOP
Arrange Array elements in ascending order using Bubble Sort Code
#include <stdio.h>
int main(){
int temp, i, j, number[5]={7,8,3,2,5};
/* This is the main logic of bubble sort algorithm
*/
for(i = 0 ;i < 5; i++){
for(j = 0; j < 5-i-1 ; ++j){
if(number[j] > number[j+1]){
temp = number[j];
number[j] = number[j+1];
number[j+1] = temp;
}
}
}
printf(“Sorted Array: “);
for(i=0;i<5;i++)
printf(” %d”,number[i]);
return 0;
}
#include <iostream>
using namespace std;
void bubble_Sort(int array[], int size)
{
for (int i = 0; i < size – 1; ++i)
{
for (int i = 0; i < i – i – 1; ++i)
{
// To sort in descending order, change > to < in this line.
if (array[i] > array[i + 1])
{
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
void print_Array(int array[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << ” ” << array[i];
}
cout << “\n”;
}
int main()
{
int number[] = {7,8,3,2,5};
int size = sizeof(number) / sizeof(number[0]);
bubble_Sort(number, size);
cout << “Sorted Array :\n”;
print_Array(number, size);
}
}
import java.util.Arrays;
class LFC {
void bubble_Sort(int array[]) {
int size = array.length;
for (int i = 0; i < size – 1; i++)
for (int j = 0; j < size – i – 1; j++)
// To sort in descending order, change > to < in this line.
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
public static void main(String args[]) {
int[] number = {7,8,3,2,5};
LFC bs = new LFC();
bs.bubble_Sort(number);
System.out.println(“Sorted Array :”);
System.out.println(Arrays.toString(number));
}
def bubble_Sort(array):
for i in range(len(array)):
for j in range(0, len(array) – i – 1):
# To sort in descending order, change > to < in this line.
if array[j] > array[j + 1]:
(array[j], array[j + 1]) = (array[j + 1], array[j])
number = [ 7,8,3,2,5 ];
bubble_Sort(number)
print(‘Sorted Array: ‘)
print(number)}
function bubble_Sort(&$number)
{
$n = sizeof($number);
// Traverse through all array elements
for($i = 0; $i < $n; $i++)
{
$temp = False;
// Last i elements are already
// in place
for ($j = 0; $j < $n – $i – 1; $j++)
{
if ($number[$j] > $number[$j+1])
{
$t = $number[$j];
$number[$j] = $number[$j+1];
$number[$j+1] = $t;
$temp = True;
}
}
// IF no two elements were swapped
// by inner loop, then break
if ($temp == False)
break;
}
}
// Driver code to test above
$number = array(7,8,3,2,5);
$len = sizeof($number);
bubble_Sort($number);
echo “Sorted Array : \n”;
for($i = 0; $i < $len; $i++)
echo $number[$i].” “;
Output
Sorted Array: 2 3 5 7 8
Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number