Friday, 16 March 2018

Bubble Sort in C

Bubble sort is a concept that we use to sorting of elements in ascending order . In C programming , their are many more technique that will do the same but according to the complexity of the program it is better upto some extends

/* Pre-Processors*/
#include<stdio.h>
#include<conio.h>
#define MAX 15 /* Length of MAX*/

void main()
{
    int array[MAX];
    int i, j, n, temp;

    printf("Enter no. of elements for array: ");
    scanf("%d", &n);
    printf("\nEnter the elements of array: \n");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &array[i]);
    }

    for (i = 0; i < n; 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("Array in Ascending...\n");
    for (i = 0; i < n; i++)
    {
        printf("%d\n", array[i]);
    }
    getch();
}


No comments:

Post a Comment