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;