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

C Program to Bubble Sort Using C Programming Language

How to write a C Program to Bubble Sort in C Programming Language ?

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position.

See More: Bubble sort

Solution For C Program :

C Program to sort an array using bubble sort

How to write a C Program to sort an array using bubble sort in C Programming Language ?

This C Program to sort an array using bubble sort.

Solution:

  1. #include<stdio.h>
  2. main()
  3. {
  4.         int n,i,j,temp;
  5.         printf("Enter the number of elements: ");
  6.         scanf("%d",&n);
  7.         int a[n];
  8.         printf("Enter the elements in the array\n");
  9.         for(i=0;i<n;i++)
  10.                 scanf("%d",&a[i]);
  11.         for(i=0;i<n;i++)
  12.         {
  13.                 for(j=i+1;j<n;j++)
  14.                 {
  15.                         if(a[i]>a[j])
  16.                         {
  17.                                 temp=a[i];
  18.                                 a[i]=a[j];
  19.                                 a[j]=temp;
  20.                         }
  21.                 }
  22.         }
  23.         printf("\nThe sorted array is - \n" );
  24.         for(i=0;i<n;i++)
  25.                 printf("%d, ",a[i]);
  26. }

C Program Bubble Sort

How to write a C Program Bubble Sort in C Programming Language ?


Solution:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. void ordena(int *v, int tam);
  5.  
  6. void ordena(int *v, int tam){
  7.         int i,j,k,aux,cont=0,x=0;
  8.         for(i=0;i<tam/2;i++){
  9.                 cont++;
  10.                 for(j=0,k=tam-1;j<tam/2&&k>(tam/2)-1;j++,k--){
  11.                         cont++;
  12.                         if(*(v+j)>*(v+j+1)){
  13.                                 aux=*(v+j);
  14.                                 *(v+j)=*(v+j+1);
  15.                                 *(v+j+1)=aux;
  16.                                 x=1;
  17.                         }
  18.                         if(*(v+k)<*(v+k-1)){
  19.                                 aux=*(v+k);
  20.                                 *(v+k)=*(v+k-1);
  21.                                 *(v+k-1)=aux;
  22.                                 x=1;
  23.                         }
  24.                 }
  25.                 if(x==0)
  26.                         break;
  27.         }
  28. }

Bubble Sort Cadastro



void bubblesort() {
 
        struct FichaCadastro auxiliar;
        int ordenacao, ordenacao2, permutacao;

for (ordenacao = 0; ordenacao < NumeroCadastro; ordenacao++)
                {
                        for (ordenacao2 = 0; ordenacao2 < NumeroCadastro - 1; ordenacao2++)
                        {
                                if (toupper(pessoas[ordenacao2].nome[0]) > toupper(pessoas[ordenacao2 + 1].nome[0]))
                                {
                                        auxiliar = pessoas[ordenacao2];
                                        pessoas[ordenacao2] = pessoas[ordenacao2 + 1];
                                        pessoas[ordenacao2 + 1] = auxiliar;
                                }
                        }
                }
 
                exibicao();
}