[go: up one dir, main page]

0% found this document useful (0 votes)
11 views2 pages

Dsa 1

The document provides a C program that implements the bubble sort algorithm to sort an array of numbers. It includes functions to perform the sorting and to print the sorted array. The program prompts the user to input the number of elements and the elements themselves, then displays the sorted array.

Uploaded by

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

Dsa 1

The document provides a C program that implements the bubble sort algorithm to sort an array of numbers. It includes functions to perform the sorting and to print the sorted array. The program prompts the user to input the number of elements and the elements themselves, then displays the sorted array.

Uploaded by

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

1 Write a program to sort an array of numbers using bubble sort.

#include <stdio.h>

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

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

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

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

int temp = arr[j];

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

arr[j + 1] = temp;

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

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

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

printf("\n");

int main() {

int n;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);

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

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

bubbleSort(arr, n);

printf("Sorted array:\n");

printArray(arr, n);

return 0;

/*

Output:

Enter the number of elements: 5

Enter 5 elements:

64 34 25 12 22

Sorted array:

12 22 25 34 64

*/

You might also like