0% found this document useful (0 votes)
3 views

implementation of Bubble Sort using an array in C

The document contains a C implementation of the Bubble Sort algorithm using an array. It defines a function to sort the array and another to print the sorted array. The main function demonstrates sorting a predefined array and outputs the sorted result.

Uploaded by

devanshkadam2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

implementation of Bubble Sort using an array in C

The document contains a C implementation of the Bubble Sort algorithm using an array. It defines a function to sort the array and another to print the sorted array. The main function demonstrates sorting a predefined array and outputs the sorted result.

Uploaded by

devanshkadam2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

{ } implementation of Bubble Sort using an array in C.

#include <stdio.h>

// Bubble sort function

void bubbleSort(int arr[], int n) {

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

// Flag to detect if any swap happens

int swapped = 0;

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

// Swap if the element is greater than the next one

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

swapped = 1;

// If no two elements were swapped, break early

if (!swapped)

break;

// Function to print array

void printArray(int arr[], int n) {


printf("Sorted array: ");

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

printf("%d ", arr[i]);

printf("\n");

// Main function

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);

printArray(arr, n);

return 0;

OUTPUT –

Sorted array: 11 12 22 25 34 64 90

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy