Assignment Programming
Bubble Sort Algorithm using Loops
Submitted by:
Waleed Ghaffar
Reg. No# 70148780
Dated: 12th March, 2025.
Department of Computer Science
The University of Lahore.
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int size)
{
for (int i = 0; i < size - 1; ++i)
{
for (int j = 0; j < size - i - 1; ++j)
{
if (arr[j] > arr[j + 1])
{
swap(arr[j], arr[j + 1]);
}
}
}
}
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int size;
cout << "\nEnter the size of the array: ";
cin >> size;
int *arr = new int[size];
cout << "\nEnter the array elements: ";
for (int i=0;i<size;i++)
{
cin>>arr[i];
}
cout << "Unsorted array: ";
printArray(arr, size);
bubbleSort(arr, size);
cout << "Sorted array: ";
printArray(arr, size);
delete[] arr;
return 0;
}
Output: