0% found this document useful (0 votes)
9 views8 pages

Lab 9

The document is a lab journal that outlines 4 tasks for students to implement different sorting algorithms like bubble sort, selection sort, and insertion sort. The tasks instruct students to write C++ programs to sort arrays and linked lists using each algorithm. The lab journal provides code templates and explanations for each sorting algorithm to help students complete the tasks. It also includes a grading sheet for the lab instructor to evaluate students' work.

Uploaded by

asfa rehman
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)
9 views8 pages

Lab 9

The document is a lab journal that outlines 4 tasks for students to implement different sorting algorithms like bubble sort, selection sort, and insertion sort. The tasks instruct students to write C++ programs to sort arrays and linked lists using each algorithm. The lab journal provides code templates and explanations for each sorting algorithm to help students complete the tasks. It also includes a grading sheet for the lab instructor to evaluate students' work.

Uploaded by

asfa rehman
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/ 8

Bahria University, Lahore Campus

Department of Computer Science


Lab Journal 09
(Fall 2023)

Course: Data Structures and Algorithm - Lab Date: _______________


Course Code: CSL-221 Max Marks: 10
Faculty’s Name: Summaira Nosheen

Name: ____ Enroll No: __ Class: _BSIT 3(A)___________

Objective(s):
Upon completion of this lab session, learners will be able to:
Implementation of Insertion Sort

Implementation of Selection Sort

Implementation of Bubble Sort

Lab Tasks:
Task #.1: Estimated Time: 30 Mins.
Write a program in C++ to implement Bubble Sort for sorting ‘n’ number of elements
in an array.

#include <iostream>
Using namespace std;
Void bubbleSort(int arr[], int n) {
For (int I = 0; I < n – 1; ++i) {
For (int j = 0; j < n – I – 1; ++j) {
If (arr[j] > arr[j + 1]) {

Int temp = arr[j];


Arr[j] = arr[j + 1];
Arr[j + 1] = temp;
}
}
}
}

Int main() {
Int n;

Cout << “Enter the number of elements: “;


Cin >> n;

Int arr[n];

Cout << “Enter the elements:\n”;


For (int I = 0; I < n; ++i) {
%
Enrollment Number: ____________________________
Cin >> arr[i];
}

bubbleSort(arr, n);

cout << “Sorted array:\n”;


for (int I = 0; I < n; ++i) {
cout << arr[i] << “ “;
}

Return 0;
}

Page 2 of 8
%
Enrollment Number: ____________________________

Task #.2: Estimated Time: 30 Mins.


Write a program in C++ to implement Bubble Sort for sorting linked list.

#include <iostream>
Using namespace std;
Class Node {
Public:
Int data;
Node* next;

Node(int value) : data(value), next(nullptr) {}


};

Void bubbleSortLinkedList(Node* start) {


If (!start || !start->next) {

Return;
}

Bool swapped;
Node* ptr1;
Node* lptr = nullptr;

Do {
Swapped = false;
Ptr1 = start;

While (ptr1->next != lptr) {


If (ptr1->data > ptr1->next->data) {

Int temp = ptr1->data;


Ptr1->data = ptr1->next->data;
Ptr1->next->data = temp;
Swapped = true;
}
Ptr1 = ptr1->next;
}
Lptr = ptr1;
} while (swapped);
}

Void displayList(Node* start) {


While (start) {
Cout << start->data << “ “;
Start = start->next;
}
Cout << “\n”;
}

Int main() {

Page 3 of 8
%
Enrollment Number: ____________________________
Node* head = new Node(5);
Head->next = new Node(2);
Head->next->next = new Node(9);
Head->next->next->next = new Node(1);
Head->next->next->next->next = new Node(7);

Cout << “Original linked list: “;


displayList(head);

bubbleSortLinkedList(head);

cout << “Sorted linked list: “;


displayList(head);

return 0;
}

Task #.3: Estimated Time: 30 Mins.


Write a program in C++ to sort an array of ‘n’ elements using Selection Sort Algorithm.

#include <iostream>
Using namespace std;
Void selectionSort(int arr[], int n) {
For (int I = 0; I < n – 1; ++i) {

Int minIndex = I;
For (int j = I + 1; j < n; ++j) {
If (arr[j] < arr[minIndex]) {
minIndex = j;
}
Page 4 of 8
%
Enrollment Number: ____________________________
}

Int temp = arr[i];


Arr[i] = arr[minIndex];
Arr[minIndex] = temp;
}
}

Int main() {
Int n;

Cout << “Enter the number of elements: “;


Cin >> n;

Int arr[n];

Cout << “Enter the elements:\n”;


For (int I = 0; I < n; ++i) {
Cin >> arr[i];
}

selectionSort(arr, n);

cout << “Sorted array:\n”;


for (int I = 0; I < n; ++i) {
cout << arr[i] << “ “;
}

Return 0;
}

Page 5 of 8
%
Enrollment Number: ____________________________

Task #.4: Estimated Time: 30 Mins.


Write a program in C++ to sort an array of ‘n’ elements using Insertion Sort Algorithm

#include <iostream>
Using namespace std;
Void insertionSort(int arr[], int n) {
For (int I = 1; I < n; ++i) {
Int key = arr[i];
Int j = I – 1;

While (j >= 0 && arr[j] > key) {


Arr[j + 1] = arr[j];
--j;
}

Arr[j + 1] = key;
}
}

Int main() {
Int n;

Cout << “Enter the number of elements: “;

Page 6 of 8
%
Enrollment Number: ____________________________
Cin >> n;

Int arr[n];

Cout << “Enter the elements:\n”;


For (int I = 0; I < n; ++i) {
Cin >> arr[i];
}

insertionSort(arr, n);

cout << “Sorted array:\n”;


for (int I = 0; I < n; ++i) {
cout << arr[i] << “ “;
}

Return 0;
}

Lab Grading Sheet :

Page 7 of 8
%
Enrollment Number: ____________________________
Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 10
2. 10
3. 10
4. 10
Total 10 Signature

Note : Attempt all tasks and get them checked by your Lab Instructor.

Page 8 of 8

You might also like