logo

Programa d'ordenació de bombolles en C

Classificació de bombolles és un algorisme d'ordenació senzill i intuïtiu. Canvia repetidament els elements adjacents si estan en l'ordre incorrecte fins que s'ordena la matriu. En aquest algorisme, l'element més gran 'bombolla' fins al final de la matriu en cada iteració. L'ordenació de bombolles és ineficient per a conjunts de dades grans, però és útil per a finalitats educatives i conjunts de dades petits. En aquest article, implementarem l'algorisme d'ordenació de bombolles en llenguatge de programació C.

El primer pas és definir la funció d'ordenació de bombolles. Aquesta funció pren com a paràmetres una matriu enter i la mida de la matriu. La funció no retorna res ja que modifica la matriu original. Aquí hi ha definició de la funció:

 void bubble_sort(int arr[], int n) { int i, j; for (i = 0; i <n - 1; i++) { for (j="0;" j <n i j++) if (arr[j]> arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } </n>

La funció té dos bucles. El bucle exterior va des del primer element fins al darrer element de la matriu. El bucle interior va des del primer element fins a l'últim element de la part no ordenada de la matriu. La condició del bucle intern és n - i - 1 perquè els darrers i elements de la matriu ja estan ordenats.

En cada iteració del bucle interior, comparem elements adjacents. Si l'element esquerre és més gran que l'element dret, els intercanviem. Un cop finalitzat el bucle interior, es garanteix que l'element més gran es troba al final de la part no ordenada de la matriu.

Ara, podem escriure la funció principal per provar la nostra implementació d'ordenació de bombolles. Aquí teniu la funció principal juntament amb la part anterior:

Programa C:

 #include void bubble_sort(int arr[], int n) { int i, j; for (i = 0; i <n - 1; i++) { for (j="0;" j <n i j++) if (arr[j]> arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr) / sizeof(arr[0]); bubble_sort(arr, n); printf(&apos;Sorted array: &apos;); for (int i = 0; i <n; i++) { printf('%d ', arr[i]); } return 0; < pre> <p>The main function creates an integer array arr of size 7 and initializes it with random numbers. We then calculate the size of the array by dividing the size of the array by the size of an integer element. Next, we call the bubble_sort function to sort the array. Finally, we print the sorted array using a for loop.</p> <p> <strong>When we run the program, we should see the following output:</strong> </p> <pre> Sorted array: 11 12 22 25 34 64 90 </pre> <p>This output shows that our bubble sort implementation correctly sorted the array in ascending order.</p> <p>To run the program, we need to compile it using a C compiler. Here is an example <strong>compilation command for GCC:</strong> </p> <pre> gcc -o bubble_sort bubble_sort.c </pre> <p>This command compiles the bubble_sort.c file and produces an executable file named bubble_sort.</p> <p>In summary, the bubble sort algorithm repeatedly swaps adjacent elements until the array is sorted. The algorithm has a time complexity of O(n<sup>2</sup>), which makes it inefficient for large data sets. However, it is useful for educational purposes and small data sets. We implemented the bubble sort algorithm in C programming language and tested it using a simple example.</p> <h3>Characteristics:</h3> <ul> <li>Bubble sort is a simple sorting algorithm.</li> <li>It works by repeatedly swapping adjacent elements if they are in the wrong order.</li> <li>The algorithm sorts the array in ascending or descending order.</li> <li>It has a time complexity of O(n<sup>2</sup>) in the worst case, where n is the size of the array.</li> </ul> <h3>Usage:</h3> <ul> <li>Bubble sort is useful for educational purposes and small data sets.</li> <li>It is not suitable for large data sets because of its time complexity.</li> </ul> <h3>Advantages:</h3> <ul> <li>Bubble sort is easy to understand and implement.</li> <li>It requires minimal additional memory space to perform the sorting.</li> </ul> <h3>Disadvantages:</h3> <ul> <li>It is not efficient for large data sets because of its time complexity.</li> <li>It has poor performance compared to other sorting algorithms, such as quicksort and mergesort.</li> </ul> <h2>Conclusion:</h2> <p>Bubble sort is a simple and intuitive sorting algorithm that is useful for educational purposes and small data sets. However, its time complexity makes it inefficient for large data sets. Therefore, it is not commonly used in real-world applications. Other sorting algorithms, such as quicksort and mergesort, are more efficient for large data sets.</p> <hr></n;></n>

Aquesta sortida mostra que la nostra implementació d'ordenació de bombolles va ordenar correctament la matriu en ordre ascendent.

Per executar el programa, hem de compilar-lo mitjançant un compilador C. Aquí teniu un exemple ordre de compilació per a GCC:

 gcc -o bubble_sort bubble_sort.c 

Aquesta ordre compila el fitxer bubble_sort.c i produeix un fitxer executable anomenat bubble_sort.

En resum, l'algoritme d'ordenació de bombolles intercanvia repetidament elements adjacents fins que s'ordena la matriu. L'algorisme té una complexitat temporal de O(n2), cosa que fa que sigui ineficient per a grans conjunts de dades. Tanmateix, és útil per a finalitats educatives i conjunts de dades petits. Hem implementat l'algoritme d'ordenació de bombolles en llenguatge de programació C i el vam provar amb un exemple senzill.

Característiques:

  • L'ordenació de bombolles és un algorisme d'ordenació senzill.
  • Funciona intercanviant repetidament elements adjacents si estan en l'ordre incorrecte.
  • L'algorisme ordena la matriu en ordre ascendent o descendent.
  • Té una complexitat temporal de O(n2) en el pitjor dels casos, on n és la mida de la matriu.

Ús:

  • L'ordenació de bombolles és útil amb finalitats educatives i conjunts de dades petits.
  • No és adequat per a grans conjunts de dades a causa de la seva complexitat temporal.

Avantatges:

  • L'ordenació de bombolles és fàcil d'entendre i implementar.
  • Requereix un espai de memòria addicional mínim per dur a terme l'ordenació.

Desavantatges:

  • No és eficient per a grans conjunts de dades a causa de la seva complexitat temporal.
  • Té un rendiment baix en comparació amb altres algorismes d'ordenació, com ara quicksort i mergesort.

Conclusió:

Bubble sort és un algorisme d'ordenació senzill i intuïtiu que és útil per a finalitats educatives i petits conjunts de dades. Tanmateix, la seva complexitat temporal el fa ineficient per a grans conjunts de dades. Per tant, no s'utilitza habitualment en aplicacions del món real. Altres algorismes d'ordenació, com ara quicksort i mergesort, són més eficients per a grans conjunts de dades.