Program Discussion :: Sorting
36 / 6
Write a program to compute the bubble sort for the given input array
Answer:
#include
#include
template
void bubble(bubble a[], int n)
{
int i, j;
for(i=0;i
Asked In ::
Language:
Rahul Jain
15 Jun, 2017 10:50 PM
<!DOCTYPE html>
<html>
<body>
<?php
$arrays = [
[2, 3, 5],
[3, 4, 8],
[3, 4, 5, 8, 9],
];
$flattened = [];
foreach ($arrays as $array) {
foreach ($array as $element) {
$flattened[] = $element;
}
}
for ($x = 0; $x < count($flattened) - 1; $x++)
{
for ($y = $x + 1; $y < count($flattened); $y++)
{
if ($flattened[$x] > $flattened[$y])
{
$temp = $flattened[$x];
$flattened[$x] = $flattened[$y];
$flattened[$y] = $temp;
}
}
}
echo implode(', ', $flattened);
?>
</body>
</html>
Language:
Aswin
7 Jul, 2017 9:30 AM
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
Language:
Neel
7 Jul, 2017 9:30 AM
#include<conio.h>
#include<iostream.h>
template<class bubble>
void bubble(bubble a[], int n)
{
int i, j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
bubble element;
element = a[i];
a[i] = a[j];
a[j] = element;
}
}
}
}
void main()
{
int a[6]={1,2,3,4,4,3};
char b[4]={'s','b','d','e'};
clrscr();
bubble(a,6);
cout<<"\nSorted Order Integers: ";
for(int i=0;i<6;i++)
cout<<a[i]<<"\t";
bubble(b,4);
cout<<"\nSorted Order Characters: ";
for(int j=0;j<4;j++)
cout<<b[j]<<"\t";
getch();
}