ClubEnsayos.com - Ensayos de Calidad, Tareas y Monografias
Buscar

ORDENAMIENTO BURBUJA EN C


Enviado por   •  12 de Agosto de 2014  •  552 Palabras (3 Páginas)  •  187 Visitas

Página 1 de 3

Have an array you need to put in order? Keeping business records and want to sort them by ID number or last name of client? Then you'll need a sorting algorithm. To understand the more complex and efficient sorting algorithms, it's important to first understand the simpler, but slower algorithms. In this article, you'll learn about bubble sort, including a modified bubble sort that's slightly more efficient; insertion sort; and selection sort. Any of these sorting algorithms are good enough for most small tasks, though if you were going to process a large amount of data, you would want to choose one of the sorting algorithms listed on the advanced sorting page.

Bubble sort

The simplest sorting algorithm is bubble sort. The bubble sort works by iterating down an array to be sorted from the first element to the last, comparing each pair of elements and switching their positions if necessary. This process is repeated as many times as necessary, until the array is sorted. Since the worst case scenario is that the array is in reverse order, and that the first element in sorted array is the last element in the starting array, the most exchanges that will be necessary is equal to the length of the array. Here is a simple example:

Given an array 23154 a bubble sort would lead to the following sequence of partially sorted arrays: 21354, 21345, 12345. First the 1 and 3 would be compared and switched, then the 4 and 5. On the next pass, the 1 and 2 would switch, and the array would be in order.

The basic code for bubble sort looks like this, for sorting an integer array:

#include<stdio.h>

#include<conio.h>

main()

{

int i,j,a[4];

int temp=0;

for(i=0;i<4;i++)

{

printf("Ingrese el %d numero ",i+1);

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

}

for (i=0;i<4;i++)

{

for (j=0; j < 4-1 ;j++)

{

if (a[j] < a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

printf("\nSu lista en descendente es:\n");

for ( i = 0 ; i < 4 ; i++ )

printf("\n%d", a[i]);

getch();

}

...

Descargar como (para miembros actualizados) txt (2 Kb)
Leer 2 páginas más »
Disponible sólo en Clubensayos.com