0% found this document useful (0 votes)
34 views

Dsa Assignment

The document contains code snippets demonstrating bubble sort and selection sort algorithms. The first code sample sorts an array of 10 integers using bubble sort and tracks the number of passes. The second sample separates the bubble sort logic into a function. The document also lists selection sort but does not include any sample code.

Uploaded by

Yashal Ijaz
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)
34 views

Dsa Assignment

The document contains code snippets demonstrating bubble sort and selection sort algorithms. The first code sample sorts an array of 10 integers using bubble sort and tracks the number of passes. The second sample separates the bubble sort logic into a function. The document also lists selection sort but does not include any sample code.

Uploaded by

Yashal Ijaz
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

Name: Yashal Ijaz

Roll no: F2021105016


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:

You might also like