Write a program to sort the given array using Bubble Sort.

 

Write a program to sort the given array using Bubble Sort.

 

 

#include <stdio.h>

 

void bubbleSort(int array[], int size)

{

  for (int step = 0; step < size - 1; ++step)

  {

 

 

    int swapped = 0;

 

    for (int i = 0; i < size - step - 1; ++i) {

 

      if (array[i] > array[i + 1]) {

       

        int temp = array[i];

        array[i] = array[i + 1];

        array[i + 1] = temp;

        swapped = 1;

      }

    }

 

    if (swapped == 0)

      break;

  }

}

 

void printarray(int array[], int size) {

  for (int i = 0; i < size; ++i) {

    printf("%d  ", array[i]);

  }

  printf("\n");

}

 

int main() {

  int data[100];

  int size ;

  printf("Enter number of elements:\n");

  scanf("%d",&size);

  printf("Enter:\n");

  for(int i=0; i<size; i++) 

{                                                                               

      printf("Element[%d]: ",i);

      scanf("%d",&data[i]);

  }

 

  bubbleSort(data, size);

  printf("Sorted Array in Ascending Order:\n");

  printarray(data, size);

}


    OUTPUT:



                                                                                             

Comments

Popular posts from this blog

[ C++ program] Assume that a bank maintains two kinds of accounts for customers, | | one called as savings account and the other as current account. | | The savings account provides compound interest and withdrawal facilities but no cheque book facility. | | The current account provides cheque book facility but no interest. | | Current account holders should also maintain a minimum balance | | and if the balance falls below this level, a service charge is imposed. | | Create a class account that stores customer name, account number and type of account. | | From this derive the classes curacct and savacct to make them more specific to their requirements.

Perform all operation in a singly linked list using C language:

Write a program in C++ to create a base class shape and derive two classes named Circle and Square from it. Create and initialize objects from these classes using appropriate constructors.