0% found this document useful (0 votes)
32 views3 pages

DATA Structure

This document contains two C++ programs that implement bubble sort to sort arrays in ascending order. The first program asks the user to input the size of an array and its elements, then calls a bubbleSort function to sort the array. The second program predefines an array of 10 integers and directly calls bubbleSort to sort this array in ascending order, printing the sorted output. Both programs implement the standard bubble sort algorithm by iterating through the array and swapping adjacent elements if out of order.

Uploaded by

Babar Ali
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)
32 views3 pages

DATA Structure

This document contains two C++ programs that implement bubble sort to sort arrays in ascending order. The first program asks the user to input the size of an array and its elements, then calls a bubbleSort function to sort the array. The second program predefines an array of 10 integers and directly calls bubbleSort to sort this array in ascending order, printing the sorted output. Both programs implement the standard bubble sort algorithm by iterating through the array and swapping adjacent elements if out of order.

Uploaded by

Babar Ali
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/ 3

ROLL NO: FA17-EPE-006 NAME: BABAR ALI

1.Write a program that places element of an unsorted array user define array
in ascending order using BubbleSort Algorithm

// below we have a simple C++ program for bubble sort

#include <stdio.h>

void bubbleSort(int arr[], int n)

int i, j, temp;

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

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

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

// swap the elements

temp = arr[j];

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

arr[j+1] = temp;

// print the sorted array

printf("Sorted Array: ");

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

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

}
int main()

int arr[100], i, n, step, temp;

// ask user for number of elements to be sorted

printf("Enter the number of elements to be sorted: ");

scanf("%d", &n);

// input elements if the array

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

printf("Enter element no. %d: ", i+1);

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

// call the function bubbleSort

bubbleSort(arr, n);

return 0;

2.Write a program that places element of an unsorted


array[33,21,28,23,36,34,26,42,22,27] in ascending order using BubbleSort
Algorithm

#include <stdio.h>

void bubbleSort(int arr[], int n)

int i, j, temp;

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


{

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

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

// swap the elements

temp = arr[j];

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

arr[j+1] = temp;

// print the sorted array

printf("Sorted Array: \n");

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

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

// Driver program to test above functions

int main()

int arr[] = {33,21,28,23,36,34,26,42,22,27};

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

bubbleSort(arr, n);

return 0;

You might also like