gpt-4o-2024-05-13· OpenAI
WildChat-4.8M (AI2)
Generat a code in c program bubble short
- User
Generat a code in c program bubble short
- Assistant
Sure! Below is a simple implementation of the bubble sort algorithm in C:
#include <stdio.h> // Function to perform bubble sort void bubbleSort(int arr[], int n) { int i, j, temp; for (i = 0; i < n-1; i++) { // Last i elements are already sorted, no need to check them for (j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { // Swap arr[j] and arr[j+1] temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } // Function to print an array void printArray(int arr[], int size) { int i; for (i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {64, 34, 25, 12, 22, 11, 90}; int n = sizeof(arr)/sizeof(arr[0]); printf("Unsorted array: \n"); printArray(arr, n); bubbleSort(arr, n); printf("Sorted array: \n"); printArray(arr, n); return 0; }