Showing posts with label Bubble sort. Show all posts
Showing posts with label Bubble sort. Show all posts

Friday, 27 March 2015

Cpp Program for sorting elements using bubble sort


This is a Cpp program that performs sorting of elements using bubble sort.

Bubble sort is a technique where each element is compared with its adjacent element , if its adjacent element is smaller then swapping occurs, this continues until every element is checked.




See the animation below to understand,
Bubble-sort-example-300px.gif

"Bubble-sort-example-300px" by Swfung8 - Own work. Licensed under CC BY-SA 3.0 via Wikimedia Commons.


Download Code
PROGRAM : 
#include <iostream>

using namespace std;

int main()
{
    int n, i, j, swap;
    cout << "How many elements ? :\t";
    cin >> n;
    int array[n];
    for (i = 0; i < n; i++)
    {
        cout << "Enter number " << i+1 << ": \t";
        cin >> array[i];
    }

    for (i = 0 ; i < ( n - 1 ); i++)
    {
        for (j = 0 ; j < n - i - 1; j++)
        {
            if (array[j] > array[j+1])
            {
                swap       = array[j];
                array[j]   = array[j+1];
                array[j+1] = swap;
            }
        }
    }
    cout << "\nSorted list :\n";
    for ( i = 0 ; i < n ; i++ )
        cout << array[i] << "\t";
    return 0;
}

OUTPUT  : 
Bubble Sort



Download Code

Tuesday, 6 January 2015

Java Program to sort elements using Bubble Sort

This is a Java program that performs sorting of elements using bubble sort.

Bubble sort is a technique where each element is compared with its adjacent element , if its adjacent element is smaller then swapping occurs, this continues until every element is checked.




See the animation below to understand,
Bubble-sort-example-300px.gif

"Bubble-sort-example" by Swfung8 - Own work. Licensed under CC BY-SA 3.0 via Wikimedia Commons.

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class BubbleSort {
 public static void main(String[] args) {
  int n, i, j, swap;
  int[] array = new int[100];
  Scanner scan = new Scanner(System.in);

  System.out.print("How many Elements ? \t");
  n = scan.nextInt();
  for (i = 0; i < n; i++) {
   System.out.print("Enter number" + (i + 1));
   array[i] = scan.nextInt();
  }
  scan.close();

  for (i = 0; i < (n - 1); i++) {
   for (j = 0; j < n - i - 1; j++) {
    if (array[j] > array[j + 1]) {
     swap = array[j];
     array[j] = array[j + 1];
     array[j + 1] = swap;
    }
   }
  }
  System.out.print("\nSorted list :\n");
  for (i = 0; i < n; i++)
   System.out.print(array[i] + "\t");
 }
}

OUTPUT :

Sunday, 28 December 2014

C program for sorting elements using bubble sort


This is a C program that performs sorting of elements using bubble sort.

Bubble sort is a technique where each element is compared with its adjacent element , if its adjacent element is smaller then swapping occurs, this continues until every element is checked.

See the animation below to understand,



Bubble-sort-example-300px.gif

"Bubble-sort-example" by Swfung8 - Own work. Licensed under CC BY-SA 3.0 via Wikimedia Commons.


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


int main()
{
    int n, i, j, swap;
    printf("How many elements ?");
    scanf("%d", &n);
    int array[n];
    for (i = 0; i < n; i++)
    {
        printf("Enter number %d\t", 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])
            {
                swap       = array[j];
                array[j]   = array[j+1];
                array[j+1] = swap;
            }
        }
    }
    printf("\nSorted list :\n");
    for ( i = 0 ; i < n ; i++ )
        printf("%d\t", array[i]);
    return 0;
    
}

OUTPUT :

C - Bubble Sort