[go: up one dir, main page]

0% found this document useful (0 votes)
162 views9 pages

Dsa Practical

The document is a practical file submitted by Aditya Sharma (2321008) for their Data Structures and Algorithms lab course. It contains 10 programs aimed at implementing various array operations like traversing, inserting, deleting, and sorting elements in an array. For each program, it provides the aim, procedure, source code, and sample output. The 4th program demonstrates bubble sorting an array by traversing it with two nested for loops and swapping adjacent elements if out of order.

Uploaded by

Aditya sharma
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)
162 views9 pages

Dsa Practical

The document is a practical file submitted by Aditya Sharma (2321008) for their Data Structures and Algorithms lab course. It contains 10 programs aimed at implementing various array operations like traversing, inserting, deleting, and sorting elements in an array. For each program, it provides the aim, procedure, source code, and sample output. The 4th program demonstrates bubble sorting an array by traversing it with two nested for loops and swapping adjacent elements if out of order.

Uploaded by

Aditya sharma
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/ 9

2321008

PRACTICAL FILE
Of
“DATA STRUCTURE AND ALGORITHMS LAB”

SUBJECT CODE: PC-CS-205AL

SUBMITTED IN PARTIAL FULFILLMENT OF THE

REQUIREMENTS FOR THE AWARD OF

BACHELORS OF TECHNOLOGY (B.TECH)

IN

COMPUTER SCIENCE AND ENGINEERING

( SESSION: 2022-2023)

SUBMITTED BY

Aditya Sharma

2321008

BRANCH: Computer Science and Engineering

UNDER THE SUPERVISION OF:

Er. Rajwinder Kaur


Assistant Professor
CSE Department,ACE

Department Of Computer Science and Engineering

Ambala College of Engineering and Applied Research, Devsthali, Ambala

1
2321008

S.NO PRACTICAL AIM DATE SIGNATURE REMARKS

1. Traversing of an Array.

2. Insert Element in an Array.

Delete an Element from an


3.
Array.

4.

5.

6.

7.

8.

9.

10.

2
2321008

Practical No. 1
AIM: Traversing of an Array.

Software used: Code Blocks.

Procedure:
1. [Initialize Counter] set K=LB.
2. Repeat Step 3 an d4 while K<=UB.
3. {visit Element] Apply Process to LA[k].
4. [increase Counter] set K = K+1.
5. [End of while loop]
6. Exit.

Source Code:

#include<iostream>
using namespace std;
int main() {
int arr[5] ={10,20,41,50,60};
for(int i=0;i<5;i++)
{
cout << arr[i] << " ";
}
return 0;
}

Output:

3
2321008

Practical No. 2
AIM: Insert Element in an Array.

Procedure:
1. [Initialize Counter] set j=N-1.
2. Repeat Step 3 and 4 while j>=position.
3. [Move jth element downward] set LA[j+1] = LA[j].
4. [Decrease counter] set j=j-1.
5. (Ends of Step2 loop)
6. [Insert Element] set LA [position]=value.
7. [Update N] N =N+1.
8. Exit.

Source Code:

#include <iostream>
using namespace std;
int main () {
int position, j=4, c, value, array [9] = {9,0,3,4,8,6,0,9,2};
cout<<"Enter the location where you wish to insert an element:";
cin>>position;
cout<<"Enter the value you want to insert:";
cin>>value;
while(j>=position)
{
array[j+1] = array[j];
j--; }
array[position] = value;
cout<<"Resultant array is : ";
for (c = 0; c <= 9; c++)

4
2321008

{
cout<<array[c]<< " ";
}
return 0;
}

Output:

5
2321008

. Practical No. 3
AIM: Delete an Element from an Array.

Procedure:
1. [Initialize Counter] set j=element.
2. Repeat Step 3 and 4 while j<N.
3. [Move jth element upward] set LA[j] = LA[j+1].
4. [Increase counter] set j=j+1.
5. (Ends of Step2 loop)
6. [Update N] N+1 =N.
7. Exit.

Source Code:

#include<iostream>
using namespace std;
int main ()
{
int arr [5] = {8,5,7,2,1,6}, n, i, elem, j;
cout<<"Enter Element to Delete: ";
cin>>elem;
for (i=0; i<5; i++)
{
if (elem==arr[i])
{
for (j=i; j<5; j++)
{
arr[j] = arr[j+1];
i--;
n--;
}
cout << "Array After deletion:";
for (i=0; i<4; i++)
{cout << arr[i]<< " "; }
}
}

6
2321008

return 0;
}

Output:

7
2321008

Program 4
Aim: sort an Array using bubble sort.

Procedure:

 Run a nested for loop to traverse the input array using two variables i and j,
such that 0 ≤ i < n-1 and 0 ≤ j < n-i-1
 If arr[j] is greater than arr[j+1] then swap these adjacent elements, else
move on
 Print the sorted array
Source code:
#include <iostream>
using namespace std;

int* 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]);
}
}
}
return arr;
}
int main(){
int size;
cout<<"Enter the size of array : ";
cin>>size;
int arr[size];
8
2321008

cout<<"Enter the elements : ";


for(int i = 0;i<size;i++){
cin>>arr[i];
}
bubbleSort(arr,size);
for(int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
return 0;

}
Output:

You might also like