Dsa Assignment
Dsa Assignment
Bubble Sort
Code 1:
#include<iostream>
using namespace std;
int main()
{
int i;
int j;
int temp;
int pass = 0;
int a[10] = { 10,2,0,14,43,25,18,1,5,45 };
cout << "Input list ...\n";
for (i = 0; i < 10; i++)
{
cout << a[i] << "\t";
}
cout << endl;
for (i = 0; i < 10; i++)
{
for (j = i + 1; j < 10; j++)
{
if (a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
pass++;
}
cout << "Sorted Element List ...\n";
for (i = 0; i < 10; i++)
{
cout << a[i] << "\t";
}
cout << "\nNumber of passes taken to sort the list:" << pass << endl;
Code 2:
#include<iostream>
using namespace std;
void bubbleSort(int[]);
int main()
{
int i, arr[10];
cout << "Enter 10 Elements: ";
for (i = 0; i < 10; i++)
cin >> arr[i];
bubbleSort(arr);
cout << "\nThe New Sorted Array is: \n";
for (i = 0; i < 10; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
void bubbleSort(int arr[])
{
int i, j, temp;
for (i = 0; i < 9; i++)
{
for (j = 0; j < (10 - i - 1); j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Selection Sort
Code 1: