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();
}

Bubble Sort Exemplo



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

void decrescente(int vet[], int size)
{
    int i, j, old;

    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size - 1; j++)
        {
            if (vet[j] < vet[j+1])
            {
                old = vet[j];
                vet[j] = vet[j+1];
                vet[j+1] = old;
            }
        }
    }
}

void crescente(int vet[], int size)
{
    int i, j, old;

    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size - 1; j++)
        {
            if (vet[j] > vet[j+1])
            {
                old = vet[j];
                vet[j] = vet[j+1];
                vet[j+1] = old;
            }
        }
    }
}

int main()
{
    int vet1[15] = { 5,6,90,40,20,30,16,32,64,2048,4096,9,10,11,23 };
    int vet2[15] = { 5,6,90,40,20,30,16,32,64,2048,4096,9,10,11,23 };
    int vet3[15] = { 5,6,90,40,20,30,16,32,64,2048,4096,9,10,11,23 };
    int vet4[15] = { 5,6,90,40,20,30,16,32,64,2048,4096,9,10,11,23 };

    int i, j, old;

    for (i = 0; i < 15; i++)
    {
        for (j = 0; j < 15 - 1; j++)
        {
            if (vet1[j] < vet1[j+1])
            {
                old = vet1[j];
                vet1[j] = vet1[j+1];
                vet1[j+1] = old;
            }
        }
    }

    printf("Decrescente: ");
    for (i = 0; i < 15; i++)
        printf("%d ", vet1[i]);
    printf("\n");

    for (i = 0; i < 15; i++)
    {
        for (j = 0; j < 15 - 1; j++)
        {
            if (vet2[j] > vet2[j+1])
            {
                old = vet2[j];
                vet2[j] = vet2[j+1];
                vet2[j+1] = old;
            }
        }
    }

    printf("Crescente: ");
    for (i = 0; i < 15; i++)
        printf("%d ", vet2[i]);
    printf("\n");

    printf("\nPor funcao:\n");


    crescente(vet3, 15);

    printf("Crescente: ");
    for (i = 0; i < 15; i++)
        printf("%d ", vet3[i]);
    printf("\n");

    decrescente(vet4, 15);

    printf("Decrescente: ");
    for (i = 0; i < 15; i++)
        printf("%d ", vet4[i]);
    printf("\n");


    return 0;
}

C Program to Implemention Bubble Sort using array

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

/* Bubble sort : In bubble sort, we compare first two elements of array; then move one block ahead and compare the elements again until the last element; at the end we have the highest numeric value to the end; This process is iterated n times ( n -> no. of elements in array) and finally the sorted output is attained... We have used (6-i) in the "second for loop" so as to improve the performance as the last element(n) is not required to be computed as the iteration proceeds n times....*/


// bubble sort

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

int main()
{
int arr[6],i,j,temp,n;

printf("Enter the number of elements in the array\n");
scanf("%d",&n);

for(i=0; i<n;i++)
{

printf("Enter the number of elements in the array\n");
scanf("%d",&arr[i]);
}


for(i=0; i<n; i++)
{

for(j=1; j<=n-i; j++)
{

if(arr[j-1] > arr[j])
{
temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
}

else if(arr[j-1] < arr[j])
{
break;
}
}

}

for(i=0; i<n; i++)
{
printf("The sorted order is: \n");
printf("%d\n",arr[i]);
}
}